Passed
Push — 0.4 ( 487c21 )
by jean
05:10
created

ValidateObjectStep   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A violationToArray() 0 9 2
A configureOptionResolver() 0 6 1
A execute() 0 11 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ProcessBundle\Step;
6
7
use Darkilliant\ProcessBundle\State\ProcessState;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
use Symfony\Component\Validator\ConstraintViolationInterface;
10
use Symfony\Component\Validator\ConstraintViolationListInterface;
11
use Symfony\Component\Validator\Validator\ValidatorInterface;
12
13
class ValidateObjectStep extends AbstractConfigurableStep
14
{
15
    /** @var ValidatorInterface */
16
    private $validator;
17
18 3
    public function __construct(ValidatorInterface $validator)
19
    {
20 3
        $this->validator = $validator;
21 3
    }
22
23 1
    public function configureOptionResolver(OptionsResolver $resolver): OptionsResolver
24
    {
25 1
        $resolver->setRequired(['groups']);
26 1
        $resolver->setDefault('groups', []);
27
28 1
        return parent::configureOptionResolver($resolver);
29
    }
30
31 2
    public function execute(ProcessState $state)
32
    {
33 2
        $violations = $this->validator->validate($state->getData(), null, $state->getOptions()['groups']);
34
35 2
        if ($violations->count() > 0) {
36 1
            $state->error('error with object', [
37 1
                'errors' => $this->violationToArray($violations),
38
            ]);
39 1
            $state->markFail();
40
41 1
            return;
42
        }
43 1
    }
44
45 1
    private function violationToArray(ConstraintViolationListInterface $violationList): array
46
    {
47 1
        $violations = [];
48
        /** @var ConstraintViolationInterface $violation */
49 1
        foreach ($violationList as $violation) {
50 1
            $violations[$violation->getPropertyPath()] = $violation->getMessage();
51
        }
52
53 1
        return $violations;
54
    }
55
}
56