Completed
Push — master ( cb5363...63e3c2 )
by Nikola
02:41
created

IpAddressIdentityGenerator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 55.56%

Importance

Changes 0
Metric Value
dl 0
loc 24
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1
ccs 5
cts 9
cp 0.5556
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getIdentity() 0 18 4
1
<?php
2
/**
3
 * This file is part of the Rate Limit package.
4
 *
5
 * Copyright (c) Nikola Posa
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace RateLimit\Identity;
14
15
use Psr\Http\Message\RequestInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
18
/**
19
 * @author Nikola Posa <[email protected]>
20
 */
21
final class IpAddressIdentityGenerator implements IdentityGeneratorInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 2
    public function getIdentity(RequestInterface $request) : string
27
    {
28 2
        if (!$request instanceof ServerRequestInterface) {
29
            return 'ANONYMOUS';
30
        }
31
32 2
        $serverParams = $request->getServerParams();
33
34 2
        if (array_key_exists('HTTP_CLIENT_IP', $serverParams)) {
35 2
            return $serverParams['HTTP_CLIENT_IP'];
36
        }
37
38
        if (array_key_exists('HTTP_X_FORWARDED_FOR', $serverParams)) {
39
            return $serverParams['HTTP_X_FORWARDED_FOR'];
40
        }
41
42
        return $serverParams['REMOTE_ADDR'] ?? 'UNKNOWN';
43
    }
44
}
45