Passed
Pull Request — master (#554)
by Alejandro
05:37
created

ValidationException::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 9.9332
ccs 11
cts 11
cp 1
crap 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 Throwable;
9
use Zend\InputFilter\InputFilterInterface;
10
use Zend\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
11
use Zend\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
12
13
use function Functional\reduce_left;
14
use function is_array;
15
use function print_r;
16
use function sprintf;
17
18
use const PHP_EOL;
19
20
class ValidationException extends InvalidArgumentException implements ProblemDetailsExceptionInterface
21
{
22
    use CommonProblemDetailsExceptionTrait;
23
24
    private const TITLE = 'Invalid data';
25
    private const TYPE = 'INVALID_ARGUMENT';
26
27
    /** @var array */
28
    private $invalidElements;
29
30 19
    public function __construct(
31
        string $message = '',
32
        array $invalidElements = [],
33
        int $code = 0,
34
        ?Throwable $previous = null
35
    ) {
36 19
        $this->invalidElements = $invalidElements;
37 19
        parent::__construct($message, $code, $previous);
38
    }
39
40 7
    public static function fromInputFilter(InputFilterInterface $inputFilter, ?Throwable $prev = null): self
41
    {
42 7
        return static::fromArray($inputFilter->getMessages(), $prev);
43
    }
44
45 14
    public static function fromArray(array $invalidData, ?Throwable $prev = null): self
46
    {
47 14
        $status = StatusCodeInterface::STATUS_BAD_REQUEST;
48 14
        $e = new self('Provided data is not valid', $invalidData, $status, $prev);
49
50 14
        $e->detail = $e->getMessage();
51 14
        $e->title = self::TITLE;
52 14
        $e->type = self::TYPE;
53 14
        $e->status = StatusCodeInterface::STATUS_BAD_REQUEST;
54 14
        $e->additional = [
55 14
            'invalidElements' => $invalidData,
56
        ];
57
58 14
        return $e;
59
    }
60
61 6
    public function getInvalidElements(): array
62
    {
63 6
        return $this->invalidElements;
64
    }
65
66 1
    public function __toString(): string
67
    {
68 1
        return sprintf(
69 1
            '%s %s in %s:%s%s%sStack trace:%s%s',
70 1
            __CLASS__,
71 1
            $this->getMessage(),
72 1
            $this->getFile(),
73 1
            $this->getLine(),
74 1
            $this->invalidElementsToString(),
75 1
            PHP_EOL,
76 1
            PHP_EOL,
77 1
            $this->getTraceAsString()
78
        );
79
    }
80
81 1
    private function invalidElementsToString(): string
82
    {
83
        return reduce_left($this->invalidElements, function ($messageSet, string $name, $_, string $acc) {
84 1
            return $acc . sprintf(
85 1
                "\n    '%s' => %s",
86 1
                $name,
87 1
                is_array($messageSet) ? print_r($messageSet, true) : $messageSet
88
            );
89 1
        }, '');
90
    }
91
}
92