Completed
Push — 3.x ( 0a54fb...a7763e )
by Paul
10:06 queued 08:04
created

Secure::https()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Router\Rule;
10
11
use Aura\Router\Route;
12
use Psr\Http\Message\ServerRequestInterface;
13
14
/**
15
 *
16
 * A rule for HTTPS/SSL/TLS.
17
 *
18
 * @package Aura.Router
19
 *
20
 */
21
class Secure implements RuleInterface
22
{
23
    /**
24
     *
25
     * Checks that the Route `$secure` matches the corresponding server values.
26
     *
27
     * @param ServerRequestInterface $request The HTTP request.
28
     *
29
     * @param Route $route The route.
30
     *
31
     * @return bool True on success, false on failure.
32
     *
33
     */
34
    public function __invoke(ServerRequestInterface $request, Route $route)
35
    {
36
        if ($route->secure === null) {
37
            return true;
38
        }
39
40
        $server = $request->getServerParams();
41
        $secure = $this->https($server) || $this->port443($server);
42
        return $route->secure == $secure;
43
    }
44
45
    /**
46
     *
47
     * Is HTTPS on?
48
     *
49
     * @param array $server The server params.
50
     *
51
     * @return bool
52
     *
53
     */
54
    protected function https($server)
55
    {
56
        return isset($server['HTTPS'])
57
            && $server['HTTPS'] == 'on';
58
    }
59
60
61
    /**
62
     *
63
     * Is the request on port 443?
64
     *
65
     * @param array $server The server params.
66
     *
67
     * @return bool
68
     *
69
     */
70
    protected function port443($server)
71
    {
72
        return isset($server['SERVER_PORT'])
73
            && $server['SERVER_PORT'] == 443;
74
    }
75
}
76