SubdomainRoute::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 6
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\Network\Request;
19
use Cake\Routing\Router;
20
use Cake\Routing\Route\Route;
21
use Multidimensional\Subdomains\Middleware\SubdomainMiddleware;
22
23
class SubdomainRoute extends Route
24
{
25
26
    /**
27
     * @param string $url
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
28
     * @param string $method
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
29
     * @return bool|array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use false|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
30
     */
31
    public function parse($url, $method = '')
32
    {
33
        list($prefix) = $this->_getPrefixAndHost();
34
35
        if (!$this->_checkPrefix($prefix)) {
36
            return false;
37
        }
38
39
        return parent::parse($url, $method);
40
    }
41
42
    /**
43
     * @param array $url
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
44
     * @param array $context
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
45
     * @return bool|array
0 ignored issues
show
Documentation introduced by
Should the return type not be false|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
46
     */
47
    public function match(array $url, array $context = [])
48
    {
49
        if (!isset($url['prefix'])) {
50
            $url['prefix'] = null;
51
        }
52
53
        if (!$this->_checkPrefix($url['prefix'])) {
54
            return false;
55
        }
56
57
        list($prefix, $host) = $this->_getPrefixAndHost($context);
58
59
        if ($prefix !== $url['prefix']) {
60
            $url['_host'] = $url['prefix'] === 'false' ? $host : $url['prefix'] . '.' . $host;
61
        }
62
63
        return parent::match($url, $context);
64
    }
65
66
    /**
67
     * @param array $context
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
68
     * @return array
69
     */
70
    private function _getPrefixAndHost(array $context = [])
71
    {
72
        if (empty($context['_host'])) {
73
            $request = Router::getRequest(true) ?: Request::createFromGlobals();
74
            $host = $request->host();
75
        } else {
76
            $host = $context['_host'];
77
        }
78
        $subdomainMiddleware = new SubdomainMiddleware();
79
80
        return $subdomainMiddleware->getPrefixAndHost($host);
81
    }
82
83
    /**
84
     * @param string $prefix
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
85
     * @return bool
86
     */
87
    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...
88
    {
89
        $routePrefix = isset($this->defaults['prefix']) ? $this->defaults['prefix'] : null;
90
91
        return $prefix === $routePrefix;
92
    }
93
}
94