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

IpAddressIdentityResolver::getIdentity()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 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