Stencil_Installables::add_installable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Stencil Installable package
4
 *
5
 * @package Stencil
6
 * @subpackage Upgrader
7
 */
8
9
/**
10
 * Class Stencil_Installable
11
 */
12
class Stencil_Installables {
13
14
	/**
15
	 * List of registered installables.
16
	 *
17
	 * @var array
18
	 */
19
	private $installables = array();
20
21
	/**
22
	 * Register all installable items.
23
	 */
24
	public function __construct() {
25
		$this->add_installable( new Stencil_Installable_Plugin( 'stencil', 'Stencil' ) );
26
		$this->add_installable( new Stencil_Installable_Plugin( 'stencil-dwoo2', 'Dwoo 2', '5.4.0' ) );
27
		$this->add_installable( new Stencil_Installable_Plugin( 'stencil-mustache', 'Mustache' ) );
28
		$this->add_installable( new Stencil_Installable_Plugin( 'stencil-savant3', 'Savant 3' ) );
29
		$this->add_installable( new Stencil_Installable_Plugin( 'stencil-smarty2', 'Smarty 2.x' ) );
30
		$this->add_installable( new Stencil_Installable_Plugin( 'stencil-smarty3', 'Smarty 3.x' ) );
31
		$this->add_installable( new Stencil_Installable_Plugin( 'stencil-twig', 'Twig' ) );
32
33
		$this->add_installable( new Stencil_Installable_Theme( 'dwoo2', 'Dwoo' ) );
34
		$this->add_installable( new Stencil_Installable_Theme( 'mustache', 'Mustache' ) );
35
		$this->add_installable( new Stencil_Installable_Theme( 'smarty', 'Smarty' ) );
36
		$this->add_installable( new Stencil_Installable_Theme( 'twig', 'Twig' ) );
37
	}
38
39
	/**
40
	 * Add an installable to the list.
41
	 *
42
	 * @param Stencil_Installable_Interface $installable Installable to add.
43
	 */
44
	public function add_installable( Stencil_Installable_Interface $installable ) {
45
		$this->installables[] = $installable;
46
	}
47
48
	/**
49
	 * Get Installable by slug.
50
	 *
51
	 * @param string $slug Slug to find.
52
	 *
53
	 * @return mixed|null
54
	 */
55
	public function get_by_slug( $slug ) {
56
		foreach ( $this->installables as $installable ) {
57
			if ( $installable->get_slug() === $slug ) {
58
				return $installable;
59
			}
60
		}
61
62
		return null;
63
	}
64
65
	/**
66
	 * Retrieve all plugins
67
	 *
68
	 * @return Iterator list of Plugins.
69
	 */
70
	public function get_plugins() {
71
		return $this->get_installable_filtered( 'Stencil_Installable_Plugin' );
72
	}
73
74
	/**
75
	 * Get all registered themes.
76
	 *
77
	 * @return Iterator list of Themes.
78
	 */
79
	public function get_themes() {
80
		return $this->get_installable_filtered( 'Stencil_Installable_Theme' );
81
	}
82
83
	/**
84
	 * Get upgradable installables
85
	 *
86
	 * @return array of upgradable installables.
87
	 */
88
	public function get_upgradable() {
89
		$output = array();
90
91
		foreach ( $this->installables as $installable ) {
92
			if ( $installable->has_upgrade() ) {
93
				$output[] = $installable;
94
			}
95
		}
96
97
		return $output;
98
	}
99
100
	/**
101
	 * Get specific installables.
102
	 *
103
	 * @param string $class Class to get objects of.
104
	 *
105
	 * @return array
106
	 */
107
	private function get_installable_filtered( $class ) {
108
109
		$output = array();
110
		foreach ( $this->installables as $installable ) {
111
			if ( is_a( $installable, $class ) ) {
112
				$output[] = $installable;
113
			}
114
		}
115
116
		return $output;
117
	}
118
}
119