Stencil_Flow   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 18
lcom 1
cbo 2
dl 0
loc 140
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C get_page_actions() 0 40 7
C get_view_hierarchy() 0 38 8
A add_to_options() 0 16 3
1
<?php
2
/**
3
 * Flow controller class
4
 *
5
 * Builds all the actions that need to be executed on a certain page request.
6
 * Builds the view hierarchy of a certain page.
7
 *
8
 * @package Stencil
9
 */
10
11
/**
12
 * Class Flow
13
 */
14
class Stencil_Flow implements Stencil_Flow_Interface {
15
16
	/**
17
	 * Pages that are considered of the 'archive' type
18
	 *
19
	 * Allows for view to fall through to 'archive'
20
	 * Allows for listing posts
21
	 *
22
	 * @var array
23
	 */
24
	protected $archive_pages = array(
25
		'author',
26
		'category',
27
		'taxonomy',
28
		'date',
29
		'tag',
30
		'archive',
31
		'post-type-archive',
32
	);
33
34
	/**
35
	 * Create a list of actions for the specified page
36
	 *
37
	 * @param string $page Page to get Actions for.
38
	 *
39
	 * @return array of actions
40
	 */
41
	public function get_page_actions( $page ) {
42
		/**
43
		 * Actions to be executed
44
		 */
45
		$actions = array(
46
			'always'
47
		);
48
49
		// Singular = single item (post, attachment, page, etc).
50
		if ( is_singular() ) {
51
			$actions[] = 'singular';
52
		}
53
54
		// Paged page (pagination).
55
		if ( is_paged() ) {
56
			$actions[] = 'paged';
57
		}
58
59
		if ( is_sticky() ) {
60
			$actions[] = 'sticky';
61
		}
62
63
		if ( is_user_logged_in() ) {
64
			$actions[] = 'logged-in';
65
			$actions[] = 'loggedin';
66
		}
67
68
		if ( in_array( $page, $this->archive_pages, true ) || 'home' === $page ) {
69
			$actions[] = 'archived';
70
		}
71
72
		// Add page action.
73
		$actions[] = $page;
74
75
		// Apply filters.
76
		$actions = Stencil_Environment::filter( 'actions-' . $page, $actions );
77
		$actions = Stencil_Environment::filter( 'actions', $actions );
78
79
		return $actions;
80
	}
81
82
	/**
83
	 * Determine which view is available for loading
84
	 *
85
	 * @param string $page Page to get hierarchy of.
86
	 *
87
	 * @return array
88
	 * @throws Exception When handler doesn't return expected type.
89
	 */
90
	public function get_view_hierarchy( $page ) {
91
92
		/**
93
		 * Get the possible views for specified page:
94
		 */
95
		$options = Stencil_Handler_Factory::get_hierarchy_handler( $page );
96
		if ( ! is_array( $options ) && ! ( $options instanceof Traversable ) ) {
97
			throw new Exception( 'Expected array got ' . gettype( $options ) );
98
		}
99
100
		if ( is_array( $options ) ) {
101
			$options = new ArrayIterator( $options );
102
		}
103
104
		/**
105
		 * Add archive option for archive pages:
106
		 */
107
		if ( 'archived' !== $page && in_array( $page, $this->archive_pages, true ) ) {
108
			$this->add_to_options( 'archived', $options );
109
		}
110
111
		/**
112
		 * Add paged option for paged pages:
113
		 */
114
		if ( 'paged' !== $page && is_paged() ) {
115
			$this->add_to_options( 'paged', $options );
116
		}
117
118
		/**
119
		 * Convert to array for filtering and return ouput
120
		 */
121
		$options = iterator_to_array( $options );
122
123
		// Apply filter.
124
		$options = Stencil_Environment::filter( 'views-' . $page, $options );
125
126
		return $options;
127
	}
128
129
	/**
130
	 * Add additional options to the existing array
131
	 *
132
	 * @param string         $type Type to get options for.
133
	 * @param array|Iterator $options Reference. Existing options.
134
	 *
135
	 * @throws Exception
136
	 */
137
	private function add_to_options( $type, & $options ) {
138
		$additive_options = $this->get_view_hierarchy( $type );
139
140
		if ( array() !== $additive_options ) {
141
			if ( ! ( $options instanceof AppendIterator ) ) {
142
				$all_options = new AppendIterator();
143
				$all_options->append( $options );
144
			} else {
145
				$all_options = $options;
146
			}
147
148
			$all_options->append( new ArrayIterator( $additive_options ) );
149
150
			$options = $all_options;
151
		}
152
	}
153
}
154