Completed
Push — feature/update-json-schema-dep ( 21a471...3110e3 )
by Narcotic
03:12
created

Format::check()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
cc 1
eloc 5
nc 1
nop 4
crap 2
1
<?php
2
/**
3
 * custom format constraint
4
 */
5
6
namespace Graviton\JsonSchemaBundle\Validator\Constraint;
7
8
use JsonSchema\Constraints\Factory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Graviton\JsonSchemaBundl...ator\Constraint\Factory.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use JsonSchema\Constraints\FormatConstraint;
10
use Graviton\JsonSchemaBundle\Validator\Constraint\Event\ConstraintEventFormat;
11
use JsonSchema\Uri\UriRetriever;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
14
/**
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class Format extends FormatConstraint
20
{
21
22
    /**
23
     * @var EventDispatcherInterface
24
     */
25
    private $dispatcher;
26
27
    /**
28
     * @var Factory
29
     */
30
    private $factory;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
31
32
    /**
33
     * Format constructor.
34
     *
35
     * @param int               $checkMode    check mode
36
     * @param UriRetriever|null $uriRetriever uri retriever
37
     * @param Factory|null      $factory      factory
38
     */
39
    public function __construct(
40
        $checkMode = self::CHECK_MODE_NORMAL,
41
        UriRetriever $uriRetriever = null,
42
        Factory $factory = null
43
    ) {
44
        parent::__construct($checkMode, $uriRetriever, $factory);
45
        $this->factory = $factory;
46
    }
47
48
    /**
49
     * sets the event dispatcher
50
     *
51
     * @param EventDispatcherInterface $dispatcher dispatcher
52
     *
53
     * @return void
54
     */
55
    public function setEventDispatcher(EventDispatcherInterface $dispatcher)
56
    {
57
        $this->dispatcher = $dispatcher;
58
    }
59
60
    /**
61
     * checks the input
62
     *
63
     * @param mixed $element element
64
     * @param null  $schema  schema
65
     * @param null  $path    path
66
     * @param null  $i       iterator value
67
     *
68
     * @return void
69
     */
70
    public function check($element, $schema = null, $path = null, $i = null)
71
    {
72
        parent::check($element, $schema, $path, $i);
73
74
        $event = new ConstraintEventFormat($this->factory, $element, $schema, $path);
75
        $result = $this->dispatcher->dispatch(ConstraintEventFormat::NAME, $event);
76
77
        $this->addErrors($result->getErrors());
78
    }
79
}
80