Passed
Push — master ( 505ae7...78ca0f )
by Jip
03:42
created

Stencil_Abstract_Installable   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1
Metric Value
wmc 21
lcom 1
cbo 1
dl 0
loc 217
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get_slug() 0 3 1
A is_installed() 0 3 1
A has_upgrade() 0 3 1
A passed_requirements() 0 3 1
A __toString() 0 3 1
A remove() 0 3 1
C install() 0 100 12
A upgrade() 0 3 1
A cancel_installer() 0 5 1
1
<?php
2
/**
3
 * Abstract Installable
4
 *
5
 * @package Stencil
6
 * @subpackage Upgrader
7
 */
8
9
/**
10
 * Class Stencil_Installable
11
 */
12
abstract class Stencil_Abstract_Installable implements Stencil_Installable_Interface {
13
	/**
14
	 * Slug of this module.
15
	 *
16
	 * @var string
17
	 */
18
	protected $slug;
19
20
	/**
21
	 * Readable name.
22
	 *
23
	 * @var string
24
	 */
25
	protected $name;
26
27
	/**
28
	 * Version of this module.
29
	 *
30
	 * @var string
31
	 */
32
	protected $version = '0.0.0';
33
34
	/**
35
	 * Stencil_Installable constructor.
36
	 *
37
	 * @param string $slug Slug of the module.
38
	 * @param string $name Name of this module.
39
	 */
40
	public function __construct( $slug, $name ) {
41
		$this->slug = $slug;
42
		$this->name = $name;
43
	}
44
45
	/**
46
	 * Get slug name.
47
	 *
48
	 * @return string
49
	 */
50
	public function get_slug() {
51
		return $this->slug;
52
	}
53
54
	/**
55
	 * Is this installable installed.
56
	 *
57
	 * @return bool
58
	 */
59
	public function is_installed() {
60
		return is_dir( $this->get_directory() );
61
	}
62
63
	/**
64
	 * Check if there is an upgrade available
65
	 *
66
	 * @return bool|mixed
67
	 */
68
	public function has_upgrade() {
69
		return false;
70
	}
71
72
	/**
73
	 * Do all requirements pass so it is usable.
74
	 *
75
	 * @return bool|array TRUE if passed, array of errors if failed.
76
	 */
77
	public function passed_requirements() {
78
		return true;
79
	}
80
81
	/**
82
	 * Get the name.
83
	 *
84
	 * @return string
85
	 */
86
	public function __toString() {
0 ignored issues
show
Coding Style introduced by
function __toString() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-z_0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
87
		return $this->name;
88
	}
89
90
	/**
91
	 * Remove/uninstall
92
	 *
93
	 * @return bool
94
	 */
95
	public function remove() {
96
		return Stencil_File_System::remove( $this->get_directory() );
97
	}
98
99
	/**
100
	 * Install
101
	 *
102
	 * @param bool $upgrading Installing or upgrading.
103
	 *
104
	 * @return bool|WP_Error True on succes, WP_Error on failure
105
	 */
106
	public function install( $upgrading = false ) {
107
		$download_link = $this->get_download_link();
108
		$target_path   = $this->get_directory();
109
110
		require ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
111
112
		$upgrader = $this->get_upgrader();
113
		$upgrader->init();
114
115
		if ( ! $upgrading ) {
116
			$upgrader->install_strings();
117
		} else {
118
			$upgrader->upgrade_strings();
119
		}
120
121
		$skin = $upgrader->skin;
122
123
		$skin->header();
124
125
		// Connect to the Filesystem first.
126
		$res = $upgrader->fs_connect( array( WP_CONTENT_DIR, $target_path ) );
127
128
		// Mainly for non-connected filesystem.
129
		if ( ! $res ) {
130
			$skin->footer();
131
132
			return false;
133
		}
134
135
		$skin->before();
136
137
		if ( is_wp_error( $res ) ) {
138
			$this->cancel_installer( $skin, $res );
139
140
			return $res;
141
		}
142
143
		/**
144
		 * Download the package (Note, This just returns the filename
145
		 * of the file if the package is a local file)
146
		 */
147
		$download = $upgrader->download_package( $download_link );
148
		if ( is_wp_error( $download ) ) {
149
			$this->cancel_installer( $skin, $download );
150
151
			return $download;
152
		}
153
154
		// Unzips the file into a temporary directory.
155
		$working_dir = $upgrader->unpack_package( $download, true );
156
		if ( is_wp_error( $working_dir ) ) {
157
			$this->cancel_installer( $skin, $working_dir );
158
159
			return $working_dir;
160
		}
161
162
		$maintenance_mode = false;
0 ignored issues
show
Unused Code introduced by
$maintenance_mode is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
163
		$temporary_path   = $target_path . '_upgrading';
164
165
		if ( $upgrading ) {
166
			$maintenance_mode = $this->need_maintenance();
0 ignored issues
show
Bug introduced by
The method need_maintenance() does not seem to exist on object<Stencil_Abstract_Installable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
167
			$upgrader->maintenance_mode( $maintenance_mode );
168
169
			$skin->feedback( 'remove_old' );
170
171
			if ( is_dir( $temporary_path ) ) {
172
				Stencil_File_System::remove( $temporary_path );
173
			}
174
175
			// Move current install.
176
			Stencil_File_System::move( $target_path, $temporary_path );
177
178
			$skin->feedback( 'installing_package' );
179
180
		} else {
181
			$skin->feedback( 'installing_package' );
182
		}
183
184
		$installed = Stencil_File_System::move( $working_dir . DIRECTORY_SEPARATOR . $this->get_slug() . '-master', $target_path );
185
		Stencil_File_System::remove( $working_dir );
186
187
		if ( $upgrading ) {
188
			if ( false === $installed || is_wp_error( $installed ) ) {
189
				// Restore old install.
190
				Stencil_File_System::move( $temporary_path, $target_path );
191
192
				return false;
193
			} else {
194
				// Remove old install.
195
				Stencil_File_System::remove( $temporary_path );
196
			}
197
		}
198
199
		$upgrader->maintenance_mode( false );
200
201
		$skin->feedback( $installed ? 'process_success' : 'process_failed' );
202
203
		// Done.
204
		return true;
205
	}
206
207
	/**
208
	 * Upgrade
209
	 *
210
	 * @return bool
211
	 * @throws Exception When an upgrade is already in progress for this package.
212
	 */
213
	public function upgrade() {
214
		return $this->install( true );
215
	}
216
217
	/**
218
	 * Cancel installer.
219
	 *
220
	 * @param WP_Upgrader_Skin $skin Skin to set message on.
221
	 * @param WP_Error|string $error Error to display.
222
	 */
223
	protected function cancel_installer( $skin, $error ) {
224
		$skin->error( $error );
225
		$skin->after();
226
		$skin->footer();
227
	}
228
}
229