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

ShortUrlNotFoundException::fromNotFound()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

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