Repository   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 56.59%

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 3
dl 0
loc 210
ccs 30
cts 53
cp 0.5659
rs 9.92
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findPackage() 0 18 2
A findPackages() 0 15 5
A hasPackage() 0 12 3
A filterPackages() 0 12 3
A getPackages() 0 8 2
A reload() 0 4 1
A getPaths() 0 4 1
A addPath() 0 8 2
A count() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A getIterator() 0 4 1
A initialize() 0 15 5
1
<?php
2
/**
3
 * Repository.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Packages!
9
 * @subpackage     Repository
10
 * @since          1.0.0
11
 *
12
 * @date           30.05.15
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Packages\Repository;
18
19
use Nette;
20
21
use IPub\Packages\Entities;
22
use IPub\Packages\Loaders;
23
24
/**
25
 * Packages repository
26
 *
27
 * @package        iPublikuj:Packages!
28
 * @subpackage     Repository
29
 *
30
 * @author         Adam Kadlec <[email protected]>
31
 */
32 1
final class Repository implements IRepository
33
{
34
	/**
35
	 * Implement nette smart magic
36
	 */
37 1
	use Nette\SmartObject;
38
39
	/**
40
	 * @var array
41
	 */
42
	private $paths = [];
43
44
	/**
45
	 * @var Loaders\ILoader
46
	 */
47
	private $loader;
48
49
	/**
50
	 * @var Entities\IPackage[]
51
	 */
52
	private $packages;
53
54
	/**
55
	 * @param Loaders\ILoader $loader
56
	 */
57
	public function __construct(Loaders\ILoader $loader)
58
	{
59 1
		$this->loader = $loader;
60 1
	}
61
62
	/**
63
	 * {@inheritdoc}
64
	 */
65
	public function findPackage(string $name, string $version = 'latest')
66
	{
67
		// normalize name
68 1
		$name = strtolower($name);
69
70 1
		if ($version === 'latest') {
71 1
			$packages = $this->findPackages($name);
72
73 1
			usort($packages, function (Entities\Package $a, Entities\Package $b) {
74
				return version_compare($a->getVersion(), $b->getVersion());
75 1
			});
76
77 1
			return end($packages);
78
79
		} else {
80
			return current($this->findPackages($name, $version));
81
		}
82
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 */
87
	public function findPackages(string $name, string $version = NULL) : array
88
	{
89
		// normalize name
90 1
		$name = strtolower($name);
91
92 1
		$packages = [];
93
94 1
		foreach ($this->getPackages() as $package) {
95 1
			if ($package->getName() === $name && ($version === NULL || $version === $package->getVersion())) {
96 1
				$packages[] = $package;
97
			}
98
		}
99
100 1
		return $packages;
101
	}
102
103
	/**
104
	 * {@inheritdoc}
105
	 */
106
	public function hasPackage(Entities\IPackage $package) : bool
107
	{
108
		$packageId = $package->getUniqueName();
109
110
		foreach ($this->getPackages() as $repoPackage) {
111
			if ($packageId === $repoPackage->getUniqueName()) {
112
				return TRUE;
113
			}
114
		}
115
116
		return FALSE;
117
	}
118
119
	/**
120
	 * {@inheritdoc}
121
	 */
122
	public function filterPackages(callable $callback) : array
123
	{
124
		$packages = [];
125
126
		foreach ($this->getPackages() as $package) {
127
			if (call_user_func($callback, $package) === TRUE) {
128
				$packages[$package->getName()] = $package;
129
			}
130
		}
131
132
		return $packages;
133
	}
134
135
	/**
136
	 * {@inheritdoc}
137
	 */
138
	public function getPackages() : array
139
	{
140 1
		if ($this->packages === NULL) {
141 1
			$this->initialize();
142
		}
143
144 1
		return $this->packages;
145
	}
146
147
	/**
148
	 * {@inheritdoc}
149
	 */
150
	public function reload() : void
151
	{
152
		$this->initialize();
153
	}
154
155
	/**
156
	 * {@inheritdoc}
157
	 */
158
	public function getPaths() : array
159
	{
160
		return $this->paths;
161
	}
162
163
	/**
164
	 * {@inheritdoc}
165
	 */
166
	public function addPath($path) : void
167
	{
168 1
		if (!is_array($path)) {
169 1
			$path = [$path];
170
		}
171
172 1
		$this->paths = array_merge($this->paths, $path);
173 1
	}
174
175
	/**
176
	 * {@inheritdoc}
177
	 */
178
	public function count()
179
	{
180
		return count($this->getPackages());
181
	}
182
183
	/**
184
	 * {@inheritdoc}
185
	 */
186
	public function offsetExists($name)
187
	{
188
		return isset($this->packages[$name]);
189
	}
190
191
	/**
192
	 * {@inheritdoc}
193
	 */
194
	public function offsetGet($name)
195
	{
196
		return $this->findPackage($name);
197
	}
198
199
	/**
200
	 * {@inheritdoc}
201
	 */
202
	public function offsetSet($name, $package)
203
	{
204
		$this->packages[$name] = $package;
205
	}
206
207
	/**
208
	 * {@inheritdoc}
209
	 */
210
	public function offsetUnset($name)
211
	{
212
		unset($this->packages[$name]);
213
	}
214
215
	/**
216
	 * {@inheritdoc}
217
	 */
218
	public function getIterator()
219
	{
220
		return new \ArrayIterator($this->packages);
221
	}
222
223
	/**
224
	 * Initializes the packages array. Mostly meant as an extension point
225
	 */
226
	private function initialize()
227
	{
228 1
		$this->packages = [];
229
230 1
		foreach ($this->paths as $path) {
231 1
			$files = glob($path . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . 'composer.json', GLOB_NOSORT) ?: [];
232
233 1
			foreach ($files as $file) {
234
				/** @var Entities\IPackage $package */
235 1
				if ($package = $this->loader->load($file)) {
236 1
					$this->packages[$package->getName()] = $package;
237
				}
238
			}
239
		}
240 1
	}
241
}
242