Www   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 3
dl 0
loc 87
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B __invoke() 0 20 6
C canAddWww() 0 25 7
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Middleware to redirect to force www subdomain or remove it.
11
 */
12
class Www
13
{
14
    use Utils\RedirectTrait;
15
16
    /**
17
     * @var bool Add or remove www
18
     */
19
    private $addWww;
20
21
    /**
22
     * Configure whether the www subdomain should be added or removed.
23
     *
24
     * @param bool $addWww
25
     */
26
    public function __construct($addWww = false)
27
    {
28
        $this->addWww = (bool) $addWww;
29
        $this->redirect(301);
30
    }
31
32
    /**
33
     * Execute the middleware.
34
     *
35
     * @param ServerRequestInterface $request
36
     * @param ResponseInterface      $response
37
     * @param callable               $next
38
     *
39
     * @return ResponseInterface
40
     */
41
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
42
    {
43
        $uri = $request->getUri();
44
        $host = $uri->getHost();
45
46
        if ($this->addWww) {
47
            if ($this->canAddWww($host)) {
48
                $host = "www.{$host}";
49
            }
50
        } elseif (strpos($host, 'www.') === 0) {
51
            $host = substr($host, 4);
52
        }
53
54
        //redirect
55
        if ($this->redirectStatus !== false && ($uri->getHost() !== $host)) {
56
            return $this->getRedirectResponse($request, $uri->withHost($host), $response);
57
        }
58
59
        return $next($request->withUri($uri->withHost($host)), $response);
60
    }
61
62
    /**
63
     * Check whether the domain can add a www. subdomain.
64
     * Returns false if:
65
     * - the host is "localhost"
66
     * - the host is a ip
67
     * - the host has already a subdomain, for example "subdomain.example.com".
68
     *
69
     * @param string $host
70
     *
71
     * @return bool
72
     */
73
    private function canAddWww($host)
74
    {
75
        if (empty($host) || filter_var($host, FILTER_VALIDATE_IP)) {
76
            return false;
77
        }
78
79
        $host = explode('.', $host);
80
81
        switch (count($host)) {
82
            case 1: //localhost
83
                return false;
84
85
            case 2: //example.com
86
                return true;
87
88
            case 3:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
89
                //example.co.uk
90
                if ($host[1] === 'co') {
91
                    return true;
92
                }
93
94
            default:
95
                return false;
96
        }
97
    }
98
}
99