ConstraintViolationListValueResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 34
ccs 14
cts 14
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
A resolve() 0 22 4
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