Completed
Push — master ( fd8864...aeb2ba )
by AJ
01:59
created

SubdomainMiddleware::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 9.2
cc 3
eloc 10
nc 3
nop 3
1
<?php
2
/**
3
 * CakePHP Plugin : CakePHP Subdomain Routing
4
 * Copyright (c) Multidimension.al (http://multidimension.al)
5
 * Github : https://github.com/multidimension-al/cakephp-subdomains
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE file
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     (c) Multidimension.al (http://multidimension.al)
12
 * @link          https://github.com/multidimension-al/cakephp-subdomains Github
13
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
16
namespace Multidimensional\Subdomains\Middleware;
17
18
use Cake\Core\Configure;
19
20
class SubdomainMiddleware {
21
        
22
    /**
23
     * @param \Psr\Http\Message\ServerRequestInterface $request The request.
24
     * @param \Psr\Http\Message\ResponseInterface $response The response.
25
     * @param callable $next The next middleware to call.
26
     * @return \Psr\Http\Message\ResponseInterface A response.
27
     */
28
    public function __invoke($request, $response, $next) {
29
        
30
        $uri = $request->getUri();
31
        $host = $uri->getHost();
32
        
33
        list($prefix) = $this->getPrefixAndHost($host);
34
        
35
        if($prefix !== false){
36
            
37
            $params = (array) $request->getAttribute('params', []);
38
            
39
            if (empty($params['prefix'])) {
40
                $params['prefix'] = $prefix;            
41
            }
42
            
43
            $request = $request->withAttribute('params', $params);
44
        
45
        }
46
47
        return $next($request, $response);
48
        
49
    }    
50
    
51
    public function getSubdomains() {
52
        
53
        $validConfiguration = Configure::check('Multidimensional/Subdomains.subdomains');
54
        
55
        if (!$validConfiguration) {
56
            return [];
57
        }
58
        
59
        $subdomains = Configure::read('Multidimensional/Subdomains.subdomains');
60
        
61
        if (!is_array($subdomains) || count($subdomains) == 0) {
62
            return [];
63
        }
64
        
65
        return $subdomains;
66
        
67
    }
68
    
69
    public function getPrefixAndHost($host) {
70
        
71
        if (empty($host)) {
72
            return [false, false];
73
        }
74
        
75
        if (preg_match('/(.*?)\.([^\/]*\..{2,5})/i', $host, $match)) {
76
                        
77
            if (in_array($match[1], $this->getSubdomains())) {
78
                return [$match[1], $match[2]];
79
            } else {
80
                return [false, $match[2]];
81
            }
82
            
83
        } else {
84
            return [false, $host];
85
        }  
86
        
87
    }
88
    
89
}