Completed
Push — master ( 96ffdd...44db84 )
by
unknown
02:19
created

onAfterInit()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 11
nop 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]
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
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...
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($countryObject && $countryObject->Code) {
31
            if (isset($_GET[$param])) {
32
                $countryCode = preg_replace("/[^A-Z]+/", "", strtoupper(Convert::raw2sql($_GET[$param])));
33
                if($countryCode) {
34
                    if (strtoupper($countryObject->Code) != $countryCode) {
35
                        return $this->owner->redirect(
36
                            CountryPrices_ChangeCountryController::new_country_link($countryCode)
37
                        );
38
                    }
39
                }
40
            }
41
            
42
            $countryID = $countryObject->ID;
43
            //check that there is a translation
44
            if ($this->owner->dataRecord->hasCountryLocalInURL($countryID)) {
45
                $newURL = $this->addCountryCodeToUrlIfRequired($countryObject->Code);
46
                if ($newURL) {
47
                    $this->owner->redirect($newURL);
48
                }
49
            }
50
        }
51
52
        $this->owner->dataRecord->loadTranslatedValues($countryID, null);
53
    }
54
55
    /**
56
     * returns the best fieldname for
57
     * @param string $fieldName [description]
58
     */
59
    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...
60
    {
61
        $countryObject = CountryPrice_EcommerceCountry::get_real_country();
62
63
        //check country
64
        if (!empty($countryObject->$fieldName)) {
65
            return $countryObject->$fieldName;
66
        }
67
68
        //check distributor
69
        $distributor = Distributor::get_one_for_country($countryObject->Code);
70
        if (!empty($distributor->$fieldName)) {
71
            return $distributor->$fieldName;
72
        }
73
        //check EcomConfig
74
        $distributor = Distributor::get_one_for_country($countryObject->Code);
75
        if (!empty($distributor->$fieldName)) {
76
            return $distributor->$fieldName;
77
        }
78
    }
79
80
    /**
81
     * caching variable
82
     *
83
     * @var integer
84
     */
85
    private static $_redirection_count = 0;
86
87
    /**
88
     * returns a string for the new url if a locale parameter can be added
89
     *
90
     * @return string | null
91
     */
92
    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...
93
    {
94
        if (isset($_POST) && count($_POST)) {
95
            return null;
96
        }
97
        //to do: add query here!
98
        $protocol = Director::is_https() ? 'https://' : 'http://';
99
        $oldURL = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
100
101
        $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($oldURL);
102
        if($hasCountrySegment){
103
            $newURL = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($countryCode, $oldURL);
104
        }
105
        else {
106
            $newURL = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($countryCode, $oldURL);
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;
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...
132
            $currency = CountryPrice_EcommerceCurrency::get_currency_for_country($country->Code);
133
            $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...
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
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...
153
     */
154
    public function AlternativeHrefLangLinksCachingKey()
155
    {
156
        $countryObject = CountryPrice_EcommerceCountry::get_real_country();
157
        if($countryObject && $countryObject->Code) {
158
            return 'AlternativeHrefLangLinksCachingKey-'.$countryObject->Code.'-'.$this->owner->dataRecord->ID.'-'.strtotime($this->owner->dataRecord->LastEdited);
159
        }
160
        return 'AlternativeHrefLangLinksCachingKey'.$this->owner->dataRecord->ID.'-'.$this->owner->dataRecord->ID.'-'.strtotime($this->owner->dataRecord->LastEdited);
161
    }
162
163
    /**
164
     *
165
     * @param string $link - passed by reference
166
     */
167
    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...
168
    {
169
        $obj = $this->owner->dataRecord->CanonicalObject();
170
        if($obj) {
171
            $link = $obj->Link();
172
        } else {
173
            $link = $this->owner->dataRecord->AbsoluteLink();
174
        }
175
        return $link;
176
    }
177
178
}
179