Completed
Push — master ( a7d308...df23f2 )
by Alejandro
25s queued 11s
created

ValidationException::formMessagesToString()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 1
crap 2
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 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
    /** @var array */
29
    private $invalidElements;
30
31 7
    public static function fromInputFilter(InputFilterInterface $inputFilter, ?Throwable $prev = null): self
32
    {
33 7
        return static::fromArray($inputFilter->getMessages(), $prev);
34
    }
35
36 14
    public static function fromArray(array $invalidData, ?Throwable $prev = null): self
37
    {
38 14
        $status = StatusCodeInterface::STATUS_BAD_REQUEST;
39 14
        $e = new self('Provided data is not valid', $status, $prev);
40
41 14
        $e->detail = $e->getMessage();
42 14
        $e->title = self::TITLE;
43 14
        $e->type = self::TYPE;
44 14
        $e->status = StatusCodeInterface::STATUS_BAD_REQUEST;
45 14
        $e->invalidElements = $invalidData;
46 14
        $e->additional = ['invalidElements' => array_keys($invalidData)];
47
48 14
        return $e;
49
    }
50
51 1
    public function getInvalidElements(): array
52
    {
53 1
        return $this->invalidElements;
54
    }
55
56 1
    public function __toString(): string
57
    {
58 1
        return sprintf(
59 1
            '%s %s in %s:%s%s%sStack trace:%s%s',
60 1
            __CLASS__,
61 1
            $this->getMessage(),
62 1
            $this->getFile(),
63 1
            $this->getLine(),
64 1
            $this->invalidElementsToString(),
65 1
            PHP_EOL,
66 1
            PHP_EOL,
67 1
            $this->getTraceAsString()
68
        );
69
    }
70
71 1
    private function invalidElementsToString(): string
72
    {
73
        return reduce_left($this->getInvalidElements(), function ($messageSet, string $name, $_, string $acc) {
74 1
            return $acc . sprintf(
75 1
                "\n    '%s' => %s",
76 1
                $name,
77 1
                is_array($messageSet) ? print_r($messageSet, true) : $messageSet
78
            );
79 1
        }, '');
80
    }
81
}
82