Passed
Pull Request — master (#17)
by Glynn
01:51
created

BladeOne::get_auth_data()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 16
nop 0
dl 0
loc 13
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types=1 );
4
5
/**
6
 * The BladeOne Module for Perique.
7
 *
8
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
 *
20
 * @author Glynn Quelch <[email protected]>
21
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
22
 * @package PinkCrab\BladeOne_Engine
23
 */
24
25
namespace PinkCrab\BladeOne;
26
27
use PinkCrab\Loader\Hook_Loader;
28
use PinkCrab\BladeOne\BladeOne_Engine;
29
use PinkCrab\Perique\Interfaces\Module;
30
use PinkCrab\BladeOne\PinkCrab_BladeOne;
31
use PinkCrab\Perique\Services\View\View;
32
use PinkCrab\Perique\Interfaces\Renderable;
33
use PinkCrab\Perique\Application\App_Config;
34
use PinkCrab\Perique\Interfaces\DI_Container;
35
36
class BladeOne implements Module {
37
38
	private ?string $template_path = null;
39
	private ?string $compiled_path = null;
40
	private int $mode              = PinkCrab_BladeOne::MODE_AUTO;
41
	/** @var ?\Closure(BladeOne_Engine):BladeOne_Engine */
42
	private $config = null;
43
44
	/**
45
	 * Set the template path.
46
	 *
47
	 * @param string $template_path
48
	 * @return self
49
	 */
50
	public function template_path( string $template_path ): self {
51
		$this->template_path = $template_path;
52
		return $this;
53
	}
54
55
	/**
56
	 * Set the compiled path.
57
	 *
58
	 * @param string $compiled_path
59
	 * @return self
60
	 */
61
	public function compiled_path( string $compiled_path ): self {
62
		$this->compiled_path = $compiled_path;
63
		return $this;
64
	}
65
66
	/**
67
	 * Set the mode.
68
	 *
69
	 * @param int $mode
70
	 * @return self
71
	 */
72
	public function mode( int $mode ): self {
73
		$this->mode = $mode;
74
		return $this;
75
	}
76
77
	/**
78
	 * Provider config.
79
	 *
80
	 * @param \Closure(BladeOne_Engine):BladeOne_Engine $config
81
	 * @return self
82
	 */
83
	public function config( \Closure $config ): self {
84
		$this->config = $config;
85
		return $this;
86
	}
87
88
	/**
89
	 * Creates the shared instance of the module and defines the
90
	 * DI Rules to use the BladeOne_Engine.
91
	 *
92
	 * @pram App_Config $config
93
	 * @pram Hook_Loader $loader
94
	 * @pram DI_Container $di_container
95
	 * @return void
96
	 */
97
	public function pre_boot( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
98
99
		$wp_upload_dir = wp_upload_dir();
100
		$compiled_path = $this->compiled_path ?? sprintf( '%1$s%2$sblade-cache', $wp_upload_dir['basedir'], \DIRECTORY_SEPARATOR ); // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
101
		$instance      = new PinkCrab_BladeOne(
102
			$this->template_path ?? $config->path( 'view' ),
103
			$compiled_path,
104
			$this->mode
105
		);
106
107
		$instance->setAuth( ...$this->get_auth_data() );
0 ignored issues
show
Bug introduced by
It seems like $this->get_auth_data() can also be of type array and array; however, parameter $user of eftec\bladeone\BladeOne::setAuth() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
		$instance->setAuth( /** @scrutinizer ignore-type */ ...$this->get_auth_data() );
Loading history...
108
109
		// Create the compiled path if it does not exist.
110
		if ( ! \file_exists( $compiled_path ) ) {
111
			mkdir( $compiled_path );
112
		}
113
114
		$di_container->addRule(
115
			BladeOne_Engine::class,
116
			array(
117
				'constructParams' => array(
118
					$instance,
119
				),
120
				'call'            => array(
121
					array( 'allow_pipe', array() ),
122
				),
123
				'shared'          => true,
124
			)
125
		);
126
127
		$di_container->addRule(
128
			Renderable::class,
129
			array(
130
				'instanceOf' => BladeOne_Engine::class,
131
				'shared'     => true,
132
			)
133
		);
134
135
		$di_container->addRule(
136
			View::class,
137
			array(
138
				'substitutions' => array(
139
					Renderable::class => BladeOne_Engine::class,
140
				),
141
				'shared'        => true,
142
			)
143
		);
144
145
	}
146
147
	/**
148
	 * Gets the current logged in user details
149
	 *
150
	 * @return array{0:string, 1:string, 2:string[]}
151
	 */
152
	private function get_auth_data(): array {
153
154
		// @codeCoverageIgnoreStart
155
		if ( ! function_exists( 'wp_get_current_user' ) ) {
156
			require_once ABSPATH . 'wp-includes/pluggable.php';
157
		}
158
		// @codeCoverageIgnoreEnd
159
160
		$user = \wp_get_current_user();
161
		return array(
162
			0 !== $user->ID ? $user->user_login : '',
163
			0 !== $user->ID ? $user->roles[0] : '',
164
			0 !== $user->ID ? array_keys( array_filter( $user->allcaps ) ) : array(),
165
		);
166
	}
167
168
	/**
169
	 * Allows for the config to be passed to the provider, before its used.
170
	 *
171
	 * @pram App_Config $config
172
	 * @pram Hook_Loader $loader
173
	 * @pram DI_Container $di_container
174
	 * @return void
175
	 */
176
	public function pre_register( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
177
178
		$provider = $di_container->create( BladeOne_Engine::class );
179
180
		// If dont have an instance of BladeOne_Engine, return.
181
		if ( ! $provider instanceof BladeOne_Engine ) {
182
			throw new \RuntimeException( 'Unable to create BladeOne_Engine instance to configure instance' );
183
		}
184
185
		// Pass the config to the provider, if set.
186
		if ( ! is_null( $this->config ) ) {
187
			\call_user_func( $this->config, $provider );
188
		}
189
190
	}
191
192
	## Unused methods
193
194
195
	/** @inheritDoc */
196
	public function post_register( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void {} // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
197
198
	/** @inheritDoc */
199
	public function get_middleware(): ?string {
200
		return null;
201
	}
202
}
203