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

addCountryCodeToUrlIfRequired()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 13
nc 5
nop 1
1
<?php
2
3
4
/**
5
 * www.mysite.com/mypage/?ecomlocale=AU
6
 * if there is a tranlsation page redirects to
7
 * URL with ?ecomlocale=AU
8
 *
9
 * if you go to a URL with ?ecomlocale=AU and the shop country does not match
10
 * the get param then you get redirected to that shop country.
11
 *
12
 *
13
 */
14
15
class CountryPrice_Page_Controller_Extension extends Extension
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...
16
{
17
18
    /**
19
     * replaces `Title` and `Content` with translated content
20
     * where available.
21
     *
22
     * If the country code in the get parameter is not correct then
23
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
24
     */
25
    public function onAfterInit()
0 ignored issues
show
Coding Style introduced by
onAfterInit uses the super-global variable $_GET 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...
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
26
    {
27
        $countryID = 0;
28
        $param = Config::inst()->get('CountryPrice_Translation', 'locale_get_parameter');
29
        $countryObject = CountryPrice_EcommerceCountry::get_real_country();
30
        if (isset($_GET[$param])) {
31
            $countryCode = preg_replace("/[^A-Z]+/", "", strtoupper(Convert::raw2sql($_GET[$param])));
32
            if ($countryObject->Code != $countryCode) {
33
                return $this->owner->redirect(
34
                    CountryPrices_ChangeCountryController::new_country_link($countryCode)
35
                );
36
            }
37
        }
38
39
        //check that there is a country!
40
        if ($countryObject) {
41
            $countryID = $countryObject->ID;
42
            //check that there is a translation
43
            if ($this->owner->dataRecord->hasCountryLocalInURL($countryID)) {
44
                $newURL = $this->addCountryCodeToUrlIfRequired($countryObject->Code);
45
                if ($newURL) {
46
                    $this->owner->redirect($newURL);
47
                }
48
            }
49
        }
50
51
        $this->owner->dataRecord->loadTranslatedValues($countryID, null);
52
    }
53
54
    /**
55
     * returns the best fieldname for
56
     * @param string $fieldName [description]
57
     */
58
    public function CountryDistributorBestContentValue($fieldName)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
59
    {
60
        $countryObject = CountryPrice_EcommerceCountry::get_real_country();
61
62
        //check country
63
        if (!empty($countryObject->$fieldName)) {
64
            return $countryObject->$fieldName;
65
        }
66
67
        //check distributor
68
        $distributor = Distributor::get_one_for_country($countryObject->Code);
69
        if (!empty($distributor->$fieldName)) {
70
            return $distributor->$fieldName;
71
        }
72
        //check EcomConfig
73
        $distributor = Distributor::get_one_for_country($countryObject->Code);
74
        if (!empty($distributor->$fieldName)) {
75
            return $distributor->$fieldName;
76
        }
77
    }
78
79
    /**
80
     * caching variable
81
     *
82
     * @var integer
83
     */
84
    private static $_redirection_count = 0;
85
86
    /**
87
     * returns a string for the new url if a locale parameter can be added
88
     *
89
     * @return string | null
90
     */
91
    private function addCountryCodeToUrlIfRequired($countryCode = '')
0 ignored issues
show
Coding Style introduced by
addCountryCodeToUrlIfRequired uses the super-global variable $_POST 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...
Coding Style introduced by
addCountryCodeToUrlIfRequired 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...
92
    {
93
        if (isset($_POST) && count($_POST)) {
94
            return null;
95
        }
96
        //to do: add query here!
97
        $oldURL = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
98
99
        $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($oldURL);
100
        if($hasCountrySegment){
101
            $newURL = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($countryCode, $oldURL);
102
        }
103
        else {
104
            $newURL = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($countryCode, $oldURL);
105
        }
106
107
        if ($oldURL !== $newURL && self::$_redirection_count < 3) {
108
            self::$_redirection_count++;
109
            return $newURL;
110
        }
111
        return null;
112
    }
113
114
115
    /**
116
     *
117
     *
118
     * @return ArrayList
119
     */
120
    public function ChooseNewCountryList()
121
    {
122
        $countries = CountryPrice_EcommerceCountry::get_real_countries_list();
123
        $currentCode = '';
124
        if ($obj = CountryPrice_EcommerceCountry::get_real_country()) {
125
            $currentCode = $obj->Code;
126
        }
127
        $al = ArrayList::create();
128
        foreach ($countries as $country) {
129
            $isCurrentOne = $currentCode == $country->Code ? true : false;
130
            $currency = null;
0 ignored issues
show
Unused Code introduced by
$currency is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
131
            $currency = CountryPrice_EcommerceCurrency::get_currency_for_country($country->Code);
132
            $currencyCode = CountryPrice_EcommerceCurrency::get_currency_for_country($country->Code);
0 ignored issues
show
Unused Code introduced by
$currencyCode is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
            $al->push(
134
                ArrayData::create(
135
                    array(
136
                        'Link' => CountryPrices_ChangeCountryController::new_country_link($country->Code),
137
                        'Title' => $country->Name,
138
                        'CountryCode' => $country->Code,
139
                        'LinkingMode' => ($isCurrentOne ? 'current' : 'link'),
140
                        'Currency' => $currency,
141
                        'CurrencyCode' => $currency
142
                    )
143
                )
144
            );
145
        }
146
        return $al;
147
    }
148
149
    /**
150
     *
151
     * @return DataList
0 ignored issues
show
Documentation introduced by
Should the return type not be 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...
152
     */
153
    public function AlternativeHrefLangLinksCachingKey()
154
    {
155
        return 'AlternativeHrefLangLinksCachingKey'.'-'.$this->owner->dataRecord->ID.'-'.strtotime($this->owner->dataRecord->LastEdited);
156
    }
157
}
158