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

Stencil_Installable_Plugin   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2
Metric Value
wmc 11
lcom 2
cbo 2
dl 0
loc 103
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get_download_link() 0 3 1
A has_upgrade() 0 16 2
A passed_requirements() 0 9 3
A get_directory() 0 3 1
A get_file_data() 0 5 1
A get_upgrader() 0 5 2
1
<?php
2
/**
3
 * Installable plugin
4
 *
5
 * @package Stencil
6
 * @subpackage Upgrader
7
 */
8
9
/**
10
 * Class Stencil_Installable_Plugin
11
 */
12
class Stencil_Installable_Plugin extends Stencil_Abstract_Installable implements Stencil_Installable_Interface {
13
	/**
14
	 * Download format.
15
	 */
16
	const DOWNLOAD_FORMAT = 'https://github.com/moorscode/%s/archive/master.zip';
17
18
	/**
19
	 * Required PHP version.
20
	 *
21
	 * @var string
22
	 */
23
	private $required_php_version = '0.0.0';
24
25
	/**
26
	 * Stencil_Installable constructor.
27
	 *
28
	 * @param string $slug Slug of the module.
29
	 * @param string $name Name of this module.
30
	 * @param string $required_php_version Minimal PHP version required.
31
	 */
32
	public function __construct( $slug, $name, $required_php_version = '5.2.0' ) {
33
		parent::__construct( $slug, $name );
34
		$this->required_php_version = $required_php_version;
35
	}
36
37
	/**
38
	 * Get the download link
39
	 *
40
	 * @return bool|string
41
	 */
42
	public function get_download_link() {
43
		return sprintf( self::DOWNLOAD_FORMAT, $this->slug );
44
	}
45
46
	/**
47
	 * Check if there is an upgrade available
48
	 *
49
	 * @return bool|mixed
50
	 */
51
	public function has_upgrade() {
52
		// Check if there are upgrades available.
53
		$path = $this->get_directory() . DIRECTORY_SEPARATOR . sprintf( '%s.php', $this->slug );
54
55
		if ( ! is_file( $path ) ) {
56
			$headers = array(
57
				'version' => '0.0.0',
58
			);
59
		} else {
60
			$headers = get_file_data( $path, array( 'version' => 'Version' ) );
61
		}
62
63
		$latest_version = Stencil_Installable_Versions::get( $this );
64
65
		return version_compare( $headers['version'], $latest_version, '<' );
66
	}
67
68
	/**
69
	 * Do all requirements pass so it is usable.
70
	 *
71
	 * @return bool|array TRUE if passed, array of errors if failed.
72
	 */
73
	public function passed_requirements() {
74
		$errors = array();
75
76
		if ( version_compare( PHP_VERSION, $this->required_php_version, '<' ) ) {
77
			$errors[] = sprintf( __( 'PHP version %s required, %s available.' ), $this->required_php_version, PHP_VERSION );
78
		}
79
80
		return empty( $errors ) ? true : $errors;
81
	}
82
83
84
	/**
85
	 * Get base directory
86
	 */
87
	public function get_directory() {
88
		return dirname( STENCIL_PATH ) . DIRECTORY_SEPARATOR . $this->slug;
89
	}
90
91
	/**
92
	 * Get file headers
93
	 *
94
	 * @return array
95
	 */
96
	public function get_file_data() {
97
		$path = $this->get_directory() . DIRECTORY_SEPARATOR . $this->slug . '.php';
98
99
		return get_file_data( $path, array( 'version' => 'Version' ) );
100
	}
101
102
	/**
103
	 * Upgrader
104
	 *
105
	 * @param bool $upgrading Upgrading or installing.
106
	 *
107
	 * @return Plugin_Upgrader
108
	 */
109
	public function get_upgrader( $upgrading = false ) {
110
		$skin = ( $upgrading ) ? new Plugin_Upgrader_Skin() : new Plugin_Installer_Skin();
111
112
		return new Plugin_Upgrader( $skin );
113
	}
114
}
115