Completed
Push — master ( a994d0...47d2e9 )
by Nicolaas
02:15
created

CountryPrice_Page_Controller_Extension   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 11
dl 0
loc 159
rs 10
c 0
b 0
f 0
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]
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...
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)
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
        $protocol = Director::is_https() ? 'https://' : 'http://'
98
        $oldURL = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
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_VARIABLE
Loading history...
99
100
        $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($oldURL);
101
        if($hasCountrySegment){
102
            $newURL = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($countryCode, $oldURL);
103
        }
104
        else {
105
            $newURL = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($countryCode, $oldURL);
106
        }
107
108
        if ($oldURL !== $newURL && self::$_redirection_count < 3) {
109
            self::$_redirection_count++;
110
            return $newURL;
111
        }
112
        return null;
113
    }
114
115
116
    /**
117
     *
118
     *
119
     * @return ArrayList
120
     */
121
    public function ChooseNewCountryList()
122
    {
123
        $countries = CountryPrice_EcommerceCountry::get_real_countries_list();
124
        $currentCode = '';
125
        if ($obj = CountryPrice_EcommerceCountry::get_real_country()) {
126
            $currentCode = $obj->Code;
127
        }
128
        $al = ArrayList::create();
129
        foreach ($countries as $country) {
130
            $isCurrentOne = $currentCode == $country->Code ? true : false;
131
            $currency = null;
132
            $currency = CountryPrice_EcommerceCurrency::get_currency_for_country($country->Code);
133
            $currencyCode = CountryPrice_EcommerceCurrency::get_currency_for_country($country->Code);
134
            $al->push(
135
                ArrayData::create(
136
                    array(
137
                        'Link' => CountryPrices_ChangeCountryController::new_country_link($country->Code),
138
                        'Title' => $country->Name,
139
                        'CountryCode' => $country->Code,
140
                        'LinkingMode' => ($isCurrentOne ? 'current' : 'link'),
141
                        'Currency' => $currency,
142
                        'CurrencyCode' => $currency
143
                    )
144
                )
145
            );
146
        }
147
        return $al;
148
    }
149
150
    /**
151
     *
152
     * @return DataList
153
     */
154
    public function AlternativeHrefLangLinksCachingKey()
155
    {
156
        return 'AlternativeHrefLangLinksCachingKey'.'-'.$this->owner->dataRecord->ID.'-'.strtotime($this->owner->dataRecord->LastEdited);
157
    }
158
159
    /**
160
     *
161
     * @param string $link - passed by reference
162
     */
163
    public function UpdateCanonicalLink(&$link)
164
    {
165
        $obj = $this->owner->dataRecord->CanonicalObject();
166
        if($obj) {
167
            $link = $obj->Link();
168
        } else {
169
            $link = $this->owner->dataRecord->AbsoluteLink();
170
        }
171
        return $link;
172
    }
173
174
}
175