UrlManager   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 111
Duplicated Lines 4.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 1
dl 5
loc 111
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C getUrl() 5 54 21
A encodeToUrl() 0 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Url Manager
5
 *
6
 * @category  	core
7
 * @package   	core\
8
 * @author    	Judicaël Paquet <[email protected]>
9
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
10
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
11
 * @version   	Release: 1.0.0
12
 * @filesource	https://github.com/las93/venus2
13
 * @link      	https://github.com/las93
14
 * @since     	1.0
15
 */
16
namespace Venus\core;
17
18
/**
19
 * Url Manager
20
 *
21
 * @category  	core
22
 * @package   	core\
23
 * @author    	Judicaël Paquet <[email protected]>
24
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
25
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
26
 * @version   	Release: 1.0.0
27
 * @filesource	https://github.com/las93/venus2
28
 * @link      	https://github.com/las93
29
 * @since     	1.0
30
 */
31
class UrlManager
32
{
33
    /**
34
     * The base Uri to construct the route
35
     * @var string
36
     */
37
    private $_sBaseUri = '';
38
39
    /**
40
     * create an URL
41
     *
42
     * @access public
43
     * @param  string $sCode code of the url between "routes" and "route" in Route.conf
44
     * @param  array $aParams parameters to create the url
45
     * @return string
46
     *
47
     * @tutorial	If I have this route I could make my URL:
48
     *
49
     * 				"menu_edit": {
50
     *					"route": "[/:language]/menu[/:id]/edit/",
51
     *					"controller": "\\src\\BackOffice\\Controller\\MenuManager",
52
     *					"action": "edit",
53
     *					"constraints": {
54
     * 						"language": "[a-z]{0,2}",
55
     * 						"id": "[0-9]+"
56
     *					},
57
     *					"content_type": "html"
58
     *				},
59
     *
60
     *				I must write this:
61
     *
62
     *				$oUrlManager = new \Venus\core\UrlManager;
63
     *				$sUrl = $oUrlManager->getUrl('menu_edit', array('language' => 'vn', 'id' => 125));
64
     */
65
    public function getUrl(string $sCode, array $aParams = array()) : string
66
    {
67
        if (isset($_SERVER) && isset($_SERVER['HTTP_HOST'])) {
68
69
            foreach (Config::get('Route') as $sHost => $oHost) {
0 ignored issues
show
Bug introduced by
The expression \Venus\core\Config::get('Route') of type null is not traversable.
Loading history...
70
71
                if ((!strstr($sHost, '/') && $sHost == $_SERVER['HTTP_HOST'])
72
                    || (strstr($sHost, '/')
73
                    && strstr($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], $sHost))) {
74
75 View Code Duplication
                    if (strstr($sHost, '/')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
                        && strstr($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], $sHost)) {
77
78
                        $this->_sBaseUri = preg_replace('#^[^/]+#', '', $sHost);
79
                    }
80
81
                    if (isset($oHost->routes)) {
82
83
                        foreach($oHost->routes as $sKey => $oRoute) {
84
85
                            if ($sKey === $sCode) {
86
87
                                $sRoute = $this->_sBaseUri.$oRoute->route;
88
89
                                if (isset($oRoute->constraints)) {
90
91
                                    foreach ($oRoute->constraints as $sName => $sType) {
92
93
                                        if (!isset($aParams[$sName])) { $aParams[$sName] = ''; }
94
95
                                        if (preg_match('#'.$sType.'#', $aParams[$sName])) {
96
97
                                            if ($aParams[$sName]) { $sRoute = str_replace('[/:'.$sName.']', '/'.$aParams[$sName], $sRoute); } else { $sRoute = str_replace('[/:'.$sName.']', '', $sRoute); }
98
                                            $sRoute = str_replace('[:'.$sName.']', $aParams[$sName], $sRoute);
99
                                            continue;
100
                                        } else if (isset($oRoute->defaults_constraints)
101
                                            && isset($oRoute->defaults_constraints->{$sName})
102
                                            && preg_match('#'.$sType.'#', $oRoute->defaults_constraints->{$sName})) {
103
104
                                            continue;
105
                                        }
106
107
                                        throw new \Exception('For the route '.$sCode.' the parameter '.$sName.' is not good!');
108
                                    }
109
                                }
110
111
                                return $sRoute;
112
                            }
113
                        }
114
                    }
115
                }
116
            }
117
        }
118
    }
119
120
    /**
121
     * encode text for the url
122
     *
123
     * @access public
124
     * @param  string $sStringToEncode text
125
     * @return string
126
     */
127
    public function encodeToUrl(string $sStringToEncode) : string
128
    {
129
        if (!is_string($sStringToEncode)) {
130
131
            throw new \Exception();
132
        }
133
134
        $sStringToEncode = str_replace(['à','á','â','ã','ä','ç','è','é','ê','ë','ì','í','î','ï','ñ','ò','ó','ô','õ','ö','ù','ú','û','ü','ý','ÿ','À','Á','Â','Ã','Ä','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ñ','Ò','Ó','Ô','Õ','Ö','Ù','Ú','Û','Ü','Ý'],
135
            ['a','a','a','a','a','c','e','e','e','e','i','i','i','i','n','o','o','o','o','o','u','u','u','u','y','y','A','A','A','A','A','C','E','E','E','E','I','I','I','I','N','O','O','O','O','O','U','U','U','U','Y'],
136
                $sStringToEncode);
137
138
        $sStringToEncode = preg_replace('/[^a-zA-Z0-9_]+/', '_', preg_quote($sStringToEncode));
139
        return strtolower($sStringToEncode);
140
    }
141
}
142