Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.19% |
23 / 27 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| AuditOrigin | |
85.19% |
23 / 27 |
|
33.33% |
2 / 6 |
9.26 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getClientIp | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getClientUserAgent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getServerName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTerminalUserName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createFromContext | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\Other; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\ValueObjects\CompositeValueObject; |
| 6 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 7 | use Psr\Http\Message\ServerRequestInterface; |
| 8 | use RKA\Middleware\IpAddress; |
| 9 | use Symfony\Component\HttpFoundation\Request; |
| 10 | |
| 11 | class AuditOrigin implements ValueObjectInterface |
| 12 | { |
| 13 | use CompositeValueObject; |
| 14 | |
| 15 | public function __construct( |
| 16 | private readonly ?string $clientIp = null, |
| 17 | private readonly ?string $clientUserAgent = null, |
| 18 | private readonly ?string $serverName = null, |
| 19 | private readonly ?string $terminalUserName = null, |
| 20 | ) { |
| 21 | } |
| 22 | |
| 23 | public function getClientIp(): ?string |
| 24 | { |
| 25 | return $this->clientIp; |
| 26 | } |
| 27 | |
| 28 | public function getClientUserAgent(): ?string |
| 29 | { |
| 30 | return $this->clientUserAgent; |
| 31 | } |
| 32 | |
| 33 | public function getServerName(): ?string |
| 34 | { |
| 35 | return $this->serverName; |
| 36 | } |
| 37 | |
| 38 | public function getTerminalUserName(): ?string |
| 39 | { |
| 40 | return $this->terminalUserName; |
| 41 | } |
| 42 | |
| 43 | public static function createFromContext(ApieContext $context): self |
| 44 | { |
| 45 | $clientIp = null; |
| 46 | $clientUserAgent = null; |
| 47 | $serverName = null; |
| 48 | $terminalUserName = null; |
| 49 | |
| 50 | $request = $context->getContext(ServerRequestInterface::class, false); |
| 51 | |
| 52 | if ($request instanceof ServerRequestInterface) { |
| 53 | $trustedProxies = ['127.0.0.1']; |
| 54 | if (class_exists(Request::class)) { |
| 55 | $trustedProxies = Request::getTrustedProxies(); |
| 56 | } |
| 57 | $ip = new class(checkProxyHeaders: true, trustedProxies: $trustedProxies, headersToInspect: [ 'CF-Connecting-IP', 'True-Client-IP', 'X-Real-IP', 'Forwarded', 'X-Forwarded-For', 'X-Forwarded', 'X-Cluster-Client-Ip', 'Client-Ip', ]) extends IpAddress { |
| 58 | public function determineClientIpAddress($request): ?string |
| 59 | { |
| 60 | return parent::determineClientIpAddress($request); |
| 61 | } |
| 62 | }; |
| 63 | $clientIp = $ip->determineClientIpAddress($request); |
| 64 | $clientUserAgent = $request->getHeaderLine('User-Agent') ? : null; |
| 65 | ; |
| 66 | } else { |
| 67 | $serverName = gethostname(); |
| 68 | $terminalUserName = get_current_user(); |
| 69 | } |
| 70 | return new self( |
| 71 | $clientIp, |
| 72 | $clientUserAgent, |
| 73 | $serverName, |
| 74 | $terminalUserName |
| 75 | ); |
| 76 | } |
| 77 | } |