Completed
Push — master ( 0df295...ff7fdb )
by
unknown
02:27
created

loadTranslatedValues()   C

Complexity

Conditions 13
Paths 90

Size

Total Lines 47
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 5.0999
c 0
b 0
f 0
cc 13
eloc 34
nc 90
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
class CountryPrice_SiteTreeExtensions extends SiteTreeExtension
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...
5
{
6
    private static $has_many = array(
0 ignored issues
show
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
7
        'CountryPriceTranslations' => 'CountryPrice_Translation'
8
    );
9
10
    private static $field_labels = array(
0 ignored issues
show
Unused Code introduced by
The property $field_labels is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
        'CountryPriceTranslations' => 'Translations'
12
    );
13
14
    /**
15
     * Update Fields
16
     * @return FieldList
17
     */
18
    public function updateCMSFields(FieldList $fields)
19
    {
20
        $fields->addFieldsToTab(
21
            'Root.Translations',
22
            GridField::create(
0 ignored issues
show
Documentation introduced by
\GridField::create('Coun...orOrderItems::create()) is of type object<GridField>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
23
                'CountryPriceTranslations',
24
                'Translations',
25
                $this->owner->CountryPriceTranslations(),
26
                GridFieldConfigForOrderItems::create()
27
            )
28
        );
29
        return $fields;
30
    }
31
32
    private static $_translations = array();
33
34
    /**
35
     *
36
     * @return DataList
37
     */
38
    public function AvailableTranslationLinks()
39
    {
40
        return $this->owner->CountryPriceTranslations()
41
            ->innerJoin('EcommerceCountry', '"EcommerceCountry"."ID" = "CountryPrice_Translation"."EcommerceCountryID"');
42
    }
43
44
    /**
45
     *
46
     * @return CountryPrice_Translation | null
47
     */
48
    public function CanonicalLink()
49
    {
50
        return $this->owner->CountryPriceTranslations()
51
            ->innerJoin('EcommerceCountry', '"EcommerceCountry"."ID" = "CountryPrice_Translation"."EcommerceCountryID"')
52
            ->filter(array('EcommerceCountry.IsBackupCountry' => 1))
53
            ->first();
54
    }
55
56
    public function loadTranslatedValues($countryID = 0, $variableOrMethod = '')
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...
57
    {
58
        $translation = null;
59
        if (! $countryID) {
60
            $countryObject = CountryPrice_EcommerceCountry::get_real_country();
61
            if ($countryObject) {
62
                $countryID = $countryObject->ID;
63
            }
64
        }
65
        if ($countryID) {
66
            $key = $this->owner->ID.'_'.$countryID;
67
            if (isset(self::$_translations[$key])) {
68
                $translation = self::$_translations[$key];
69
            } else {
70
                $translation = $this->owner->getRealEcommerceTranslation($countryID);
71
                self::$_translations[$key] = $translation;
72
            }
73
        }
74
        if ($translation) {
75
            $fieldsToReplace = $translation->FieldsToReplace();
76
            foreach ($fieldsToReplace as $replaceFields) {
77
                $pageField = $replaceFields->PageField;
78
                $pageFieldTranslated = $pageField . 'Translated';
79
                $translationField = $replaceFields->TranslationField;
80
                if (! $variableOrMethod || $variableOrMethod === $pageField) {
81
                    if ($translation->hasMethod($translationField)) {
82
                        $this->owner->$pageField = $translation->$translationField();
83
                        $this->owner->$pageFieldTranslated = $translation->$translationField();
84
                    } else {
85
                        $this->owner->$pageField = $translation->$translationField;
86
                        $this->owner->$pageFieldTranslated = $translation->$translationField;
87
                    }
88
                }
89
                if ($variableOrMethod) {
90
                    return $this->owner->$pageField;
91
                }
92
            }
93
        } else {
94
            if ($variableOrMethod) {
95
                if ($translation->hasMethod($variableOrMethod)) {
96
                    return $this->owner->$variableOrMethod();
97
                } else {
98
                    return $this->owner->$variableOrMethod;
99
                }
100
            }
101
        }
102
    }
103
104
    /**
105
     * @var int $countryID
106
     *
107
     * @return CountryPrice_Translation | null
0 ignored issues
show
Documentation introduced by
Should the return type not be CountryPrice_Translation|null?

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...
108
     */
109
    public function getRealEcommerceTranslation($countryID)
110
    {
111
        return CountryPrice_Translation::get()
112
            ->filter(
113
                array(
114
                    "EcommerceCountryID" => $countryID,
115
                    'ParentID' => $this->owner->ID
116
                )
117
            )
118
            ->exclude(
119
                array('WithoutTranslation' => 1)
120
            )
121
            ->first();
122
    }
123
124
    /**
125
     * @var int $countryID
126
     *
127
     * @return CountryPrice_Translation | null
128
     */
129
    public function getEcommerceTranslation($countryID)
130
    {
131
        return $this->owner
132
            ->CountryPriceTranslations()
133
            ->filter(
134
                array(
135
                    "EcommerceCountryID" => $countryID,
136
                    'ParentID' => $this->owner->ID,
137
                )
138
            )
139
            ->first();
140
    }
141
142
    /**
143
     * @var int $countryID
144
     *
145
     * @return bool
146
     */
147
    public function hasCountryLocalInURL($countryID)
148
    {
149
        return $this->getEcommerceTranslation($countryID) ? true : false;
150
    }
151
152
}
153