Completed
Push — development ( 143149...212b7e )
by Alexander
03:21
created

LazyCalls::invokeLazyCalls()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 9
cts 9
cp 1
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @file      LazyCalls.php
4
 *
5
 * PHP version 5.4+
6
 *
7
 * @author    Yancharuk Alexander <alex at itvault dot info>
8
 * @copyright © 2012-2015 Alexander Yancharuk <alex at itvault at info>
9
 * @date      2015-12-26 13:38
10
 * @license   The BSD 3-Clause License
11
 *            <http://opensource.org/licenses/BSD-3-Clause>
12
 */
13
14
namespace Traits;
15
16
use Exception;
17
use Veles\Traits\SingletonInstance;
18
19
trait LazyCalls
20
{
21
	/** @var  array */
22
	protected static $calls = [];
23
24
	use SingletonInstance;
25
26
	/**
27
	 * Lazy calls invocation
28
	 */
29 2
	protected static function invokeLazyCalls()
30
	{
31 2
		foreach (static::$calls as $call) {
32 2
			call_user_func_array(
33 2
				[static::$instance->getDriver(), $call['method']],
34 2
				$call['arguments']
35 2
			);
36 2
		}
37 2
		static::$calls = [];
38 2
	}
39
40
	/**
41
	 * @return $this
42
	 */
43 2
	public static function instance()
44
	{
45 2
		if (null === static::$instance) {
46 2
			$class = get_called_class();
47
48 2
			static::$instance = new $class;
49 2
		}
50
51 2
		if ([] !== static::$calls) {
52 2
			static::invokeLazyCalls();
53 2
		}
54
55 2
		return static::$instance;
56
	}
57
58
	/**
59
	 * Collect calls which will be invoked during first real query
60
	 *
61
	 * @param $method
62
	 * @param $arguments
63
	 *
64
	 * @return mixed
65
	 * @throws Exception
66
	 */
67 2
	public function __call($method, $arguments)
68
	{
69 2
		if (!method_exists($this->getDriver(), $method)) {
0 ignored issues
show
Documentation Bug introduced by
The method getDriver does not exist on object<Traits\LazyCalls>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
70 1
			throw new Exception('Calling non existent method!');
71
		}
72
73 1
		static::addCall($method, $arguments);
74 1
	}
75
76
	/**
77
	 * Save calls for future invocation
78
	 *
79
	 * @param string $method Method name that should be called
80
	 * @param array $arguments Method arguments
81
	 */
82 2
	public static function addCall($method, array $arguments = [])
83
	{
84 2
		static::$calls[] = [
85 2
			'method'    => $method,
86
			'arguments' => $arguments
87 2
		];
88 2
	}
89
}
90