Completed
Push — develop ( ce465f...dc3813 )
by David
02:29 queued 11s
created

Wordlift_Install_Service::do_install()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 48
rs 9.1344
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B Wordlift_Install_Service::install() 0 33 6
1
<?php
2
/**
3
 * Installs: Install Service.
4
 *
5
 * The Installation Service.
6
 *
7
 * @since      3.18.0
8
 * @package    Wordlift
9
 * @subpackage Wordlift/install
10
 */
11
12
/**
13
 * Define the {@link Wordlift_Install_Service} interface.
14
 *
15
 * @since      3.18.0
16
 * @package    Wordlift
17
 * @subpackage Wordlift/install
18
 */
19
class Wordlift_Install_Service {
20
21
	/**
22
	 * A {@link Wordlift_Log_Service} instance.
23
	 *
24
	 * @since  3.18.0
25
	 * @access private
26
	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
27
	 */
28
	private $log;
29
30
	/**
31
	 * The singleton instance.
32
	 *
33
	 * @since  3.18.0
34
	 * @access private
35
	 * @var \Wordlift_Install_Service $instance A {@link Wordlift_Install_Service} instance.
36
	 */
37
	private static $instance;
38
39
	/**
40
	 * @var array
41
	 */
42
	private $installs;
43
44
	/**
45
	 * Wordlift_Install_Service constructor.
46
	 *
47
	 * @since 3.18.0
48
	 */
49
	public function __construct() {
50
51
		/** Installs. */
52
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install.php';
53
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-1-0-0.php';
54
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-3-10-0.php';
55
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-3-12-0.php';
56
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-3-14-0.php';
57
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-3-15-0.php';
58
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-3-18-0.php';
59
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-3-18-3.php';
60
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-3-19-5.php';
61
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-3-20-0.php';
62
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-3-23-4.php';
63
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-3-24-2.php';
64
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-all-entity-types.php';
65
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'install/class-wordlift-install-package-type.php';
66
67
		// Get the install services.
68
		$this->installs = array(
69
			new Wordlift_Install_1_0_0(),
70
			new Wordlift_Install_3_10_0(),
71
			new Wordlift_Install_3_12_0(),
72
			new Wordlift_Install_3_14_0(),
73
			new Wordlift_Install_3_15_0(),
74
			new Wordlift_Install_3_18_0(),
75
			new Wordlift_Install_3_18_3(),
76
			new Wordlift_Install_3_19_5(),
77
			new Wordlift_Install_3_20_0(),
78
			/*
79
			 * This should be enabled with #852.
80
			 */
81
			// new Wordlift_Install_All_Entity_Types(),
82
			new Wordlift_Install_Package_Type(),
83
			new Wordlift_Install_3_23_4(),
84
			new Wordlift_Install_3_24_2(),
85
		);
86
87
		self::$instance = $this;
88
89
		$this->log = Wordlift_Log_Service::get_logger( get_class() );
90
91
		add_action( 'init', array( $this, 'install' ) );
92
93
	}
94
95
	/**
96
	 * Get the singleton instance.
97
	 *
98
	 * @since 3.18.0
99
	 */
100
	public static function get_instance() {
101
102
		return self::$instance;
103
	}
104
105
	/**
106
	 * Loop thought all versions and install the updates.
107
	 *
108
	 * @return void
109
	 * @since 3.18.0
110
	 *
111
	 * @since 3.20.0 use a transient to avoid concurrent installation calls.
112
	 */
113
	public function install() {
114
115
		$version = null;
0 ignored issues
show
Unused Code introduced by
$version 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...
116
117
		if ( $this->install_required() && false === get_transient( '_wl_installing' ) ) {
118
			set_transient( '_wl_installing', true, 5 * MINUTE_IN_SECONDS );
119
120
			/** @var Wordlift_Install $install */
121
			foreach ( $this->installs as $install ) {
122
				// Get the install version.
123
				$version = $install->get_version();
124
125
				if ( version_compare( $version, $this->get_current_version(), '>' )
126
				     || $install->must_install() ) {
127
128
					$class_name = get_class( $install );
129
130
					$this->log->info( "Current version is {$this->get_current_version()}, installing $class_name..." );
131
132
					// Install version.
133
					$install->install();
134
135
					$this->log->info( "$class_name installed." );
136
137
					// Bump the version.
138
					update_option( 'wl_db_version', $version );
139
				}
140
141
			}
142
143
			@delete_transient( '_wl_installing' );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
144
		}
145
	}
146
147
	private function install_required() {
148
149
		/** @var Wordlift_Install $install */
150
		foreach ( $this->installs as $install ) {
151
			// Get the install version.
152
			$version = $install->get_version();
153
154
			if ( version_compare( $version, $this->get_current_version(), '>' )
155
			     || $install->must_install() ) {
156
				return true;
157
			}
158
159
		}
160
161
		return false;
162
	}
163
164
	/**
165
	 * Retrieve the current db version.
166
	 *
167
	 * @return type
168
	 */
169
	private function get_current_version() {
170
		return get_option( 'wl_db_version', '0.0.0' );
171
	}
172
173
}
174