IpAddressIdentityResolver::getIdentity()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 1
crap 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