Completed
Push — master ( e77a87...e3d3eb )
by Patrick
03:02
created

Provider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 91
rs 10
wmc 13
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethodByName() 0 12 3
A addFromEach() 0 18 4
B callOnEach() 0 26 6
1
<?php
2
3
class Provider extends Singleton
4
{
5
    /** The methods loaded by the provider */
6
    protected $methods;
7
8
    /**
9
     * Get the method object corresponding to the name
10
     *
11
     * @param string $methodName The class name of the method to get the instance for
12
     *
13
     * @return boolean|stdClass The specified method class instance or false if it is not loaded
14
     */
15
    public function getMethodByName($methodName)
16
    {
17
        $count = count($this->methods);
18
        for($i = 0; $i < $count; $i++)
19
        {
20
            if(strcasecmp(get_class($this->methods[$i]), $methodName) === 0)
21
            {
22
                return $this->methods[$i];
23
            }
24
        }
25
        return false;
26
    }
27
28
    /**
29
     * Calls the indicated function on each method and add the result
30
     *
31
     * @param string $functionName The function to call
32
     * @param string $checkField A field to check if it is set a certain way before calling the function
33
     * @param mixed $checkValue The value that field should be set to to not call the function
34
     *
35
     * @return integer The added returnValue
36
     */
37
    protected function addFromEach($functionName, $checkField = false, $checkValue = false)
38
    {
39
        $retCount = 0;
40
        $count = count($this->methods);
41
        for($i = 0; $i < $count; $i++)
42
        {
43
            if($checkField !== false)
44
            {
45
                if($this->methods[$i]->{$checkField} === $checkValue)
46
                {
47
                    continue;
48
                }
49
            }
50
            $res = call_user_func(array($this->methods[$i], $functionName));
51
            $retCount += $res;
52
        }
53
        return $retCount;
54
    }
55
56
    /**
57
     * Calls the indicated function on each method
58
     *
59
     * @param string $functionName The function to call
60
     * @param array $args The arguments for the function
61
     * @param string $checkField A field to check if it is set a certain way before calling the function
62
     * @param mixed $checkValue The value that field should be set to to not call the function
63
     * @param callable $resFunction Function to call on the result, otherwise the function will return on the first non-false result
64
     *
65
     * @return Auth\Group|Auth\User|false The merged returnValue
66
     */
67
    protected function callOnEach($functionName, $args, $checkField = false, $checkValue = false, $resFunction = null)
68
    {
69
        $ret = false;
70
        $count = count($this->methods);
71
        for($i = 0; $i < $count; $i++)
72
        {
73
            if($checkField !== false)
74
            {
75
                if($this->methods[$i]->{$checkField} === $checkValue)
76
                {
77
                    continue;
78
                }
79
            }
80
            $res = call_user_func_array(array($this->methods[$i], $functionName), $args);
81
            if($resFunction !== null)
82
            {
83
                call_user_func($resFunction, $ret, $res);
84
                continue;
85
            }
86
            if($res !== false)
87
            {
88
                return $res;
89
            }
90
        }
91
        return $ret;
92
    }
93
}
94
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
95