Completed
Push — master ( 3108c8...abd0b4 )
by AJ
8s
created

SubdomainRoute   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 4
dl 0
loc 80
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 11 2
B match() 0 19 5
A _getPrefixAndHost() 0 12 3
A _checkPrefix() 0 7 2
A _getSubdomains() 0 6 1
A _subdomainCheck() 0 15 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\Routing\Route;
17
18
use Cake\Routing\Router;
19
use Cake\Network\Request;
20
21
use Cake\Routing\Route\Route;
22
23
use Multidimensional\Subdomains\Middleware\SubdomainMiddleware;
24
25
class SubdomainRoute extends Route {
26
    	
27
    public function parse($url, $method = '') {
28
29
        list($prefix) = $this->_getPrefixAndHost();
30
31
        if (!$this->_checkPrefix($prefix)) {
32
            return false;
33
        }
34
35
        return parent::parse($url, $method);
36
37
    }
38
39
    public function match(array $url, array $context = []) {
40
41
        if (!isset($url['prefix'])) {
42
            $url['prefix'] = false;
43
        }
44
		
45
        if (!$this->_checkPrefix($url['prefix'])) {
46
            return false;
47
        }
48
49
        list($prefix, $host) = $this->_getPrefixAndHost($context);
50
51
        if ($prefix !== $url['prefix']) {
52
            $url['_host'] = $url['prefix'] === false ? $host : $url['prefix'] . '.' . $host;
53
        }
54
        
55
        return parent::match($url, $context);
56
57
    }
58
 
59
60
    private function _getPrefixAndHost(array $context = []) {
61
62
        if (empty($context['_host'])) {
63
            $request = Router::getRequest(true) ?: Request::createFromGlobals();
64
            $host = $request->host();
65
        } else {
66
            $host = $context['_host'];
67
        }
68
69
		return $this->_subdomainCheck($host);
70
        
71
    }
72
73
    private function _checkPrefix($prefix) {
0 ignored issues
show
Coding Style introduced by
function _checkPrefix() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
74
75
        $routePrefix = isset($this->defaults['prefix']) ? $this->defaults['prefix'] : false;
76
77
        return $prefix === $routePrefix;
78
79
    }
80
	
81
    private function _getSubdomains() {
82
        
83
        $subdomains = new SubdomainMiddleware();
84
        return $subdomains->getSubdomains();
85
        
86
    }
87
	
88
	private function _subdomainCheck($host) {
89
		
90
		if (preg_match('/(.*?)\.([^\/]*\..{2,5})/i', $host, $parts)) {
91
                        
92
            if (in_array($parts[1], $this->_getSubdomains())) {
93
                return [$parts[1], $parts[2]];
94
            } else {
95
                return [false, $parts[2]];
96
            }
97
            
98
        } else {
99
            return [false, $host];
100
        }		
101
		
102
	}
103
104
}