Completed
Push — master ( ca910a...8ffcad )
by Evan
02:52
created

Hook::onlyXtimes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Silk\Event;
4
5
use Silk\Support\Callback;
6
7
class Hook
8
{
9
    protected $handle;
10
11
    protected $callback;
12
13
    protected $callbackParamCount;
14
15
    protected $priority;
16
17
    protected $iterations;
18
19
    protected $maxIterations;
20
21
22
    /**
23
     * Create a new Hook instance
24
     *
25
     * @param  string $handle action or filter handle
26
     * @param  int    $priority
27
     * @return static         instance
28
     */
29
    public static function on($handle, $priority = 10)
30
    {
31
        return new static($handle, $priority);
32
    }
33
34
    /**
35
     * Create a new Hook instance
36
     * @param  string $handle action or filter handle
37
     * @param  int    $priority
38
     */
39
    public function __construct($handle, $priority = 10)
40
    {
41
        $this->handle = $handle;
42
        $this->priority = $priority;
43
    }
44
45
    /**
46
     * [setCallback description]
47
     * @param callable $callback [description]
48
     */
49
    public function setCallback(callable $callback)
50
    {
51
        $this->callback = new Callback($callback);
52
        $this->callbackParamCount = $this->callback->reflect()->getNumberOfParameters();
53
54
        return $this;
55
    }
56
57
    /**
58
     * Set the hook in WP
59
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
60
     */
61
    public function listen()
62
    {
63
        add_filter($this->handle, [$this, 'mediateCallback'], $this->priority, 100);
64
65
        return $this;
66
    }
67
68
    /**
69
     * Unset the hook in WP
70
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
71
     */
72
    public function remove()
73
    {
74
        remove_filter($this->handle, [$this, 'mediateCallback'], $this->priority);
75
76
        return $this;
77
    }
78
79
    /**
80
     * [mediateCallback description]
81
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
82
     */
83
    public function mediateCallback($given = null)
84
    {
85
        if (! $this->shouldInvoke(func_get_args())) {
86
            return $given;
87
        }
88
89
        return $this->invokeCallback(func_get_args());
90
    }
91
92
    public function shouldInvoke(array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        if ($this->hasExceededIterations()) {
95
            return false;
96
        }
97
98
        return true;
99
    }
100
101
    /**
102
     * [invokeCallback description]
103
     * @param  [type] $arguments [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
104
     * @return [type]            [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
105
     */
106
    protected function invokeCallback($arguments)
107
    {
108
        $arguments = array_slice($arguments, 0, $this->callbackParamCount ?: null);
109
110
        $this->iterations++;
111
112
        return $this->callback->callArray($arguments);
113
    }
114
115
    /**
116
     * [once description]
117
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
118
     */
119
    public function once()
120
    {
121
        $this->onlyXtimes(1);
122
123
        return $this;
124
    }
125
126
    /**
127
     * [onlyXtimes description]
128
     * @param  [type] $times [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
129
     * @return [type]        [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
130
     */
131
    public function onlyXtimes($times)
132
    {
133
        $this->maxIterations = (int) $times;
134
135
        return $this;
136
    }
137
138
    /**
139
     * Prevent the callback from being triggered again
140
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
141
     */
142
    public function bypass()
143
    {
144
        $this->onlyXtimes(0);
145
146
        return $this;
147
    }
148
149
    /**
150
     * [withPriority description]
151
     * @param  [type] $priority [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
152
     * @return [type]           [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
153
     */
154
    public function withPriority($priority)
155
    {
156
        $this->remove();
157
158
        $this->priority = $priority;
159
160
        $this->listen();
161
162
        return $this;
163
    }
164
165
    /**
166
     * [hasExceededIterations description]
167
     * @return boolean [description]
168
     */
169
    protected function hasExceededIterations()
170
    {
171
        return ($this->maxIterations > -1) && ($this->iterations >= $this->maxIterations);
172
    }
173
}
174