Passed
Push — master ( a357e7...195a07 )
by Anton
04:22 queued 01:11
created

Addons::install()   B

Complexity

Conditions 9
Paths 11

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 7.041
c 0
b 0
f 0
cc 9
eloc 9
nc 11
nop 2
1
<?php
2
3
namespace Modules\Extend\Utils\Loader {
4
5
	use Modules\Extend, Utils\Schema, Arr, Explorer;
6
7
	abstract class Addons extends Extend\Utils\Loader {
8
9
		# Get items
10
11
		protected function getItems() {
12
13
			$items = [];
14
15
			foreach (Explorer::iterateDirs($this->dir_name) as $name) {
16
17
				if (null !== ($data = $this->getItem($name))) {
18
19
					$items[$name] = $data; $items[$name]['installed'] = false;
20
				}
21
			}
22
23 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...
24
25
				if (isset($items[$item['name']])) $items[$item['name']]['installed'] = true;
26
			}
27
28
			# ------------------------
29
30
			return Arr::sortby($items, 'title');
31
		}
32
33
		# Get installed items
34
35
		protected function getItemsInstalled() {
36
37
			$items = [];
38
39 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...
40
41
				if (static::$extension_class::valid($item['name'])) $items[$item['name']] = $item;
42
			}
43
44
			# ------------------------
45
46
			return $items;
47
		}
48
49
		# Constructor
50
51
		public function __construct(bool $load_all = true) {
52
53
			$this->dir_name = static::$root_dir;
54
55
			$this->items = ($load_all ? $this->getItems() : $this->getItemsInstalled());
56
		}
57
58
		# Install/uninstall item
59
60
		public function install(string $name, bool $value = true) {
61
62
			if (!isset($this->items[$name])) return false;
63
64
			$items = [];
65
66
			foreach ($this->items as $item) {
67
68
				if ($value) { if ($item['installed'] || ($item['name'] === $name)) $items[] = $item; }
69
70
				else { if ($item['installed'] && ($item['name'] !== $name)) $items[] = $item; }
71
			}
72
73
			if (!Schema::get(static::$schema)->save($items)) return false;
74
75
			$this->items[$name]['installed'] = $value;
76
77
			# ------------------------
78
79
			return true;
80
		}
81
	}
82
}
83