Completed
Push — master ( 0c53d4...debbec )
by David
09:08 queued 10s
created

Wordlift_Install_Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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
	 * The singleton instance.
23
	 *
24
	 * @since  3.18.0
25
	 * @access private
26
	 * @var \Wordlift_Install_Service $instance A {@link Wordlift_Install_Service} instance.
27
	 */
28
	private static $instance;
29
30
	/**
31
	 * A {@link Wordlift_Log_Service} instance.
32
	 *
33
	 * @since  3.18.0
34
	 * @access private
35
	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
36
	 */
37
	private $log;
38
39
	/**
40
	 * Wordlift_Install_Service constructor.
41
	 *
42
	 * @since 3.18.0
43
	 */
44
	public function __construct() {
45
46
		self::$instance = $this;
47
48
		$this->log = Wordlift_Log_Service::get_logger( get_class() );
49
50
	}
51
52
	/**
53
	 * Get the singleton instance.
54
	 *
55
	 * @since 3.18.0
56
	 */
57
	public static function get_instance() {
58
59
		return self::$instance;
60
	}
61
62
	/**
63
	 * Loop thought all versions and install the updates.
64
	 *
65
	 * @since 3.18.0
66
	 *
67
	 * @return void
68
	 */
69
	public function install() {
70
71
		// Get the install services.
72
		$installs = array(
73
			new Wordlift_Install_1_0_0(),
74
			new Wordlift_Install_3_10_0(),
75
			new Wordlift_Install_3_12_0(),
76
			new Wordlift_Install_3_14_0(),
77
			new Wordlift_Install_3_15_0(),
78
			new Wordlift_Install_3_18_0(),
79
		);
80
81
		$version = null;
82
83
		/** @var Wordlift_Install $install */
84
		foreach ( $installs as $install ) {
85
			// Get the install version.
86
			$version = $install->get_version();
87
88
			if ( version_compare( $version, $this->get_current_version(), '>' ) ) {
89
				$this->log->debug( "Current version is {$this->get_current_version()}, installing v$version..." );
90
				// Install version.
91
				$install->install();
92
93
				$this->log->info( "v$version installed." );
94
95
			}
96
97
		}
98
99
		// Bump the `wl_db_version`.
100
		if ( null !== $version ) {
101
			update_option( 'wl_db_version', $version );
102
		}
103
104
	}
105
106
	/**
107
	 * Retrieve the current db version.
108
	 *
109
	 * @return type
110
	 */
111
	private function get_current_version() {
112
		return get_option( 'wl_db_version', '0.0.0' );
113
	}
114
115
}
116