Completed
Pull Request — develop (#645)
by Alejandro
05:15
created

ShortUrlNotFoundException   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 25
rs 10
ccs 13
cts 13
cp 1
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromNotFound() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core\Exception;
6
7
use Fig\Http\Message\StatusCodeInterface;
8
use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
9
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
10
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
11
12
use function sprintf;
13
14
class ShortUrlNotFoundException extends DomainException implements ProblemDetailsExceptionInterface
15
{
16
    use CommonProblemDetailsExceptionTrait;
17
18
    private const TITLE = 'Short URL not found';
19
    private const TYPE = 'INVALID_SHORTCODE';
20
21 10
    public static function fromNotFound(ShortUrlIdentifier $identifier): self
22
    {
23 10
        $shortCode = $identifier->shortCode();
24 10
        $domain = $identifier->domain();
25 10
        $suffix = $domain === null ? '' : sprintf(' for domain "%s"', $domain);
26 10
        $e = new self(sprintf('No URL found with short code "%s"%s', $shortCode, $suffix));
27
28 10
        $e->detail = $e->getMessage();
29 10
        $e->title = self::TITLE;
30 10
        $e->type = self::TYPE;
31 10
        $e->status = StatusCodeInterface::STATUS_NOT_FOUND;
32 10
        $e->additional = ['shortCode' => $shortCode];
33
34 10
        if ($domain !== null) {
35 1
            $e->additional['domain'] = $domain;
36
        }
37
38 10
        return $e;
39
    }
40
}
41