ValidationException   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 30
dl 0
loc 56
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromInputFilter() 0 3 1
A fromArray() 0 13 1
A getInvalidElements() 0 3 1
A invalidElementsToString() 0 7 2
A __toString() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core\Exception;
6
7
use Fig\Http\Message\StatusCodeInterface;
8
use Laminas\InputFilter\InputFilterInterface;
9
use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
10
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
11
use Throwable;
12
13
use function array_keys;
14
use function Functional\reduce_left;
15
use function is_array;
16
use function print_r;
17
use function sprintf;
18
19
use const PHP_EOL;
20
21
class ValidationException extends InvalidArgumentException implements ProblemDetailsExceptionInterface
22
{
23
    use CommonProblemDetailsExceptionTrait;
24
25
    private const TITLE = 'Invalid data';
26
    private const TYPE = 'INVALID_ARGUMENT';
27
28
    private array $invalidElements;
29
30 14
    public static function fromInputFilter(InputFilterInterface $inputFilter, ?Throwable $prev = null): self
31
    {
32 14
        return static::fromArray($inputFilter->getMessages(), $prev);
33
    }
34
35 21
    public static function fromArray(array $invalidData, ?Throwable $prev = null): self
36
    {
37 21
        $status = StatusCodeInterface::STATUS_BAD_REQUEST;
38 21
        $e = new self('Provided data is not valid', $status, $prev);
39
40 21
        $e->detail = $e->getMessage();
41 21
        $e->title = self::TITLE;
42 21
        $e->type = self::TYPE;
43 21
        $e->status = StatusCodeInterface::STATUS_BAD_REQUEST;
44 21
        $e->invalidElements = $invalidData;
45 21
        $e->additional = ['invalidElements' => array_keys($invalidData)];
46
47 21
        return $e;
48
    }
49
50 2
    public function getInvalidElements(): array
51
    {
52 2
        return $this->invalidElements;
53
    }
54
55 2
    public function __toString(): string
56
    {
57 2
        return sprintf(
58
            '%s %s in %s:%s%s%sStack trace:%s%s',
59
            __CLASS__,
60 2
            $this->getMessage(),
61 2
            $this->getFile(),
62 2
            $this->getLine(),
63 2
            $this->invalidElementsToString(),
64
            PHP_EOL,
65
            PHP_EOL,
66 2
            $this->getTraceAsString(),
67
        );
68
    }
69
70 2
    private function invalidElementsToString(): string
71
    {
72 2
        return reduce_left($this->getInvalidElements(), fn ($messages, string $name, $_, string $acc) => $acc . sprintf(
73
            "\n    '%s' => %s",
74 2
            $name,
75 2
            is_array($messages) ? print_r($messages, true) : $messages,
76 2
        ), '');
77
    }
78
}
79