Completed
Pull Request — develop (#15)
by Narcotic
13:08
created

Factory::createInstanceFor()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.2
cc 4
eloc 7
nc 4
nop 2
crap 20
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\Factory as BaseFactory;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class Factory extends BaseFactory
17
{
18
19
    /**
20
     * @var EventDispatcherInterface
21
     */
22
    private $dispatcher = null;
23
24
    /**
25
     * Create a constraint instance for the given constraint name.
26
     *
27
     * @param string                   $constraintName  constraint name
28
     * @param EventDispatcherInterface $eventDispatcher dispatcher
29
     *
30
     * @throws InvalidArgumentException if is not possible create the constraint instance.
31
     *
32
     * @return ConstraintInterface|ObjectConstraint instance
33
     */
34
    public function createInstanceFor($constraintName, EventDispatcherInterface $eventDispatcher = null)
35
    {
36
        $instance = parent::createInstanceFor($constraintName);
37
38
        if (!is_null($eventDispatcher)) {
39
            $this->dispatcher = $eventDispatcher;
40
        }
41
42
        if (!is_null($this->dispatcher) && is_callable([$instance, 'setEventDispatcher'])) {
43
            $instance->setEventDispatcher($this->dispatcher);
44
        }
45
46
        return $instance;
47
    }
48
}
49