Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/ElggUpgrade.php (1 issue)

Checks if properties have been declared.

Best Practice Bug Major
1
<?php
2
3
/**
4
 * Upgrade object for upgrades that need to be tracked
5
 * and listed in the admin area.
6
 *
7
 * @todo Expand for all upgrades to be \ElggUpgrade subclasses.
8
 */
9
10
use Elgg\TimeUsing;
11
use Elgg\Upgrade\Batch;
12
13
/**
14
 * Represents an upgrade that runs outside of the upgrade.php script.
15
 *
16
 * @package Elgg.Admin
17
 * @access private
18
 *
19
 * @property int $is_completed
20
 * @property int $processed
21
 * @property int $offset
22
 * @property int $has_errors
23
 */
24
class ElggUpgrade extends ElggObject {
25
26
	use TimeUsing;
27
	
28
	private $requiredProperties = [
29
		'id',
30
		'title',
31
		'description',
32
		'class',
33
	];
34
35
	/**
36
	 * Do not use.
37
	 *
38
	 * @access private
39
	 * @var callable
40
	 */
41
	public $_callable_egefps = 'elgg_get_entities_from_private_settings';
42
43
	/**
44
	 * Set subtype to upgrade
45
	 *
46
	 * @return null
47
	 */
48 5
	public function initializeAttributes() {
49 5
		parent::initializeAttributes();
50
51 5
		$this->attributes['subtype'] = 'elgg_upgrade';
52
53
		// unowned
54 5
		$this->attributes['container_guid'] = 0;
55 5
		$this->attributes['owner_guid'] = 0;
56 5
	}
57
58
	/**
59
	 * Mark this upgrade as completed
60
	 *
61
	 * @return bool
62
	 */
63 1
	public function setCompleted() {
64 1
		$this->setCompletedTime();
65 1
		return $this->is_completed = true;
66
	}
67
68
	/**
69
	 * Has this upgrade completed?
70
	 *
71
	 * @return bool
72
	 */
73
	public function isCompleted() {
74
		return (bool) $this->is_completed;
75
	}
76
77
	/**
78
	 * Sets an unique id for the upgrade
79
	 *
80
	 * @param string $id Upgrade id in format <plugin_name>:<yyymmddhh>
81
	 * @return void
82
	 */
83 5
	public function setID($id) {
84 5
		$this->id = $id;
85 5
	}
86
87
	/**
88
	 * Sets a class for the upgrade
89
	 *
90
	 * @param string $class Fully qualified class name
91
	 * @return void
92
	 */
93 5
	public function setClass($class) {
94 5
		$this->class = $class;
95 5
	}
96
97
	/**
98
	 * Return instance of the class that processes the data
99
	 *
100
	 * @return Batch|false
101
	 */
102 4
	public function getBatch() {
103 4
		return _elgg_services()->upgradeLocator->getBatch($this->class);
104
	}
105
106
	/**
107
	 * Sets the timestamp for when the upgrade completed.
108
	 *
109
	 * @param int $time Timestamp when upgrade finished. Defaults to now.
110
	 * @return bool
111
	 */
112 1
	public function setCompletedTime($time = null) {
113 1
		if (!$time) {
114 1
			$time = $this->getCurrentTime()->getTimestamp();
115
		}
116
117 1
		return $this->completed_time = $time;
0 ignored issues
show
Bug Best Practice introduced by
The property completed_time does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
118
	}
119
120
	/**
121
	 * Gets the time when the upgrade completed.
122
	 *
123
	 * @return string
124
	 */
125
	public function getCompletedTime() {
126
		return $this->completed_time;
127
	}
128
129
	/**
130
	 * Require an upgrade page.
131
	 *
132
	 * @return mixed
133
	 * @throws UnexpectedValueException
134
	 */
135 5
	public function save() {
136 5
		if (!isset($this->is_completed)) {
137 5
			$this->is_completed = 0;
138
		}
139
140 5
		foreach ($this->requiredProperties as $prop) {
141 5
			if (!$this->$prop) {
142 5
				throw new UnexpectedValueException("ElggUpgrade objects must have a value for the $prop property.");
143
			}
144
		}
145
146 5
		return parent::save();
147
	}
148
149
	/**
150
	 * Set a value as private setting or attribute.
151
	 *
152
	 * Attributes include title and description.
153
	 *
154
	 * @param string $name  Name of the attribute or private_setting
155
	 * @param mixed  $value Value to be set
156
	 * @return void
157
	 */
158 5
	public function __set($name, $value) {
159 5
		if (array_key_exists($name, $this->attributes)) {
160 4
			parent::__set($name, $value);
161
		} else {
162 5
			$this->setPrivateSetting($name, $value);
163
		}
164 5
	}
165
166
	/**
167
	 * Get an attribute or private setting value
168
	 *
169
	 * @param string $name Name of the attribute or private setting
170
	 * @return mixed
171
	 */
172 5
	public function __get($name) {
173
		// See if its in our base attribute
174 5
		if (array_key_exists($name, $this->attributes)) {
175 5
			return parent::__get($name);
176
		}
177
178 5
		return $this->getPrivateSetting($name);
179
	}
180
181
}
182