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

Basic::getFirst()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 5

Duplication

Lines 4
Ratio 30.77 %

Importance

Changes 0
Metric Value
dl 4
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 4
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, Modules\Settings, Explorer;
13
14
	abstract class Basic extends Extend\Utils\Loader {
15
16
		protected $section = '', $loaded = false, $primary = null, $active = null;
17
18
		/**
19
		 * Load an item data
20
		 *
21
		 * @return array|null : the data or null on failure
22
		 */
23
24
		protected function loadItem(string $name) {
25
26
			return ($this->loaded ? ($this->items[$name] ?? null) : parent::loadItem($name));
27
		}
28
29
		/**
30
		 * Load a first item data
31
		 *
32
		 * @return array|null : the data or null on failure
33
		 */
34
35
		protected function loadFirst() {
36
37
			if ($this->loaded) return ($this->items[key($this->items)] ?? null);
38
39
			foreach (Explorer::iterateDirs($this->dir_name) as $name) {
40
41
				if (null !== ($data = $this->loadItem($name))) return $data;
42
			}
43
44
			# ------------------------
45
46
			return null;
47
		}
48
49
		/**
50
		 * Load a primary item data
51
		 *
52
		 * @return array|null : the data or null on failure
53
		 */
54
55
		protected function loadPrimary() {
56
57
			$primary = static::$default[$this->section];
58
59
			return ($this->loadItem($primary) ?? null);
60
		}
61
62
		/**
63
		 * Load an active item data
64
		 *
65
		 * @return array|null : the data or null on failure
66
		 */
67
68
		protected function loadActive() {
69
70
			$active = Settings::get(static::$param[$this->section]);
71
72
			return ($this->loadItem($active) ?? $this->primary ?? $this->loadFirst());
73
		}
74
75
		/**
76
		 * Constructor
77
		 */
78
79
		public function __construct(string $section, bool $load_all = true) {
80
81
			$this->section = (($section === SECTION_ADMIN) ? SECTION_ADMIN : SECTION_SITE);
82
83
			$this->dir_name = static::$root_dir[$this->section];
84
85
			if ($load_all) { $this->items = $this->loadItems(); $this->loaded = true; }
86
87
			$this->primary = $this->loadPrimary(); $this->active = $this->loadActive();
88
		}
89
90
		/**
91
		 * Activate an item
92
		 *
93
		 * @param $save : tells to save the new value in the global settings
94
		 *
95
		 * @return bool : true on success or false on failure
96
		 */
97
98
		public function activate(string $name, bool $save = false) : bool {
99
100
			if (null === ($data = $this->loadItem($name))) return false;
101
102
			if ($save && ((!Settings::set(static::$param[$this->section], $name)) || !Settings::save())) return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression \Modules\Settings::set(s...$this->section], $name) of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
103
104
			$this->active = $data;
105
106
			# ------------------------
107
108
			return true;
109
		}
110
111
		/**
112
		 * Get the active section
113
		 */
114
115
		public function getSection() : string {
116
117
			return $this->section;
118
		}
119
120
		/**
121
		 * Get the primary item data or a specific param value
122
		 *
123
		 * @return array|mixed|false : the data array, the param value, or false if the primary item was not loaded
124
		 */
125
126
		public function getPrimary(string $param = null) {
127
128
			if (null !== $param) return ($this->primary[$param] ?? false);
129
130
			return ($this->primary ?? false);
131
		}
132
133
		/**
134
		 * Get the active item data or a specific param value
135
		 *
136
		 * @return array|mixed|false : the data array, the param value, or false if the active item was not loaded
137
		 */
138
139
		public function get(string $param = null) {
140
141
			if (null !== $param) return ($this->active[$param] ?? false);
142
143
			return ($this->active ?? false);
144
		}
145
	}
146
}
147