Completed
Push — master ( 665429...116aa7 )
by Adam
07:10
created

Package::getType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
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
		$extra = $this->getExtra();
66
67
		return $extra->offsetExists('title') ? $extra->offsetGet('title') : $this->getName();
68
	}
69
70
	/**
71
	 * {@inheritdoc}
72
	 */
73
	public function hasTitle() : bool
74
	{
75
		return $this->getTitle() ? TRUE : FALSE;
76
	}
77
78
	/**
79
	 * {@inheritdoc}
80
	 */
81
	public function getDescription()
82
	{
83
		return $this->composerData['description'];
84
	}
85
86
	/**
87
	 * {@inheritdoc}
88
	 */
89
	public function getVersion() : string
90
	{
91
		return isset($this->composerData['version']) ? $this->composerData['version'] : '0.0.0';
92
	}
93
94
	/**
95
	 * {@inheritdoc}
96
	 */
97
	public function getType() : string
98
	{
99
		return isset($this->composerData['type']) ? $this->composerData['type'] : 'undefined';
100
	}
101
102
	/**
103
	 * {@inheritdoc}
104
	 */
105 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...
106
	{
107
		$keywords = [];
108
109
		if (isset($this->composerData['keywords'])) {
110
			$keywords = $this->composerData['keywords'];
111
112
			$keywords = is_array($keywords) ? $keywords : array_map('trim', explode(',', $keywords));
113
		}
114
115
		return Utils\ArrayHash::from($keywords);
116
	}
117
118
	/**
119
	 * {@inheritdoc}
120
	 */
121
	public function getHomepage()
122
	{
123
		return isset($this->composerData['homepage']) ? $this->composerData['homepage'] : NULL;
124
	}
125
126
	/**
127
	 * {@inheritdoc}
128
	 */
129
	public function getReleaseDate()
130
	{
131
		if (isset($this->composerData['time'])) {
132
			try {
133
				return new Utils\DateTime($this->composerData['time'], new \DateTimeZone('UTC'));
134
135
			} catch (\Exception $ex) {
136
				// If date could not be converted to object, than is in wrong format and is not added to package
137
			}
138
		}
139
140
		return NULL;
141
	}
142
143
	/**
144
	 * {@inheritdoc}
145
	 */
146 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...
147
	{
148
		$licenses = [];
149
150
		if (isset($this->composerData['license'])) {
151
			$licenses = $this->composerData['license'];
152
153
			$licenses = is_array($licenses) ? $licenses : array_map('trim', explode(',', $licenses));
154
		}
155
156
		return Utils\ArrayHash::from($licenses);
157
	}
158
159
	/**
160
	 * {@inheritdoc}
161
	 */
162
	public function getAuthors() : Utils\ArrayHash
163
	{
164
		$authors = [];
165
166
		if (isset($this->composerData['authors'])) {
167
			$authors = $this->composerData['authors'];
168
		}
169
170
		return Utils\ArrayHash::from($authors);
171
	}
172
173
174
	/**
175
	 * {@inheritdoc}
176
	 */
177
	public function getSupport()
178
	{
179
		return isset($this->composerData['support']) ? $this->composerData['support'] : NULL;
180
	}
181
182
	/**
183
	 * {@inheritdoc}
184
	 */
185 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...
186
	{
187
		$ret = [];
188
189
		if (isset($this->composerData['require'])) {
190
			foreach ($this->composerData['require'] as $name => $require) {
191
				if (strpos($name, '/') !== FALSE) {
192
					$ret[] = $name;
193
				}
194
			}
195
		}
196
197
		return $ret;
198
	}
199
200
	/**
201
	 * {@inheritdoc}
202
	 */
203 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...
204
	{
205
		$ret = [];
206
207
		if (isset($this->composerData['require-dev'])) {
208
			foreach ($this->composerData['require-dev'] as $name => $require) {
209
				if (strpos($name, '/') !== FALSE) {
210
					$ret[] = $name;
211
				}
212
			}
213
		}
214
215
		return $ret;
216
	}
217
218
	/**
219
	 * {@inheritdoc}
220
	 */
221
	public function getAutoload() : Utils\ArrayHash
222
	{
223
		$autoload = [];
224
225
		if (isset($this->composerData['autoload'])) {
226
			$autoload = $this->composerData['autoload'];
227
		}
228
229
		return Utils\ArrayHash::from($autoload);
230
	}
231
232
	/**
233
	 * {@inheritdoc}
234
	 */
235
	public function getExtra() : Utils\ArrayHash
236
	{
237
		$data = [];
238
239
		if (isset($this->composerData['extra'])) {
240
			$data = $this->composerData['extra'];
241
		}
242
243
		return Utils\ArrayHash::from($data);
244
	}
245
246
	/**
247
	 * {@inheritdoc}
248
	 */
249 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...
250
	{
251
		$data = [];
252
253
		if (isset($this->composerData['extra']['ipub']['configuration'])) {
254
			$data = $this->composerData['extra']['ipub']['configuration'];
255
		}
256
257
		return Utils\ArrayHash::from($data);
258
	}
259
260
	/**
261
	 * {@inheritdoc}
262
	 */
263 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...
264
	{
265
		$scripts = ['IPub\Packages\Scripts\ConfigurationScript'];
266
267
		if (isset($this->composerData['extra']['ipub']['scripts'])) {
268
			$scripts = array_merge($scripts, $this->composerData['extra']['ipub']['scripts']);
269
		}
270
271
		return Utils\ArrayHash::from($scripts);
272
	}
273
274
	/**
275
	 * {@inheritdoc}
276
	 */
277
	public function getUniqueName() : string
278
	{
279
		return sprintf('%s-%s', $this->getName(), $this->composerData['version']);
280
	}
281
282
	/**
283
	 * {@inheritdoc}
284
	 */
285
	public function getPath() : string
286
	{
287
		$reflectionClass = new \ReflectionClass($this);
288
289
		return dirname($reflectionClass->getFileName());
290
	}
291
292
	/**
293
	 * {@inheritdoc}
294
	 */
295
	public function compare(IPackage $package, string $operator = '==') : bool
296
	{
297
		return strtolower($this->getName()) === strtolower($package->getName()) &&
298
		version_compare(strtolower($this->getVersion()), strtolower($package->getVersion()), $operator);
299
	}
300
301
	/**
302
	 * {@inheritdoc}
303
	 */
304
	public function __toString() : string
305
	{
306
		return $this->getUniqueName();
307
	}
308
}
309