Repository::buildFromPath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CompoLab\Domain;
4
5
use CompoLab\Domain\ValueObject\Dir;
6
use CompoLab\Domain\ValueObject\File;
7
use CompoLab\Domain\ValueObject\Url;
8
9
final class Repository implements \Countable, \JsonSerializable
10
{
11
    const INDEX = 'packages.json';
12
13
    /** @var Url */
14
    private $baseUrl;
15
16
    /** @var Dir */
17
    private $cachePath;
18
19
    /** @var Package[] */
20
    private $packages = [];
21
22
    /** @var int */
23
    private $count = 0;
24
25 2
    public function __construct(Url $baseUrl, Dir $cachePath, array $packages = [])
26
    {
27 2
        $this->baseUrl = $baseUrl;
28 2
        $this->cachePath = $cachePath;
29
30 2
        foreach ($packages as $package) {
31
            $this->addPackage($package);
32
        }
33 2
    }
34
35 1
    public function getBaseUrl(): Url
36
    {
37 1
        return $this->baseUrl;
38
    }
39
40 2
    public function getUrl(string $uri): Url
41
    {
42 2
        return new Url(
43 2
            sprintf('%s/%s',
44 2
                rtrim($this->baseUrl, '/'),
45 2
                ltrim($uri, '/'))
46
        );
47
    }
48
49 1
    public function getIndexUrl(): Url
50
    {
51 1
        return $this->getUrl(self::INDEX);
52
    }
53
54 1
    public function getCachePath(): Dir
55
    {
56 1
        return $this->cachePath;
57
    }
58
59 1
    public function getFile(string $path): File
60
    {
61 1
        return new File(
62 1
            sprintf('%s/%s',
63 1
                $this->cachePath,
64 1
                ltrim($path, '/'))
65
        );
66
    }
67
68 3
    public function getIndexFile(): File
69
    {
70 3
        return new File(
71 3
            sprintf('%s/%s', $this->cachePath, self::INDEX)
72
        );
73
    }
74
75 4
    public function addPackage(Package $package)
76
    {
77 4
        if (!isset($this->packages[$package->getName()])) {
78 4
            $this->packages[$package->getName()] = [];
79
        }
80
81 4
        $this->packages[$package->getName()][(string) $package->getVersion()] = $package;
82 4
        $this->count++;
83 4
    }
84
85 3
    public function removePackage(Package $package)
86
    {
87 3
        if (!isset($this->packages[$package->getName()])) {
88
            return;
89
        }
90
91 3
        if (!isset($this->packages[$package->getName()][(string) $package->getVersion()])) {
92
            return;
93
        }
94
95 3
        unset($this->packages[$package->getName()][(string) $package->getVersion()]);
96 3
        $this->count--;
97
98 3
        if (empty($this->packages[$package->getName()])) {
99
            unset($this->packages[$package->getName()]);
100
        }
101 3
    }
102
103
    /**
104
     *
105
     * @return Package[]
106
     */
107 1
    public function getPackages(): array
108
    {
109 1
        $packages = [];
110 1
        foreach ($this->packages as $vendors) {
111 1
            foreach ($vendors as $package) {
112 1
                $packages[] = $package;
113
            }
114
        }
115
116 1
        return $packages;
117
    }
118
119 1
    public static function buildFromPath(Url $baseUrl, Dir $cachePath, string $path): self
120
    {
121 1
        if (!$json = file_get_contents($path)) {
122
            throw new \RuntimeException(sprintf('File "%s" is not readable', $path));
123
        }
124
125 1
        return self::buildFromJson($baseUrl, $cachePath, $json);
126
    }
127
128 2
    public static function buildFromJson(Url $baseUrl, Dir $cachePath, string $json): self
129
    {
130 2
        if (!$data = json_decode($json, true)) {
131
            throw new \RuntimeException('Impossible to decode JSON string as array');
132
        }
133
134 2
        if (!isset($data['packages'])) {
135
            throw new \RuntimeException('Malformed JSON');
136
        }
137
138 2
        $repository = new self($baseUrl, $cachePath);
139
140 2
        foreach ($data['packages'] as $packages) {
141 2
            foreach ($packages as $package) {
142 2
                $repository->addPackage(Package::buildFromArray((string) $cachePath, $package));
143
            }
144
        }
145
146 2
        return $repository;
147
    }
148
149 5
    public function count(): int
150
    {
151 5
        return $this->count;
152
    }
153
154 2
    public function jsonSerialize()
155
    {
156
        return [
157 2
            'packages' => $this->packages
158
        ];
159
    }
160
}
161