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

QueryHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 4
c 3
b 0
f 2
lcom 1
cbo 0
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handler() 0 21 2
A callHandlers() 0 14 2
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
}