Completed
Push — master ( 554f9d...0d589d )
by
unknown
13:57
created

Loader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 2
cbo 5
dl 0
loc 81
ccs 19
cts 21
cp 0.9048
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addStrategy() 0 4 1
A load() 0 10 3
A createJsonDefinition() 0 15 2
1
<?php
2
/**
3
 * load definitions from a source
4
 *
5
 * This Loader implements the following strategies.
6
 * - file
7
 * - directory
8
 * - scan
9
 * - mongodb
10
 */
11
12
namespace Graviton\GeneratorBundle\Definition\Loader;
13
14
use Graviton\GeneratorBundle\Definition\Loader\Strategy\StrategyInterface;
15
use Graviton\GeneratorBundle\Definition\JsonDefinition;
16
use Graviton\JsonSchemaBundle\Exception\ValidationException;
17
use Graviton\JsonSchemaBundle\Validator\InvalidJsonException;
18
use Graviton\JsonSchemaBundle\Validator\ValidatorInterface;
19
use JMS\Serializer\SerializerInterface;
20
21
/**
22
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
23
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
24
 * @link     http://swisscom.ch
25
 */
26
class Loader implements LoaderInterface
27
{
28
    /**
29
     * @var StrategyInterface[]
30
     */
31
    private $strategies = [];
32
    /**
33
     * @var SerializerInterface
34
     */
35
    private $serializer;
36
    /**
37
     * @var ValidatorInterface
38
     */
39
    private $validator;
40
41
    /**
42
     * Constructor
43
     *
44
     * @param ValidatorInterface  $validator  Validator
45
     * @param SerializerInterface $serializer Serializer
46
     */
47 10
    public function __construct(ValidatorInterface $validator, SerializerInterface $serializer)
48
    {
49 10
        $this->validator = $validator;
50 10
        $this->serializer = $serializer;
51 10
    }
52
53
    /**
54
     * add a strategy to the loader
55
     *
56
     * @param StrategyInterface $strategy strategy to add
57
     *
58
     * @return Loader
0 ignored issues
show
Documentation introduced by
Should the return type not be Loader|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
59
     */
60 10
    public function addStrategy(StrategyInterface $strategy)
61
    {
62 10
        $this->strategies[] = $strategy;
63 10
    }
64
65
    /**
66
     * load from input
67
     *
68
     * @param string|null $input input from command
69
     *
70
     * @return JsonDefinition[]
71
     */
72 6
    public function load($input)
73
    {
74 6
        foreach ($this->strategies as $strategy) {
75 6
            if ($strategy->supports($input)) {
76 6
                return array_map([$this, 'createJsonDefinition'], $strategy->load($input));
77
            }
78
        }
79
80
        return [];
81
    }
82
83
    /**
84
     * Deserialize JSON definition
85
     *
86
     * @param string $json JSON code
87
     * @return JsonDefinition
88
     * @throws InvalidJsonException  If JSON is invalid
89
     * @throws ValidationException   If definition is not valid
90
     */
91 6
    protected function createJsonDefinition($json)
92
    {
93 6
        $errors = $this->validator->validateJsonDefinition($json);
94 4
        if (!empty($errors)) {
95 2
            throw new ValidationException($errors);
96
        }
97
98 2
        $definition = $this->serializer->deserialize(
99
            $json,
100 2
            'Graviton\\GeneratorBundle\\Definition\\Schema\\Definition',
101 2
            'json'
102
        );
103
104 2
        return new JsonDefinition($definition);
105
    }
106
}
107