Completed
Push — master ( 37bc7f...665429 )
by Adam
06:53
created

Package::getExtra()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Package.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Packages!
9
 * @subpackage     Entities
10
 * @since          1.0.0
11
 *
12
 * @date           27.09.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Packages\Entities;
18
19
use Nette;
20
use Nette\Utils;
21
22
use IPub;
23
use IPub\Packages\Exceptions;
24
25
/**
26
 * Package abstract entity
27
 *
28
 * @package        iPublikuj:Packages!
29
 * @subpackage     Entities
30
 *
31
 * @author         Adam Kadlec <[email protected]>
32
 */
33 1
abstract class Package extends Nette\Object implements IPackage
34
{
35
	/**
36
	 * @var mixed
37
	 */
38
	protected $composerData;
39
40
	/**
41
	 * @param mixed[] $composerData
42
	 */
43
	public function __construct(array $composerData)
44
	{
45 1
		$this->composerData = $composerData;
46
47 1
		if (!isset($composerData['name'])) {
48
			throw new Exceptions\UnexpectedValueException('Unknown package has no name defined (' . json_encode($composerData) . ').');
49
		}
50 1
	}
51
52
	/**
53
	 * {@inheritdoc}
54
	 */
55
	public function getName() : string
56
	{
57 1
		return $this->composerData['name'];
58
	}
59
60
	/**
61
	 * {@inheritdoc}
62
	 */
63
	public function getTitle()
64
	{
65
		return isset($this->composerData['title']) ? $this->composerData['title'] : NULL;
66
	}
67
68
	/**
69
	 * {@inheritdoc}
70
	 */
71
	public function hasTitle() : bool
72
	{
73
		return $this->getTitle() ? TRUE : FALSE;
74
	}
75
76
	/**
77
	 * {@inheritdoc}
78
	 */
79
	public function getDescription()
80
	{
81
		return $this->composerData['description'];
82
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 */
87
	public function getVersion() : string
88
	{
89
		return isset($this->composerData['version']) ? $this->composerData['version'] : '0.0.0';
90
	}
91
92
	/**
93
	 * {@inheritdoc}
94
	 */
95
	public function getType() : string
96
	{
97
		return isset($this->composerData['type']) ? $this->composerData['type'] : 'undefined';
98
	}
99
100
	/**
101
	 * {@inheritdoc}
102
	 */
103 View Code Duplication
	public function getKeywords() : Utils\ArrayHash
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...
104
	{
105
		$keywords = [];
106
107
		if (isset($this->composerData['keywords'])) {
108
			$keywords = $this->composerData['keywords'];
109
110
			$keywords = is_array($keywords) ? $keywords : array_map('trim', explode(',', $keywords));
111
		}
112
113
		return Utils\ArrayHash::from($keywords);
114
	}
115
116
	/**
117
	 * {@inheritdoc}
118
	 */
119
	public function getHomepage()
120
	{
121
		return isset($this->composerData['homepage']) ? $this->composerData['homepage'] : NULL;
122
	}
123
124
	/**
125
	 * {@inheritdoc}
126
	 */
127
	public function getReleaseDate()
128
	{
129
		if (isset($this->composerData['time'])) {
130
			try {
131
				return new Utils\DateTime($this->composerData['time'], new \DateTimeZone('UTC'));
132
133
			} catch (\Exception $ex) {
134
				// If date could not be converted to object, than is in wrong format and is not added to package
135
			}
136
		}
137
138
		return NULL;
139
	}
140
141
	/**
142
	 * {@inheritdoc}
143
	 */
144 View Code Duplication
	public function getLicense() : Utils\ArrayHash
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...
145
	{
146
		$licenses = [];
147
148
		if (isset($this->composerData['license'])) {
149
			$licenses = $this->composerData['license'];
150
151
			$licenses = is_array($licenses) ? $licenses : array_map('trim', explode(',', $licenses));
152
		}
153
154
		return Utils\ArrayHash::from($licenses);
155
	}
156
157
	/**
158
	 * {@inheritdoc}
159
	 */
160
	public function getAuthors() : Utils\ArrayHash
161
	{
162
		$authors = [];
163
164
		if (isset($this->composerData['authors'])) {
165
			$authors = $this->composerData['authors'];
166
		}
167
168
		return Utils\ArrayHash::from($authors);
169
	}
170
171
172
	/**
173
	 * {@inheritdoc}
174
	 */
175
	public function getSupport()
176
	{
177
		return isset($this->composerData['support']) ? $this->composerData['support'] : NULL;
178
	}
179
180
	/**
181
	 * {@inheritdoc}
182
	 */
183 View Code Duplication
	public function getRequire() : array
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...
184
	{
185
		$ret = [];
186
187
		if (isset($this->composerData['require'])) {
188
			foreach ($this->composerData['require'] as $name => $require) {
189
				if (strpos($name, '/') !== FALSE) {
190
					$ret[] = $name;
191
				}
192
			}
193
		}
194
195
		return $ret;
196
	}
197
198
	/**
199
	 * {@inheritdoc}
200
	 */
201 View Code Duplication
	public function getRequireDev() : array
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...
202
	{
203
		$ret = [];
204
205
		if (isset($this->composerData['require-dev'])) {
206
			foreach ($this->composerData['require-dev'] as $name => $require) {
207
				if (strpos($name, '/') !== FALSE) {
208
					$ret[] = $name;
209
				}
210
			}
211
		}
212
213
		return $ret;
214
	}
215
216
	/**
217
	 * {@inheritdoc}
218
	 */
219
	public function getAutoload() : Utils\ArrayHash
220
	{
221
		$autoload = [];
222
223
		if (isset($this->composerData['autoload'])) {
224
			$autoload = $this->composerData['autoload'];
225
		}
226
227
		return Utils\ArrayHash::from($autoload);
228
	}
229
230
	/**
231
	 * {@inheritdoc}
232
	 */
233
	public function getExtra() : Utils\ArrayHash
234
	{
235
		$data = [];
236
237
		if (isset($this->composerData['extra'])) {
238
			$data = $this->composerData['extra'];
239
		}
240
241
		return Utils\ArrayHash::from($data);
242
	}
243
244
	/**
245
	 * {@inheritdoc}
246
	 */
247 View Code Duplication
	public function getConfiguration() : Utils\ArrayHash
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...
248
	{
249
		$data = [];
250
251
		if (isset($this->composerData['extra']['ipub']['configuration'])) {
252
			$data = $this->composerData['extra']['ipub']['configuration'];
253
		}
254
255
		return Utils\ArrayHash::from($data);
256
	}
257
258
	/**
259
	 * {@inheritdoc}
260
	 */
261 View Code Duplication
	public function getScripts() : Utils\ArrayHash
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...
262
	{
263
		$scripts = ['IPub\Packages\Scripts\ConfigurationScript'];
264
265
		if (isset($this->composerData['extra']['ipub']['scripts'])) {
266
			$scripts = array_merge($scripts, $this->composerData['extra']['ipub']['scripts']);
267
		}
268
269
		return Utils\ArrayHash::from($scripts);
270
	}
271
272
	/**
273
	 * {@inheritdoc}
274
	 */
275
	public function getUniqueName() : string
276
	{
277
		return sprintf('%s-%s', $this->getName(), $this->composerData['version']);
278
	}
279
280
	/**
281
	 * {@inheritdoc}
282
	 */
283
	public function getPath() : string
284
	{
285
		$reflectionClass = new \ReflectionClass($this);
286
287
		return dirname($reflectionClass->getFileName());
288
	}
289
290
	/**
291
	 * {@inheritdoc}
292
	 */
293
	public function compare(IPackage $package, string $operator = '==') : bool
294
	{
295
		return strtolower($this->getName()) === strtolower($package->getName()) &&
296
		version_compare(strtolower($this->getVersion()), strtolower($package->getVersion()), $operator);
297
	}
298
299
	/**
300
	 * {@inheritdoc}
301
	 */
302
	public function __toString() : string
303
	{
304
		return $this->getUniqueName();
305
	}
306
}
307