Redirect   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 18 3
1
<?php
2
3
/*
4
 * janitor (http://juliangut.com/janitor).
5
 * Effortless maintenance management.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/janitor
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
namespace Janitor\Handler;
13
14
use Janitor\Watcher\ScheduledWatcherInterface;
15
use Janitor\Watcher\WatcherInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
19
/**
20
 * Redirect maintenance handler.
21
 */
22
class Redirect implements HandlerInterface
23
{
24
    private $location;
25
26
    /**
27
     * Redirect constructor.
28
     *
29
     * @param string $location
30
     */
31
    public function __construct($location)
32
    {
33
        $this->location = (string) $location;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, WatcherInterface $watcher)
40
    {
41
        if ($watcher instanceof ScheduledWatcherInterface) {
42
            $response = $response->withHeader('Expires', $watcher->getEnd()->format('D, d M Y H:i:s e'));
43
        } else {
44
            $response = $response->withHeader('Cache-Control', 'max-age=0')
45
                ->withHeader('Cache-Control', 'no-cache, must-revalidate')
46
                ->withHeader('Pragma', 'no-cache');
47
        }
48
49
        $location = $this->location;
50
        if (!preg_match('/^https?:\/\//', $location)) {
51
            $location = (string) $request->getUri()->withPath($location);
52
        }
53
54
        return $response->withStatus(302)
55
            ->withHeader('Location', $location);
56
    }
57
}
58