Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SecurityEvent
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Audit\Domain\Model;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * Security Audit Event Value Object.
13 *
14 * Encapsulates an audited security incident or event (NIST SP 800-53 AU-3 / OWASP ASVS V7.3).
15 *
16 * @package App\Core\Audit\Domain\Model
17 */
18final readonly class SecurityEvent
19{
20    /**
21     * SecurityEvent constructor.
22     *
23     * @param SecurityEventType      $eventType     Classified event type.
24     * @param SecurityEventSeverity  $severity      Severity level.
25     * @param string                 $message       Human-readable event summary.
26     * @param array<string, mixed>   $context       Sanitized context data.
27     * @param string                 $ipAddress     Client IP address.
28     * @param int|null               $actorId       Optional actor user ID.
29     * @param string|null            $userAgent     Optional client user agent string.
30     * @param string|null            $requestUri    Optional request URI.
31     * @param string|null            $requestMethod Optional HTTP request method.
32     * @param string|null            $requestId     Optional request correlation UUID.
33     * @param int|null               $id            Database ID if persisted.
34     * @param string|null            $createdAt     ISO-8601 creation timestamp.
35     */
36    public function __construct(
37        public SecurityEventType     $eventType,
38        public SecurityEventSeverity $severity,
39        public string                $message,
40        public array                 $context = [],
41        public string                $ipAddress = '127.0.0.1',
42        public ?int                  $actorId = null,
43        public ?string               $userAgent = null,
44        public ?string               $requestUri = null,
45        public ?string               $requestMethod = null,
46        public ?string               $requestId = null,
47        public ?int                  $id = null,
48        public ?string               $createdAt = null,
49    ) {
50    }
51
52    /**
53     * Converts the entity into an associative array representation.
54     *
55     * @return array<string, mixed> Array representation of security event.
56     */
57    public function toArray(): array
58    {
59        return [
60            'id'             => $this->id,
61            'event_type'     => $this->eventType->value,
62            'severity'       => $this->severity->value,
63            'message'        => $this->message,
64            'context'        => $this->context,
65            'actor_id'       => $this->actorId,
66            'ip_address'     => $this->ipAddress,
67            'user_agent'     => $this->userAgent,
68            'request_uri'    => $this->requestUri,
69            'request_method' => $this->requestMethod,
70            'request_id'     => $this->requestId,
71            'created_at'     => $this->createdAt,
72        ];
73    }
74}