Passed
Push — master ( f942d9...146528 )
by Atanas
04:42
created

ApplicationTrait::__callStatic()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.9666
cc 3
nc 3
nop 2
crap 3.009
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2017-2019 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Application;
11
12
use BadMethodCallException;
13
use WPEmerge\Exceptions\ConfigurationException;
14
15
/**
16
 * Provides static access to an Application instance.
17
 *
18
 * @mixin ApplicationMixin
19
 */
20
trait ApplicationTrait {
21
	/**
22
	 * Application instance.
23
	 *
24
	 * @var Application|null
25
	 */
26
	public static $instance = null;
27
28
	/**
29
	 * Make and assign a new application instance.
30
	 *
31
	 * @return Application
32
	 */
33 3
	public static function make() {
34 3
		static::setApplication( Application::make() );
35
36 3
		return static::getApplication();
37
	}
38
39
	/**
40
	 * Get the Application instance.
41
	 *
42
	 * @codeCoverageIgnore
43
	 * @return Application|null
44
	 */
45
	public static function getApplication() {
46
		return static::$instance;
47
	}
48
49
	/**
50
	 * Set the Application instance.
51
	 *
52
	 * @codeCoverageIgnore
53
	 * @param  Application|null $application
54
	 * @return void
55
	 */
56
	public static function setApplication( $application ) {
57
		static::$instance = $application;
58
	}
59
60
	/**
61
	 * Invoke any matching instance method for the static method being called.
62
	 *
63
	 * @param  string $method
64
	 * @param  array  $parameters
65
	 * @return mixed
66
	 */
67 4
	public static function __callStatic( $method, $parameters ) {
68 4
		$application = static::getApplication();
69 4
		$callable = [$application, $method];
70
71 4
		if ( ! $application ) {
72 1
			throw new ConfigurationException(
73 1
				'Application instance not created in ' . static::class . '. ' .
74 1
				'Did you miss to call ' . static::class . '::make()?'
75
			);
76
		}
77
78 3
		if ( ! is_callable( $callable ) ) {
79
			throw new BadMethodCallException( 'Method ' . get_class( $application ) . '::' . $method . '() does not exist.' );
80
		}
81
82 3
		return call_user_func_array( $callable, $parameters );
83
	}
84
}
85