Passed
Push — master ( fad3cd...8c9440 )
by Michael
02:31
created

YamlDefinitionProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 146
Duplicated Lines 5.48 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 8
loc 146
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A registerProcessor() 0 4 1
A getDefinition() 8 8 2
A createDefinition() 0 14 2
A readConfig() 0 12 2
A getSchema() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Definition;
5
6
use Mikemirten\Component\JsonApi\Mapper\Definition\ConfigurationProcessor\ConfigurationProcessorInterface;
7
use Mikemirten\Component\JsonApi\Mapper\Definition\Exception\DefinitionProviderException;
8
use Symfony\Component\Config\Definition\ConfigurationInterface as Configuration;
9
use Symfony\Component\Config\Definition\NodeInterface;
10
use Symfony\Component\Config\Definition\Processor;
11
use Symfony\Component\Yaml\Parser;
12
13
/**
14
 * YAML-config based definition provider
15
 *
16
 *                  +--------+                +-------------+
17
 *                  | Schema |->-+        +-->| Processor A |--+
18
 *                  +--------+   |        |   +-------------+  |
19
 *                               |        |                    |
20
 * +------+   +--------+   +-----------+  |   +-------------+  |   +------------+
21
 * | File |-->| Parser |-->| Config    |--+-->| Processor B |--+-->| Definition |
22
 * +------+   +--------+   | processor |      +-------------+      +------------+
23
 *                         +-----------+
24
 *
25
 * @package Mikemirten\Component\JsonApi\Mapper\Definition
26
 */
27
class YamlDefinitionProvider implements DefinitionProviderInterface
28
{
29
    /**
30
     * Path to directory with mapping definitions
31
     *
32
     * @var string
33
     */
34
    protected $basePath;
35
36
    /**
37
     * YAML-parser
38
     *
39
     * @var Parser
40
     */
41
    protected $parser;
42
43
    /**
44
     * Configuration processor
45
     * Handles schema, normalization and default values
46
     *
47
     * @var Processor
48
     */
49
    protected $processor;
50
51
    /**
52
     * Configuration
53
     * Provides schema of mapping definition
54
     *
55
     * @var Configuration
56
     */
57
    protected $config;
58
59
    /**
60
     * Lazy initialized schema of mapping definition
61
     *
62
     * @var NodeInterface
63
     */
64
    private $schema;
65
66
    /**
67
     * Registered configuration processors
68
     * Each processor handles certain part of definition
69
     *
70
     * @var ConfigurationProcessorInterface[]
71
     */
72
    private $processors = [];
73
74
    /**
75
     * Cache of created definitions
76
     *
77
     * @var array
78
     */
79
    private $definitionCache = [];
80
81
    /**
82
     * YamlDefinitionProvider constructor.
83
     *
84
     * @param string        $basePath
85
     * @param Parser        $parser
86
     * @param Processor     $processor
87
     * @param Configuration $config
88
     */
89 1
    public function __construct(string $basePath, Parser $parser, Processor $processor, Configuration $config)
90
    {
91 1
        $this->basePath  = $basePath;
92 1
        $this->parser    = $parser;
93 1
        $this->processor = $processor;
94 1
        $this->config    = $config;
95 1
    }
96
97
    /**
98
     * Register configuration processor
99
     *
100
     * @param ConfigurationProcessorInterface $processor
101
     */
102 1
    public function registerProcessor(ConfigurationProcessorInterface $processor)
103
    {
104 1
        $this->processors[] = $processor;
105 1
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 1 View Code Duplication
    public function getDefinition(string $class): Definition
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112 1
        if (! isset($this->definitionCache[$class])) {
113 1
            $this->definitionCache[$class] = $this->createDefinition($class);
114
        }
115
116 1
        return $this->definitionCache[$class];
117
    }
118
119
    /**
120
     * Create definition
121
     *
122
     * @param  string $class
123
     * @return Definition
124
     */
125 1
    protected function createDefinition(string $class): Definition
126
    {
127 1
        $path   = $this->basePath . DIRECTORY_SEPARATOR . str_replace('\\', '.', $class) . '.yml';
128 1
        $config = $this->readConfig($path);
129
130 1
        $definition = new Definition($class);
131
132 1
        foreach ($this->processors as $processor)
133
        {
134 1
            $processor->process($config, $definition);
135
        }
136
137 1
        return $definition;
138
    }
139
140
    /**
141
     * Read configuration of mapping definition
142
     *
143
     * @param  string $path
144
     * @return array
145
     */
146 1
    protected function readConfig(string $path): array
147
    {
148 1
        if (! is_readable($path)) {
149
            throw new DefinitionProviderException('Unable to read definition by path: ' . $path);
150
        }
151
152 1
        $content = file_get_contents($path);
153 1
        $config  = $this->parser->parse($content);
154 1
        $schema  = $this->getSchema();
155
156 1
        return $this->processor->process($schema, [$config]);
157
    }
158
159
    /**
160
     * Get schema of mapping definition
161
     *
162
     * @return NodeInterface
163
     */
164 1
    protected function getSchema(): NodeInterface
165
    {
166 1
        if ($this->schema === null) {
167 1
            $this->schema = $this->config->getConfigTreeBuilder()->buildTree();
168
        }
169
170 1
        return $this->schema;
171
    }
172
}