Passed
Push — master ( 289cbe...3ce129 )
by Anton
03:29
created

Addons::getItems()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 21
Code Lines 8

Duplication

Lines 4
Ratio 19.05 %

Importance

Changes 0
Metric Value
dl 4
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 8
nc 9
nop 0
1
<?php
2
3
/**
4
 * @package Cadmium\System\Modules\Extend
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules\Extend\Utils\Loader {
11
12
	use Modules\Extend, Utils\Schema;
13
14
	abstract class Addons extends Extend\Utils\Loader {
15
16
		/**
17
		 * Load the list of all items
18
		 */
19
20
		protected function loadItemsAll() : array {
21
22
			$items = $this->loadItems(['installed' => false]);
23
24 View Code Duplication
			foreach ((Schema::get(static::$schema)->load() ?? []) as $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
25
26
				if (isset($items[$item['name']])) $items[$item['name']]['installed'] = true;
27
			}
28
29
			# ------------------------
30
31
			return $items;
32
		}
33
34
		/**
35
		 * Load the list of installed items
36
		 */
37
38
		protected function loadItemsInstalled() : array {
39
40
			$items = [];
41
42 View Code Duplication
			foreach ((Schema::get(static::$schema)->load() ?? []) as $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
43
44
				if (static::$extension_class::isValid($item['name'])) $items[$item['name']] = $item;
45
			}
46
47
			# ------------------------
48
49
			return $items;
50
		}
51
52
		/**
53
		 * Constructor
54
		 */
55
56
		public function __construct(bool $load_all = true) {
57
58
			$this->dir_name = static::$root_dir;
59
60
			$this->items = ($load_all ? $this->loadItemsAll() : $this->loadItemsInstalled());
61
		}
62
63
		/**
64
		 * Install or uninstall an item
65
		 *
66
		 * @param $value : if set to true the item will be installed, otherwise uninstalled
67
		 *
68
		 * @return bool : true on success or false on failure
69
		 */
70
71
		public function install(string $name, bool $value = true) : bool {
72
73
			if (!isset($this->items[$name])) return false;
74
75
			$items = [];
76
77
			foreach ($this->items as $item) {
78
79
				if ($value) { if ($item['installed'] || ($item['name'] === $name)) $items[] = $item; }
80
81
				else { if ($item['installed'] && ($item['name'] !== $name)) $items[] = $item; }
82
			}
83
84
			if (!Schema::get(static::$schema)->save($items)) return false;
85
86
			$this->items[$name]['installed'] = $value;
87
88
			# ------------------------
89
90
			return true;
91
		}
92
	}
93
}
94