Passed
Push — master ( cf3ebf...13bedb )
by
unknown
02:47
created

CountryURLProvider::addCountryCodeToUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
3
/**
4
 * Usage:
5
 *     $myAnswer =
6
 *         CountryPrice_Translation::get_country_url_provider()
7
 *             ->doSomething(....);
8
 *
9
 */
10
11
class CountryURLProvider extends Object implements CountryURLProviderInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    private static $country_segments  = array('nz', 'au', 'gb', 'eu', 'us', 'row');
0 ignored issues
show
Unused Code introduced by
The property $country_segments is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
15
    public function hasCountrySegment($url = ''){
16
        $url = $this->getDefaultURL($url);
17
        $parsedUrl = parse_url($url);
18
        $pathSegments = explode("/", $parsedUrl['path']);
19
        $firstSegment = '';
20
        $countries =  Config::inst()->get('CountryURLProvider', 'country_segments');
21
        foreach ($pathSegments as $position => $segment){
22
            if($segment){
23
                $firstSegment = $segment;
24
                break;
25
            }
26
        }
27
        if(in_array($firstSegment, $countries)){
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return in_array($firstSegment, $countries);.
Loading history...
28
            return true;
29
        }
30
        return false;
31
    }
32
33
    public function replaceCountryCodeInUrl($countryCode, $url = ''){
34
        $url = $this->getDefaultURL($url);
35
        $parsedUrl = parse_url($url);
36
        $pathParts = explode('/', $parsedUrl['path']);
37
        $countries =  Config::inst()->get('CountryURLProvider', 'country_segments');
38
        foreach($pathParts as $pathPartsKey => $pathPart) {
39
            //check for first match
40
            if(in_array($pathPart, $countries)) {
41
                $pathParts[$pathPartsKey] = strtolower($countryCode);
42
                break;
43
            }
44
        }
45
        $parsedUrl['path'] = implode('/', $pathParts);
46
        $url = $parsedUrl['scheme']. '://'. $parsedUrl['host']. $parsedUrl['path'];
47
        if(isset($parsedUrl['query'])){
48
            $url = $url . $parsedUrl['query'];
49
        }
50
        return $url;
51
    }
52
53
    public function addCountryCodeToUrl($countryCode, $url = ''){
54
        $url = $this->getDefaultURL($url);
55
        $parsedUrl = parse_url($url);
56
        $url = $parsedUrl['scheme']. '://'. $parsedUrl['host']. '/'. strtolower($countryCode) . $parsedUrl['path'];
57
        if(isset($parsedUrl['query'])){
58
            $url = $url . $parsedUrl['query'];
59
        }
60
        return $url;
61
    }
62
63
    private function getDefaultURL($url = '')
0 ignored issues
show
Coding Style introduced by
getDefaultURL uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
64
    {
65
        if($url) {
66
            return Director::absoluteURL($url);
67
        }
68
        return (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
69
    }
70
71
}
72