Factory::createInstanceFor()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 12
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * custom factory to inject the event dispatcher into our constraints
4
 */
5
6
namespace Graviton\JsonSchemaBundle\Validator\Constraint;
7
8
use JsonSchema\Constraints\BaseConstraint;
9
use JsonSchema\Constraints\Factory as BaseFactory;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
12
/**
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  https://opensource.org/licenses/MIT MIT License
15
 * @link     http://swisscom.ch
16
 */
17
class Factory extends BaseFactory
18
{
19
20
    /**
21
     * @var EventDispatcherInterface
22
     */
23
    private $dispatcher = null;
24
25
    /**
26
     * set EventDispatcher
27
     *
28
     * @param EventDispatcherInterface $dispatcher dispatcher
29
     *
30
     * @return void
31
     */
32
    public function setEventDispatcher($dispatcher)
33
    {
34
        $this->dispatcher = $dispatcher;
35
    }
36
37
    /**
38
     * Create a constraint instance for the given constraint name.
39
     *
40
     * @param string $constraintName constraint name
41
     *
42
     * @throws InvalidArgumentException if is not possible create the constraint instance.
43
     *
44
     * @return BaseConstraint instance
45
     */
46
    public function createInstanceFor($constraintName)
47
    {
48
        $instance = parent::createInstanceFor($constraintName);
49
50
        if (!is_null($this->dispatcher) && is_callable([$instance, 'setEventDispatcher'])) {
51
            $instance->setEventDispatcher($this->dispatcher);
52
        }
53
54
        return $instance;
55
    }
56
}
57