Completed
Push — master ( efb352...721e90 )
by Nicolaas
02:47
created

CountryURLProvider::hasCountrySegment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 133.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Usage:
5
 *     $myAnswer =
6
 *         CountryPrice_Translation::get_country_url_provider()
7
 *             ->getSomething();
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
    /**
14
     * @var string
15
     */
16
    private static $locale_get_parameter = 'ecomlocale';
17
18
    /**
19
     * returns the selected country code if there is one ...
20
     * as an uppercase code, e.g. NZ
21
     * @param string|null $url
22
     *
23
     * @return bool
24
     */
25
    public function hasCountrySegment($url = '')
26
    {
27
        return $this->CurrentCountrySegment($url) ? true : false;
28
    }
29
30
    /**
31
     * returns the selected country code if there is onCurrentCountrySegmente ...
32
     * as an uppercase code, e.g. NZ
33
     * @param string|null $url
34
     * @param bool $includeGetVariable
35
     *
36
     * @return string|null
37
     */
38
    public function CurrentCountrySegment($url = '', $includeGetVariable = true)
39
    {
40
        $potentialCountry = '';
41
        if ($includeGetVariable)
42
            $getVar = Config::inst()->get('CountryURLProvider', 'locale_get_parameter');
43
            if(isset($_GET[$getVar])) {
44
                $potentialCountry = $_GET[$getVar];
45
            }
46
        }
47
        if (strlen($potentialCountry) !== 2) {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_IF, expecting T_FUNCTION or T_CONST
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $potentialCountry.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
48
            $url = $this->getCurrentURL($url);
49
            $parts = parse_url($url);
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $parts.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
50
            if (isset($parts['path'])) {
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $parts.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
51
                $path = trim($parts['path'], '/');
52
                $array = explode('/', $path);
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $array.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
53
                $potentialCountry = isset($array[0]) ? trim($array[0]) : '';
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $potentialCountry.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
54
            }
55
        }
56
        if (strlen($potentialCountry) === 2) {
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $potentialCountry.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
57
            $potentialCountry = strtoupper($potentialCountry);
58
            $check = EcommerceCountry::get()->filter(['Code' => $potentialCountry])->count();
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $check.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
59
            if ($check) {
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $check.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
60
                return $potentialCountry;
61
            }
62
        }
63
    }
64
65
66
    /**
67
     * replaces a country code in a URL with another one
68
     *
69
     * @param  string $newCountryCode e.g. NZ / nz
70
     * @param  string $url
71
     *
72
     * @return string|null only returns a string if it is different from the original!
73
     */
74
    public function replaceCountryCodeInUrl($newCountryCode, $url = '')
75
    {
76
        $url = $this->getCurrentURL($url);
77
        $oldURL = $url;
78
        debug::log($url);
79
80
        $newCountryCode = strtolower($newCountryCode);
81
        $parsedUrl = parse_url($url);
82
        if (isset($parsedUrl['path']) && isset($parsedUrl['host'])) {
83
            $path = $parsedUrl['path'];
84
            $path = trim($path, '/');
85
            $pathParts = explode('/', $path);
86
87
            $currentCountryCode = $this->CurrentCountrySegment($url, false);
88
            if ($currentCountryCode) {
89
                $pathParts[0] = $newCountryCode;
90
            } else {
91
                array_unshift($pathParts, $newCountryCode);
92
            }
93
            $parsedUrl['path'] = implode('/', $pathParts);
94
            $newURL =
95
                $parsedUrl['scheme'] .
96
                '://' .
97
                Controller::join_links(
98
                    $parsedUrl['host'],
99
                    $parsedUrl['path']
100
                );
101
            if (isset($parsedUrl['query'])) {
102
                $newURL = $newURL . '?' . $parsedUrl['query'];
103
            }
104
        }
105
        if (trim($oldURL, '/') !== trim($newURL, '/')) {
106
            return $newURL;
107
        }
108
109
        return '';
110
    }
111
112
    /**
113
     *
114
     * @param  string|null $url can be a relative one or nothing at all ...
115
     *
116
     * @return string      full URL currently being called.
117
     */
118
    public function getCurrentURL($url = '')
119
    {
120
        if ($url) {
121
            $url = Director::absoluteURL($url);
122
        } else {
123
            $protocol = Director::is_https() ? 'https://' : 'http://';
124
125
            $url = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
126
        }
127
        if (Director::is_site_url($url)) {
128
            return $url;
129
        } else {
130
            return Director::absoluteURL('/');
131
        }
132
    }
133
}
134