Passed
Push — master ( c02d8b...47d203 )
by Andreas
09:55
created

callback   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 51.85%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 26
c 3
b 0
f 0
dl 0
loc 57
ccs 14
cts 27
cp 0.5185
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 9 5
A to_array() 0 13 3
A add_violation() 0 12 3
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\validation;
7
8
use Symfony\Component\Validator\Context\ExecutionContextInterface;
9
use midcom\datamanager\storage\container\container;
10
11
class callback
12
{
13
    private array $validation;
14
15 19
    public function __construct(array $validation)
16
    {
17 19
        $this->validation = $validation;
18
    }
19
20 8
    public function validate($payload, ExecutionContextInterface $context)
21
    {
22 8
        foreach ($this->validation as $entry) {
23 8
            if (!empty($entry['callback'])) {
24 8
                $form = $context->getObject();
25 8
                $result = $entry['callback']($this->to_array($form->getData()));
26 8
                if (is_array($result)) {
27
                    foreach ($result as $field => $message) {
28
                        $this->add_violation([$field], $context, $message);
29
                    }
30
                }
31
            }
32
        }
33
    }
34
35
    private function add_violation(array $path, ExecutionContextInterface $context, $data)
36
    {
37
        if (is_string($data)) {
38
            $context
39
                ->buildViolation('[' . implode('][', $path) . ']')
40
                ->atPath($path)
0 ignored issues
show
Bug introduced by
$path of type array is incompatible with the type string expected by parameter $path of Symfony\Component\Valida...lderInterface::atPath(). ( Ignorable by Annotation )

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

40
                ->atPath(/** @scrutinizer ignore-type */ $path)
Loading history...
41
                ->addViolation();
42
        } else {
43
            foreach ($data as $field => $message) {
44
                $childpath = $path;
45
                $childpath[] = $field;
46
                $this->add_violation($childpath, $context, $message);
47
            }
48
        }
49
    }
50
51
    /**
52
     * @param container|array $container
53
     * @return array
54
     */
55 8
    private function to_array($container) : array
56
    {
57 8
        if (is_array($container)) {
58
            // This is a newly added subform, not yet saved, so it's already in view format
59
            return $container;
60
        }
61 8
        $data = [];
62
63 8
        foreach ($container as $field => $value) {
64 8
            $data[$field] = $value->get_value();
65
        }
66
67 8
        return $data;
68
    }
69
}
70