Passed
Push — master ( 123071...efb352 )
by Nicolaas
02:34
created

CountryPrice_Page_Controller_Extension   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 11
dl 0
loc 179
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B ChooseNewCountryList() 0 28 5
C onAfterInit() 0 34 7
A CountryDistributorBestContentValue() 0 20 4
B addCountryCodeToUrlIfRequired() 0 16 5
A AlternativeHrefLangLinksCachingKey() 0 8 3
A UpdateCanonicalLink() 0 10 2
A checkForOffsiteRedirects() 0 8 2
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()
26
    {
27
        $countryID = 0;
28
        //provided by stealth ...
29
        $countryObject = CountryPrice_EcommerceCountry::get_real_country();
30
        if ($countryObject) {
31
32
            //check if a redirect is required ...
33
            $this->checkForOffsiteRedirects($countryObject);
34
35
            $countryID = $countryObject->ID;
36
            //check that there is a translation
37
            if ($this->owner->dataRecord->thisPageHasTranslation($countryID)) {
38
                //if there is a translation but it is not showing in the URL then redirect
39
                $newURL = $this->addCountryCodeToUrlIfRequired($countryObject->Code);
40
                if ($newURL) {
41
                    if (Director::is_site_url($newURL)) {
42
                        if ($this->owner->request->getVar('nothing')) {
43
                            return;
44
                        } else {
45
                            if(!strpos($newURL, '?')) {
46
                                $newURL .= '?nothing=limitless';
47
                            } else {
48
                                $newURL .= '&nothing=limitless';
49
                            }
50
                        }
51
                        $this->owner->redirect($newURL);
52
                    }
53
                }
54
            }
55
        }
56
57
        $this->owner->dataRecord->loadTranslatedValues($countryID, null);
58
    }
59
60
    /**
61
     * returns the best fieldname for
62
     * @param string $fieldName [description]
63
     */
64
    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...
65
    {
66
        $countryObject = CountryPrice_EcommerceCountry::get_real_country();
67
68
        //check country
69
        if (!empty($countryObject->$fieldName)) {
70
            return $countryObject->$fieldName;
71
        }
72
73
        //check distributor
74
        $distributor = Distributor::get_one_for_country($countryObject->Code);
75
        if (!empty($distributor->$fieldName)) {
76
            return $distributor->$fieldName;
77
        }
78
        //check EcomConfig
79
        $distributor = Distributor::get_one_for_country($countryObject->Code);
80
        if (!empty($distributor->$fieldName)) {
81
            return $distributor->$fieldName;
82
        }
83
    }
84
85
    /**
86
     * caching variable
87
     *
88
     * @var integer
89
     */
90
    private static $_redirection_count = 0;
91
92
    /**
93
     * returns a string for the new url if a locale parameter can be added
94
     *
95
     * @return string | null
96
     */
97
    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...
98
    {
99
        if (isset($_POST) && count($_POST)) {
100
            return null;
101
        }
102
        //to do: add query here!
103
104
        $newURL = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($countryCode);
0 ignored issues
show
Bug introduced by
The method replaceCountryCodeInUrl cannot be called on \CountryPrice_Translatio..._country_url_provider() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
105
106
        if ($newURL && self::$_redirection_count < 3) {
107
            self::$_redirection_count++;
108
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
        $countryObject = CountryPrice_EcommerceCountry::get_real_country();
156
        if ($countryObject && $countryObject->Code) {
157
            return 'AlternativeHrefLangLinksCachingKey-'.$countryObject->Code.'-'.$this->owner->dataRecord->ID.'-'.strtotime($this->owner->dataRecord->LastEdited);
158
        }
159
        return 'AlternativeHrefLangLinksCachingKey'.$this->owner->dataRecord->ID.'-'.$this->owner->dataRecord->ID.'-'.strtotime($this->owner->dataRecord->LastEdited);
160
    }
161
162
    /**
163
     *
164
     * @param string $link - passed by reference
165
     */
166
    public function UpdateCanonicalLink(&$link)
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...
167
    {
168
        $obj = $this->owner->dataRecord->CanonicalObject();
169
        if ($obj) {
170
            $link = $obj->Link();
171
        } else {
172
            $link = $this->owner->dataRecord->AbsoluteLink();
173
        }
174
        return $link;
175
    }
176
177
    /**
178
     * redirects visitors to another website if they are listed as such in
179
     * CountryPrices_ChangeCountryController.off_site_url_redirects
180
     *
181
     * @param  EcommerceCountry $countryObject current country of visitor
182
     *
183
     * @return null|SS_HTTPResponse
184
     */
185
    protected function checkForOffsiteRedirects($countryObject)
186
    {
187
        $redirectsArray = Config::inst()->get('CountryPrices_ChangeCountryController', 'off_site_url_redirects');
188
        $myCountryCode = strtoupper($countryObject->Code);
189
        if (isset($redirectsArray[$myCountryCode])) {
190
            return $this->redirect($redirectsArray[$myCountryCode]);
0 ignored issues
show
Bug introduced by
The method redirect() does not exist on CountryPrice_Page_Controller_Extension. Did you maybe mean checkForOffsiteRedirects()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
191
        }
192
    }
193
}
194