Completed
Push — master ( 6f39c4...0208de )
by Vincent
02:02 queued 11s
created

VersionService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 82.61%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 107
ccs 19
cts 23
cp 0.8261
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createCache() 0 17 3
A getVersion() 0 3 1
A getRulesFileContent() 0 3 1
A setVersion() 0 6 1
A getRule() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure;
6
7
use VGirol\JsonApiConstant\Versions;
0 ignored issues
show
Bug introduced by
The type VGirol\JsonApiConstant\Versions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
/**
10
 * Service to manage the version of the specification that is used when validating documents.
11
 */
12
class VersionService
13
{
14
    use CanUseDotPath;
15
16
    /**
17
     * Undocumented variable
18
     *
19
     * @var string
20
     */
21
    private $default = Versions::VERSION_1_0;
22
23
    /**
24
     * Undocumented variable
25
     *
26 768
     * @var string
27
     */
28 768
    private $version;
29 768
30
    /**
31
     * Undocumented variable
32
     *
33
     * @var array
34
     */
35
    private $cache = [];
36
37
    /**
38 768
     * Create a new instance
39
     *
40 768
     * @param string|null $version The version of the JSON:API specification
41 768
     *
42
     * @return void
43 768
     */
44
    public function __construct(string $version = null)
45
    {
46
        $this->setVersion($version ?? $this->default);
47
    }
48
49
    /**
50
     * Set the version of the JSON:API specification
51
     *
52
     * @param string $version
53
     *
54
     * @return static
55
     */
56 441
    public function setVersion(string $version)
57
    {
58 441
        $this->version = $version;
59
        $this->createCache();
60
61 768
        return $this;
62
    }
63 768
64
    /**
65 768
     * Get the version of the JSON:API specification
66 768
     *
67
     * @return string
68
     */
69 768
    public function getVersion(): string
70 768
    {
71
        return $this->version;
72
    }
73
74
    /**
75 768
     * Undocumented function
76 768
     *
77 768
     * @param string $path
78 768
     *
79
     * @return mixed
80
     * @throws \VGirol\JsonApiStructure\Exception\DotPathException
81
     */
82
    public function getRule(string $path)
83
    {
84
        return $this->retrieve($path, $this->cache);
85
    }
86
87
    /**
88
     * Undocumented function
89
     *
90
     * @return array
91
     */
92
    protected function getRulesFileContent(): array
93
    {
94
        return include __DIR__ . '/VersionRules.php';
95
    }
96
97
    /**
98
     * Undocumented function
99
     *
100
     * @return void
101
     */
102
    private function createCache(): void
103
    {
104
        $rules = $this->getRulesFileContent();
105
106
        $major = 1;
107
        $minor = 0;
108
109
        do {
110
            $version = constant("\\VGirol\\JsonApiConstant\\Versions::VERSION_{$major}_{$minor}");
111
            if (!\array_key_exists($version, $rules)) {
112
                $minor = 0;
113
                $major++;
114
            }
115
116
            $this->cache = \array_merge_recursive($this->cache, $rules[$version]);
117
            $minor++;
118
        } while ($version != $this->version);
119
    }
120
}
121