ConstraintViolationListValueResolver::supports()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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