Filters   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 80.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 147
rs 10
c 1
b 0
f 0
ccs 50
cts 62
cp 0.8065
wmc 25
lcom 2
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A registerFilterObject() 0 5 1
A addFilter() 0 14 4
C removeFilter() 0 24 7
A removeAllFilters() 0 15 3
A applyFilter() 0 12 3
B execute() 0 19 5
1
<?php
2
3
namespace Redbox\Hooks;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Redbox\Hooks\Annotations\FilterAnnotationHandler;
7
8
class Filters
9
{
10
    /**
11
     * @var array
12
     */
13
    static private $filters = [];
14
15
    /**
16
     * @var Annotations\FilterAnnotationHandler
17
     */
18
    static private $annotation_handler = null;
19
20
    /**
21
     * Initialize the Annotation Handler for
22
     * dealing with filter objects.
23
     */
24 4
    public static function init()
25
    {
26 4
        if (! self::$annotation_handler) {
27 4
            $reader = new AnnotationReader();
28 4
            self::$annotation_handler = new FilterAnnotationHandler($reader);
29 2
        }
30 4
    }
31
32
    /**
33
     * @param $object
34
     */
35 4
    public static function registerFilterObject($object)
36
    {
37 4
        self::init();
38 4
        self::$annotation_handler->read($object);
39 4
    }
40
41
    /**
42
     * @param string $tag
43
     * @param string $callback
44
     * @param int $priority
45
     * @return bool
46
     */
47 32
    public static function addFilter($tag = '', $callback = '', $priority = 10)
48
    {
49 32
        if (empty($tag) || empty($callback)) {
50 8
            return false;
51
        }
52
53 24
        if (isset(self::$filters[$tag]) === false) {
54 24
            self::$filters[$tag] = new Hook($tag);
55 12
        }
56
57 24
        self::$filters[$tag]->addHook($priority, $callback);
58
59 24
        return true;
60
    }
61
62
    /**
63
     * @param string $tag
64
     * @param string $callback
65
     * @return bool
66
     */
67 12
    public static function removeFilter($tag = '', $callback = '')
68
    {
69 12
        if (empty($tag) || empty($callback)) {
70 8
            return false;
71
        }
72
73 4
        if (isset(self::$filters[$tag]) === true) {
74
            $found = false;
75
            $hooks = self::$filters[$tag]->getHooks();
76
77
            foreach ($hooks as $priority => $callbacks) {
78
                foreach ($callbacks as $entry) {
79
                    if ($entry['callback'] == $callback) {
80
                        self::$filters[$tag]->removeCallbackWithPriority($priority, $callback);
81
                        $found = true;
82
                    }
83
                }
84
            }
85
86
            return $found;
87 4
        }
88
89
        return false;
90
    }
91
92
    /**
93
     * @param string $tag
94 24
     * @return bool
95
     */
96 24
    public static function removeAllFilters($tag = '')
97 4
    {
98
        if (empty($tag)) {
99
            return false;
100 20
        }
101 20
102 20
        if (isset(self::$filters[$tag])) {
103 20
            self::$filters[$tag]->removeAllHooks();
104
            unset(self::$filters[$tag]);
105
106
            return true;
107
        }
108
109
        return false;
110
    }
111
112
    /**
113
     * @param string $filter
114 28
     * @param string $value
115
     * @return string
116 28
     */
117 4
    public static function applyFilter($filter = '', $value = '')
118 4
    {
119 2
        if (is_array($filter)) {
120 2
            foreach ($filter as $single) {
121 24
                $value = self::execute($single, $value);
122
            }
123 28
        } else {
124
            $value = self::execute($filter, $value);
125
        }
126
127
        return $value;
128
    }
129
130
    /**
131 28
     * @param $tag
132
     * @param string $value
133 28
     * @return string
134 4
     */
135
    private static function execute($tag, $value = '')
136
    {
137 24
        if (isset(self::$filters[$tag]) === false) {
138
            return $value;
139 24
        }
140
141 24
        $hooks = self::$filters[$tag]->getHooks();
142 24
143 24
        foreach ($hooks as $priority => $callbacks) {
144 12
            do {
145 24
                $entry = current($callbacks);
146 12
                if (is_callable($entry['callback'])) {
147
                    $value = call_user_func($entry['callback'], $value);
148 24
                }
149
            } while (next($callbacks));
150
        }
151
152
        return $value;
153
    }
154
}
155