Completed
Push — master ( 4a3e49...406f94 )
by Alejandro
16s queued 12s
created

ChainIpLocationResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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