Passed
Pull Request — master (#258)
by Alejandro
03:13
created

ChainIpLocationResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 31
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A resolveIpLocation() 0 14 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\IpGeolocation;
5
6
use Shlinkio\Shlink\Common\Exception\WrongIpException;
7
8
class ChainIpLocationResolver implements IpLocationResolverInterface
9
{
10
    /**
11
     * @var IpLocationResolverInterface[]
12
     */
13
    private $resolvers;
14
15 3
    public function __construct(IpLocationResolverInterface ...$resolvers)
16
    {
17 3
        $this->resolvers = $resolvers;
18
    }
19
20
    /**
21
     * @param string $ipAddress
22
     * @return array
23
     * @throws WrongIpException
24
     */
25 3
    public function resolveIpLocation(string $ipAddress): array
26
    {
27 3
        $error = null;
28
29 3
        foreach ($this->resolvers as $resolver) {
30
            try {
31 3
                return $resolver->resolveIpLocation($ipAddress);
32 2
            } catch (WrongIpException $e) {
33 2
                $error = $e;
34
            }
35
        }
36
37
        // If this instruction is reached, it means no resolver was capable of resolving the address
38 1
        throw WrongIpException::fromIpAddress($ipAddress, $error);
39
    }
40
}
41