VersionService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 84%

Importance

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