Completed
Branch Testing (40b0cd)
by AJ
02:19
created

SubdomainRoute   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 57
ccs 0
cts 34
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 11 2
B match() 0 19 5
A _getPrefixAndHost() 0 13 3
A _checkPrefix() 0 7 2
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
use Cake\Routing\Route\Route;
21
22
use Multidimensional\Subdomains\Middleware\SubdomainMiddleware;
23
24
class SubdomainRoute extends Route {
25
        
26
    public function parse($url, $method = '') {
27
28
        list($prefix) = $this->_getPrefixAndHost();
29
30
        if (!$this->_checkPrefix($prefix)) {
31
            return false;
32
        }
33
34
        return parent::parse($url, $method);
35
36
    }
37
38
    public function match(array $url, array $context = []) {
39
40
        if (!isset($url['prefix'])) {
41
            $url['prefix'] = false;
42
        }
43
        
44
        if (!$this->_checkPrefix($url['prefix'])) {
45
            return false;
46
        }
47
48
        list($prefix, $host) = $this->_getPrefixAndHost($context);
49
50
        if ($prefix !== $url['prefix']) {
51
            $url['_host'] = $url['prefix'] === false ? $host : $url['prefix'] . '.' . $host;
52
        }
53
        
54
        return parent::match($url, $context);
55
56
    }
57
58
    private function _getPrefixAndHost(array $context = []) {
59
60
        if (empty($context['_host'])) {
61
            $request = Router::getRequest(true) ?: Request::createFromGlobals();
62
            $host = $request->host();
63
        } else {
64
            $host = $context['_host'];
65
        }
66
        
67
        $subdomainObject = new SubdomainMiddleware();
68
        return $subdomainObject->getPrefixAndHost($host);
69
        
70
    }
71
72
    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...
73
74
        $routePrefix = isset($this->defaults['prefix']) ? $this->defaults['prefix'] : false;
75
76
        return $prefix === $routePrefix;
77
78
    }
79
        
80
}