Completed
Pull Request — master (#364)
by Alejandro
11:50
created

IpCannotBeLocatedException::forLocalhost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Exception;
5
6
use Throwable;
7
8
class IpCannotBeLocatedException extends RuntimeException
9
{
10
    /** @var bool */
11
    private $isNonLocatableAddress;
12
13
    public function __construct(
14
        bool $isNonLocatableAddress,
15
        string $message,
16
        int $code = 0,
17
        ?Throwable $previous = null
18
    ) {
19
        $this->isNonLocatableAddress = $isNonLocatableAddress;
20
        parent::__construct($message, $code, $previous);
21
    }
22
23
    public static function forEmptyAddress(): self
24
    {
25
        return new self(true, 'Ignored visit with no IP address');
26
    }
27
28
    public static function forLocalhost(): self
29
    {
30
        return new self(true, 'Ignored localhost address');
31
    }
32
33
    public static function forError(Throwable $e): self
34
    {
35
        return new self(false, 'An error occurred while locating IP', $e->getCode(), $e);
36
    }
37
38
    /**
39
     * Tells if this error belongs to an address that will never be possible locate
40
     */
41
    public function isNonLocatableAddress(): bool
42
    {
43
        return $this->isNonLocatableAddress;
44
    }
45
}
46