ConstraintViolationListValueResolver::resolve()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 11
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 4
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nelexa\RequestDtoBundle\ArgumentResolver;
6
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
9
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
10
use Symfony\Component\Validator\ConstraintViolationListInterface;
11
12
final class ConstraintViolationListValueResolver implements ArgumentValueResolverInterface
13
{
14
    public const REQUEST_ATTR_KEY = '_dto_violations';
15
16 15
    public function supports(Request $request, ArgumentMetadata $argument): bool
17
    {
18 15
        return is_a($argument->getType(), ConstraintViolationListInterface::class, true);
19
    }
20
21
    /**
22
     * @return iterable
23
     */
24 15
    public function resolve(Request $request, ArgumentMetadata $argument)
25
    {
26
        /** @var ConstraintViolationListInterface|null $errorsQueue */
27 15
        $violationList = $request->attributes->get(self::REQUEST_ATTR_KEY);
28
29 15
        if ($violationList === null) {
30 1
            throw new \InvalidArgumentException(
31 1
                sprintf(
32 1
                    'The action argument "%s \$%s" is required.',
33 1
                    (string) $argument->getType(),
34 1
                    $argument->getName()
35
                )
36
            );
37
        }
38
39 14
        $request->attributes->remove(self::REQUEST_ATTR_KEY);
40
41 14
        if ($argument->isNullable() && $violationList->count() === 0) {
42 2
            $violationList = null;
43
        }
44
45 14
        yield $violationList;
46 14
    }
47
}
48