ResourceFactoryValidationDecorator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 26
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 18 4
A __construct() 0 4 1
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