IpAddressIdentityResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 26
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getIdentity() 0 20 5
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
namespace RateLimit\Middleware\Identity;
12
13
use Psr\Http\Message\RequestInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
/**
17
 * @author Nikola Posa <[email protected]>
18
 */
19
final class IpAddressIdentityResolver extends AbstractIdentityResolver
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 13
    public function getIdentity(RequestInterface $request)
25
    {
26 13
        if (!$request instanceof ServerRequestInterface) {
27 7
            return self::getDefaultIdentity($request);
28
        }
29
30 6
        $serverParams = $request->getServerParams();
31
32 6
        if (array_key_exists('HTTP_CLIENT_IP', $serverParams)) {
33 2
            return $serverParams['HTTP_CLIENT_IP'];
34
        }
35
36 4
        if (array_key_exists('HTTP_X_FORWARDED_FOR', $serverParams)) {
37 2
            return $serverParams['HTTP_X_FORWARDED_FOR'];
38
        }
39
40 2
        return isset($serverParams['REMOTE_ADDR'])
41 1
                ? $serverParams['REMOTE_ADDR']
42 2
                : self::getDefaultIdentity($request);
43
    }
44
}
45