Completed
Push — master ( ba7c5d...c3b626 )
by Julián
02:08
created

Janitor::setAttributeName()   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
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Effortless maintenance management (http://juliangut.com/janitor)
4
 *
5
 * @link https://github.com/juliangut/janitor for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/janitor/blob/master/LICENSE
8
 */
9
10
namespace Janitor;
11
12
use Janitor\Handler\Render as RenderHandler;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
class Janitor
17
{
18
    /**
19
     * List of watchers.
20
     *
21
     * @var array
22
     */
23
    protected $watchers = [];
24
25
    /**
26
     * List of excluders.
27
     *
28
     * @var array
29
     */
30
    protected $excluders = [];
31
32
    /**
33
     * Resolve handler.
34
     *
35
     * @var callable
36
     */
37
    protected $handler;
38
39
    /**
40
     * Request attribute name to store currently active watcher.
41
     *
42
     * @var string
43
     */
44
    protected $attributeName;
45
46
    /**
47
     * @param array         $watchers
48
     * @param array         $excluders
49
     * @param callable|null $handler
50
     * @param string        $attributeName
51
     */
52
    public function __construct(
53
        array $watchers = [],
54
        array $excluders = [],
55
        callable $handler = null,
56
        $attributeName = 'active_watcher'
57
    ) {
58
        foreach ($watchers as $watcher) {
59
            $this->addWatcher($watcher);
60
        }
61
62
        foreach ($excluders as $excluder) {
63
            $this->addExcluder($excluder);
64
        }
65
66
        $this->handler = $handler;
67
        $this->attributeName = $attributeName;
68
    }
69
70
    /**
71
     * Add maintenance watcher.
72
     *
73
     * @param \Janitor\Watcher $watcher
74
     */
75
    public function addWatcher(Watcher $watcher)
76
    {
77
        $this->watchers[] = $watcher;
78
79
        return $this;
80
    }
81
82
    /**
83
     * Add excluder condition.
84
     *
85
     * @param \Janitor\Excluder $excluder
86
     */
87
    public function addExcluder(Excluder $excluder)
88
    {
89
        $this->excluders[] = $excluder;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Set handler.
96
     *
97
     * @param callable $handler
98
     */
99
    public function setHandler(callable $handler)
100
    {
101
        $this->handler = $handler;
102
103
        return $this;
104
    }
105
106
    /**
107
     * Set request attribute name to store active watcher.
108
     *
109
     * @param string $attributeName
110
     */
111
    public function setAttributeName($attributeName)
112
    {
113
        $this->attributeName = $attributeName;
114
115
        return $this;
116
    }
117
118
    /**
119
     * Retrieve request attribute name storing active watcher.
120
     *
121
     * @return string
122
     */
123
    public function getAttributeName()
124
    {
125
        return $this->attributeName;
126
    }
127
128
    /**
129
     * Get next scheduled time spans.
130
     *
131
     * Returns an array of ['start' => \DateTime, 'end' => \DateTime]
132
     *
133
     * @param int $count
134
     *
135
     * @return array
136
     */
137
    public function getScheduledTimes($count = 5)
138
    {
139
        $scheduledTimes = [];
140
141
        foreach ($this->watchers as $watcher) {
142
            if ($watcher instanceof ScheduledWatcher && $watcher->isScheduled()) {
143
                $scheduledTimes = array_merge($scheduledTimes, $watcher->getScheduledTimes($count));
144
            }
145
        }
146
147
        usort(
148
            $scheduledTimes,
149
            function ($time1, $time2) {
150
                if ($time1['start'] == $time2['start']) {
151
                    return 0;
152
                }
153
154
                return $time1['start'] < $time2['start'] ? -1 : 1;
155
            }
156
        );
157
158
        return array_slice($scheduledTimes, 0, $count);
159
    }
160
161
    /**
162
     * Run middleware.
163
     *
164
     * @param \Psr\Http\Message\ServerRequestInterface $request
165
     * @param \Psr\Http\Message\ResponseInterface      $response
166
     * @param callable                                 $next
167
     *
168
     * @return \Psr\Http\Message\ResponseInterface
169
     */
170
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
171
    {
172
        $activeWatcher = $this->getActiveWatcher();
173
174
        if ($activeWatcher instanceof Watcher) {
175
            if (!$this->isExcluded($request)) {
176
                return call_user_func_array($this->getHandler(), [$request, $response, $activeWatcher]);
177
            }
178
179
            $request = $request->withAttribute($this->getAttributeName(), $activeWatcher);
180
        }
181
182
        return $next($request, $response);
183
    }
184
185
    /**
186
     * Get currenlty active watcher.
187
     *
188
     * @return \Janitor\Watcher|null
189
     */
190
    protected function getActiveWatcher()
191
    {
192
        foreach ($this->watchers as $watcher) {
193
            if ($watcher->isActive()) {
194
                return $watcher;
195
            }
196
        }
197
198
        return null;
199
    }
200
201
    /**
202
     * Whether excluding conditions are met.
203
     *
204
     * @param \Psr\Http\Message\ServerRequestInterface $request
205
     *
206
     * @return bool
207
     */
208
    protected function isExcluded(ServerRequestInterface $request)
209
    {
210
        foreach ($this->excluders as $excluder) {
211
            if ($excluder->isExcluded($request)) {
212
                return true;
213
            }
214
        }
215
216
        return false;
217
    }
218
219
    /**
220
     * Retrieve handler.
221
     *
222
     * @return callable
223
     */
224
    protected function getHandler()
225
    {
226
        if (!is_callable($this->handler)) {
227
            $this->handler = new RenderHandler;
228
        }
229
230
        return $this->handler;
231
    }
232
}
233