Completed
Push — master ( 0faaef...a96626 )
by Nikola
02:09
created

IpAddressIdentityResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

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\Middleware\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 IpAddressIdentityResolver extends AbstractIdentityResolver
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 13
    public function getIdentity(RequestInterface $request) : string
27
    {
28 13
        if (!$request instanceof ServerRequestInterface) {
29 7
            return self::getDefaultIdentity($request);
30
        }
31
32 6
        $serverParams = $request->getServerParams();
33
34 6
        if (array_key_exists('HTTP_CLIENT_IP', $serverParams)) {
35 2
            return $serverParams['HTTP_CLIENT_IP'];
36
        }
37
38 4
        if (array_key_exists('HTTP_X_FORWARDED_FOR', $serverParams)) {
39 2
            return $serverParams['HTTP_X_FORWARDED_FOR'];
40
        }
41
42 2
        return $serverParams['REMOTE_ADDR'] ?? self::getDefaultIdentity($request);
43
    }
44
}
45