ConstraintTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 0
dl 0
loc 68
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
getEventClass() 0 1 ?
addErrors() 0 1 ?
A setEventDispatcher() 0 4 1
A check() 0 18 1
1
<?php
2
/**
3
 * trait for custom constraint classes
4
 */
5
6
namespace Graviton\JsonSchemaBundle\Validator\Constraint;
7
8
use JsonSchema\Entity\JsonPointer;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  https://opensource.org/licenses/MIT MIT License
14
 * @link     http://swisscom.ch
15
 */
16
trait ConstraintTrait
17
{
18
19
    /**
20
     * @var EventDispatcherInterface
21
     */
22
    private $dispatcher;
23
24
    /**
25
     * sets the event dispatcher
26
     *
27
     * @param EventDispatcherInterface $dispatcher dispatcher
28
     *
29
     * @return void
30
     */
31
    public function setEventDispatcher(EventDispatcherInterface $dispatcher)
32
    {
33
        $this->dispatcher = $dispatcher;
34
    }
35
36
    /**
37
     * checks the input
38
     *
39
     * @param mixed       $element           element
40
     * @param null        $schema            schema
41
     * @param JsonPointer $path              path
42
     * @param null        $properties        properties
43
     * @param null        $additionalProp    added props
44
     * @param null        $patternProperties pattern props
45
     * @param array       $appliedDefaults   applied defaults
46
     *
47
     * @return void
48
     */
49
    public function check(
50
        &$element,
51
        $schema = null,
52
        JsonPointer $path = null,
53
        $properties = null,
54
        $additionalProp = null,
55
        $patternProperties = null,
56
        $appliedDefaults = array()
57
    ) {
58
        $eventClass = $this->getEventClass();
59
60
        $event = new $eventClass($this->factory, $element, $schema, $path);
0 ignored issues
show
Bug introduced by
The property factory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
        $result = $this->dispatcher->dispatch($event, $event::NAME);
62
63
        $this->addErrors($result->getErrors());
64
65
        parent::check($element, $schema, $path, $properties, $additionalProp, $patternProperties, $appliedDefaults);
66
    }
67
68
    /**
69
     * Returns the name of the Event class for this event
70
     *
71
     * @return string event class name
72
     */
73
    abstract public function getEventClass();
74
75
    /**
76
     * Adds errors
77
     *
78
     * @param array $errors errors
79
     *
80
     * @return void
81
     */
82
    abstract public function addErrors(array $errors);
83
}
84