Passed
Push — master ( a8bd8b...f2f9a5 )
by Atanas
02:01
created

ViewService::getGlobals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace WPEmerge\View;
4
5
use Closure;
6
use ViewEngine;
7
use WPEmerge\Helpers\Handler;
8
use WPEmerge\Helpers\Mixed;
9
use WPEmerge\Helpers\Path;
10
use WPEmerge\Support\Arr;
11
12
/**
13
 * Provide general view-related functionality
14
 */
15
class ViewService {
16
	/**
17
	 * Global variables
18
	 *
19
	 * @var array
20
	 */
21
	protected $globals = [];
22
23
	/**
24
	 * View composers
25
	 *
26
	 * @var array
27
	 */
28
	protected $composers = [];
29
30
	/**
31
	 * Get global variables
32
	 *
33
	 * @return array
34
	 */
35 2
	public function getGlobals() {
36 2
		return $this->globals;
37
	}
38
39
	/**
40
	 * Set a global variable
41
	 *
42
	 * @param  string $key
43
	 * @param  mixed  $value
44
	 * @return void
45
	 */
46 2
	public function addGlobal( $key, $value ) {
47 2
		$this->globals[ $key ] = $value;
48 2
	}
49
50
	/**
51
	 * Set an array of global variables
52
	 *
53
	 * @param  array $globals
54
	 * @return void
55
	 */
56 1
	public function addGlobals( $globals ) {
57 1
		foreach ( $globals as $key => $value ) {
58 1
			$this->addGlobal( $key, $value );
59 1
		}
60 1
	}
61
62
	/**
63
	 * Get view composer
64
	 *
65
	 * @param  string    $view
66
	 * @return Handler[]
67
	 */
68 2
	public function getComposersForView( $view ) {
69 2
		$view = ViewEngine::canonical( $view );
70
71 2
		$composers = [];
72
73 2
		foreach ( $this->composers as $composer ) {
74 2
			if ( in_array( $view, $composer['views'] ) ) {
75 2
				$composers[] = $composer['composer'];
76 2
			}
77 2
		}
78
79 2
		return $composers;
80
	}
81
82
	/**
83
	 * Add view composer
84
	 *
85
	 * @param string|string[] $views
86
	 * @param string|Closure  $composer
87
	 * @return void
88
	 */
89
	public function addComposer( $views, $composer ) {
90 2
		$views = array_map( function( $view ) {
91 2
			return ViewEngine::canonical( $view );
92 2
		}, Mixed::toArray( $views ) );
93 2
		$handler = new Handler( $composer, 'compose' );
94
95 2
		$this->composers[] = [
96 2
			'views' => $views,
97 2
			'composer' => $handler,
98
		];
99 2
	}
100
101
	/**
102
	 * Get the composed context for a view.
103
	 * Passes all arguments to the composer.
104
	 *
105
	 * @param  ViewInterface $view
106
	 * @return void
107
	 */
108 1
	public function compose( ViewInterface $view ) {
109 1
		$context = [];
0 ignored issues
show
Unused Code introduced by
$context 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...
110 1
		$composers = $this->getComposersForView( $view->getName() );
111
112 1
		foreach ( $composers as $composer ) {
113 1
			$composer->execute( $view );
114 1
		}
115 1
	}
116
}
117