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

Factory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 33
ccs 0
cts 11
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A createInstanceFor() 0 14 4
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