ResourceFactoryValidationDecorator::create()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 9
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 18
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Metadata\Factory;
6
7
use LAG\AdminBundle\Exception\Validation\InvalidAdminException;
8
use LAG\AdminBundle\Exception\Validation\InvalidOperationException;
9
use LAG\AdminBundle\Metadata\AdminResource;
10
use LAG\AdminBundle\Validation\Constraint\AdminValid;
11
use Symfony\Component\Validator\Constraints\Valid;
12
use Symfony\Component\Validator\Validator\ValidatorInterface;
13
14
class ResourceFactoryValidationDecorator implements ResourceFactoryInterface
15
{
16
    public function __construct(
17
        private ValidatorInterface $validator,
18
        private ResourceFactoryInterface $decorated,
19
    ) {
20
    }
21
22
    public function create(AdminResource $definition): AdminResource
23
    {
24
        $resource = $this->decorated->create($definition);
25
        $errors = $this->validator->validate($resource, [new AdminValid(), new Valid()]);
26
27
        if ($errors->count() > 0) {
28
            throw new InvalidAdminException($resource->getName(), $errors);
29
        }
30
31
        foreach ($resource->getOperations() as $operation) {
32
            $errors = $this->validator->validate($operation, [new Valid()]);
33
34
            if ($errors->count() > 0) {
35
                throw new InvalidOperationException($resource->getName(), $operation->getName(), $errors);
0 ignored issues
show
Unused Code introduced by
The call to LAG\AdminBundle\Exceptio...xception::__construct() has too many arguments starting with $errors. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
                throw /** @scrutinizer ignore-call */ new InvalidOperationException($resource->getName(), $operation->getName(), $errors);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
36
            }
37
        }
38
39
        return $resource;
40
    }
41
}
42