Completed
Push — master ( 0fba1e...5a3626 )
by Oscar
02:23
created

Https::includeSubdomains()   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
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Middleware to redirect to https protocol.
11
 */
12
class Https
13
{
14
    use Utils\RedirectTrait;
15
16
    const HEADER = 'Strict-Transport-Security';
17
18
    /**
19
     * @param int One year by default
20
     */
21
    private $maxAge = 31536000;
22
23
    /**
24
     * @param bool Whether include subdomains
25
     */
26
    private $includeSubdomains = false;
27
28
    /**
29
     * Set basic config.
30
     */
31
    public function __construct()
32
    {
33
        $this->redirect(301);
34
    }
35
36
    /**
37
     * Configure the max-age HSTS in seconds.
38
     *
39
     * @param int $maxAge
40
     * 
41
     * @return self
42
     */
43
    public function maxAge($maxAge)
44
    {
45
        $this->maxAge = $maxAge;
46
47
        return $this;
48
    }
49
50
    /**
51
     * Configure the includeSubDomains HSTS directive.
52
     *
53
     * @param bool $includeSubdomains
54
     * 
55
     * @return self
56
     */
57
    public function includeSubdomains($includeSubdomains = true)
58
    {
59
        $this->includeSubdomains = $includeSubdomains;
60
61
        return $this;
62
    }
63
64
    /**
65
     * Execute the middleware.
66
     *
67
     * @param RequestInterface  $request
68
     * @param ResponseInterface $response
69
     * @param callable          $next
70
     *
71
     * @return ResponseInterface
72
     */
73
    public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
74
    {
75
        $uri = $request->getUri();
76
77
        if (strtolower($uri->getScheme()) !== 'https') {
78
            $uri = $uri->withScheme('https');
79
80
            if ($this->redirectStatus) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->redirectStatus of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
81
                return self::getRedirectResponse($this->redirectStatus, $uri, $response);
82
            }
83
84
            $request = $request->withUri($uri);
85
        }
86
87
        if (!empty($this->maxAge)) {
88
            $response = $response->withHeader(self::HEADER, sprintf('max-age=%d%s', $this->maxAge, $this->includeSubdomains ? ';includeSubDomains' : ''));
89
        }
90
91
        return $next($request, $response);
92
    }
93
}
94