Completed
Push — master ( dfbd66...160359 )
by Vitaly
05:33 queued 01:53
created

QueryHandler::callHandlers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 9.4286
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
namespace samsonframework\orm;
3
4
/**
5
 * Class for saving external query handlers
6
 * @author Vitaly Iegorov <[email protected]>
7
 * @version 2.0
8
 */
9
class QueryHandler
10
{
11
    /** @var callback[] Collection of external query handlers(callbacks) */
12
    private $handlers = array();
13
14
    /** @var array Collection of external query handlers(callbacks) additional parameters */
15
    private $params = array();
16
17
    /**
18
     * Add extenal query handler
19
     * @param callable $callable External handler
20
     * @return self Chaining
21
     */
22
    public function handler($callable)
23
    {
24
        // If normal handler is passed
25
        if (is_callable($callable)) {
26
            // Add handler
27
            $this->handlers[] = $callable;
28
29
            // Get passed arguments
30
            $args = func_get_args();
31
32
            // Remove first argument
33
            array_shift($args);
34
35
            // Add handler parameters stack
36
            $this->params[] = &$args;
37
        } else {
38
            //e('Cannot set Query handler - function(##) does not exists', E_SAMSON_ACTIVERECORD_ERROR, $callable);
39
        }
40
41
        return $this;
42
    }
43
44
    /** Execute all available external query handlers */
45
    protected function callHandlers()
46
    {
47
        // Iterate handlers and run them
48
        foreach ($this->handlers as $i => $handler) {
49
            // Create handler params array with first parameter pointing to this query object
50
            $params = array(& $this);
51
52
            // Combine params with existing ones in one array
53
            $params = array_merge($params, $this->params[$i]);
54
55
            // Execute handler
56
            call_user_func_array($handler, $params);
57
        }
58
    }
59
}