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

get_country_url_provider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
4
5
class CountryPrice_Translation extends DataObject
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...
6
{
7
8
    private static $automatically_create_dummy_translations_for_products_and_productgroups = true;
0 ignored issues
show
Unused Code introduced by
The property $automatically_create_du...ducts_and_productgroups 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...
9
10
    private static $dependencies = array(
0 ignored issues
show
Unused Code introduced by
The property $dependencies 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
        'CountryURLProvider' => '%$CountryURLProvider',
12
    );
13
14
    /**
15
     * automatically populated by the dependency manager.
16
     *
17
     * @var CountryURLProvider
18
     */
19
    public $CountryURLProvider = null;
20
21
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db 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...
22
        'Title' => 'Varchar(200)',
23
        'UseOriginalTitle' => 'Boolean',
24
        'Content' => 'HTMLText',
25
        'UseOriginalContent' => 'Boolean',
26
        'WithoutTranslation' => 'Boolean'
27
    );
28
29
    private static $field_labels = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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...
30
        'Title' => 'Page Title',
31
        'Content' => 'Page Content',
32
        'EcommerceCountryID' => 'Country',
33
        'WithoutTranslation' => 'Price Difference Only'
34
    );
35
36
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one 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...
37
        'EcommerceCountry' => 'EcommerceCountry',
38
        'Parent' => 'SiteTree'
39
    );
40
41
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $summary_fields 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...
42
        'EcommerceCountry.Name' => 'Country',
43
        'Title' => 'Title',
44
        'WithoutTranslation.Nice' => 'Price Difference Only'
45
    );
46
47
    private static $casting = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $casting 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...
48
        'Link' => 'Varchar'
49
    );
50
51
    /**
52
     * @var string
53
     */
54
    private static $locale_get_parameter = 'ecomlocale';
0 ignored issues
show
Unused Code introduced by
The property $locale_get_parameter 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...
55
56
    /**
57
     * Standard SS variable.
58
     *
59
     * @var string
60
     */
61
    private static $singular_name = 'Translation';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
62
    public function i18n_singular_name()
63
    {
64
        return self::$singular_name;
65
    }
66
67
    /**
68
     * Standard SS variable.
69
     *
70
     * @var string
71
     */
72
    private static $plural_name = 'Translations';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
73
    public function i18n_plural_name()
74
    {
75
        return self::$plural_name;
76
    }
77
78
    public static function get_country_url_provider()
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...
79
    {
80
        $obj = Injector::inst()->get('CountryPrice_Translation');
81
        return $obj->CountryURLProvider;
82
    }
83
84
    /**
85
     * CMS Fields
86
     * @return FieldList
87
     */
88
    public function getCMSFields()
89
    {
90
        $fields = parent::getCMSFields();
91
        $withoutTranslationField = $fields->dataFieldByName('WithoutTranslation');
92
        $withoutTranslationField = CheckboxField::create(
93
            'WithoutTranslation',
94
            $withoutTranslationField->Title()
95
        )
96
        ->setDescription('The page is <em>translated</em> for search engines because it has prices for this country. ');
97
98
        $countries = CountryPrice_EcommerceCountry::get_real_countries_list()->map()->toArray();
99
        $countryDropdownField = DropdownField::create(
100
            'EcommerceCountryID',
101
            $fields->dataFieldByName('EcommerceCountryID')->Title(),
102
            array('' => '-- make sure to select a country --')+$countries
103
        );
104
105
        $fields->removeFieldFromTab("Root.Main", 'ParentID');
106
        if($this->WithoutTranslation) {
0 ignored issues
show
Documentation introduced by
The property WithoutTranslation does not exist on object<CountryPrice_Translation>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
107
            return FieldList::create(
108
                array(
109
                    $countryDropdownField,
110
                    $withoutTranslationField
111
                )
112
            );
113
        } else {
114
            $fields->addFieldToTab(
115
                'Root.Main',
116
                $countryDropdownField,
117
                'Title'
118
            );
119
            $fields->addFieldToTab(
120
                'Root.Main',
121
                $withoutTranslationField,
122
                'Title'
123
            );
124
        }
125
        $dbFields = $this->inheritedDatabaseFields();
126
        foreach($dbFields as $dbField => $fieldType) {
127
            $useField = 'UseOriginal'.$dbField;
128
            if(!empty($this->$useField)) {
129
                $fields->replaceField(
130
                    $dbField,
131
                    $fields->dataFieldByName($dbField)->performReadonlyTransformation()
132
                );
133
            }
134
            if($fields->dataFieldByName($useField)){
135
                $fields->dataFieldByName($useField)->setDescription(_t('CountryPrice_Translation.IGNORE', 'Use original value for ') . $dbField);
136
            }
137
        }
138
        return $fields;
139
    }
140
141
    public function canCreate($member = null)
142
    {
143
        if (CountryPrice_EcommerceCountry::get_real_countries_list()->count()) {
144
            return parent::canCreate($member);
145
        }
146
        return false;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    protected function validate()
153
    {
154
        $validation = parent::validate();
155
        if ($validation->valid()) {
156
            if ($this->exists()) {
157
                $existing = CountryPrice_Translation::get()
158
                    ->exclude(array("ID" => $this->ID))
159
                    ->filter(
160
                        array(
161
                            "EcommerceCountryID" => $this->EcommerceCountryID,
0 ignored issues
show
Documentation introduced by
The property EcommerceCountryID does not exist on object<CountryPrice_Translation>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
162
                            "ParentID" => $this->ParentID
0 ignored issues
show
Documentation introduced by
The property ParentID does not exist on object<CountryPrice_Translation>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
163
                        )
164
                    );
165
                if ($existing->count() > 0) {
166
                    $validation->error(
167
                        'There is already an entry for this country and page'
168
                    );
169
                }
170
            }
171
        }
172
        return $validation;
173
    }
174
175
    /**
176
     *     'PageField' => 'Title'
177
     *     'TranslationField' => 'Title'
178
     *
179
     * @return ArrayList
180
     */
181
    public function FieldsToReplace()
182
    {
183
        $al = ArrayList::create();
184
        $al->push(
185
            ArrayData::create(
186
                array(
187
                    'PageField' => 'Title',
188
                    'TranslationField' => 'Title'
189
                )
190
            )
191
        );
192
        $al->push(
193
            ArrayData::create(
194
                array(
195
                    'PageField' => 'Content',
196
                    'TranslationField' => 'Content'
197
                )
198
            )
199
        );
200
        $this->extend('updateFieldsToReplace', $al);
201
        foreach($al as $fieldToReplace) {
202
            $ignoreField = 'UseOriginal' . $fieldToReplace->PageField;
203
            if(!empty($this->owner->$ignoreField)) {
0 ignored issues
show
Documentation introduced by
The property owner does not exist on object<CountryPrice_Translation>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
204
                $al->remove($fieldToReplace);
205
            }
206
        }
207
        return $al;
208
    }
209
210
    /**
211
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be false|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...
212
     */
213
    public function Link()
214
    {
215
        return $this->getLink();
216
    }
217
218
    /**
219
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be false|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...
220
     */
221
    public function getLink()
222
    {
223
        $link = $this->Parent()->Link();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on CountryPrice_Translation. Did you maybe mean parentClass()?

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...
224
        if ($this->EcommerceCountryID) {
0 ignored issues
show
Documentation introduced by
The property EcommerceCountryID does not exist on object<CountryPrice_Translation>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
225
            $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($link);
226
            if($hasCountrySegment){
227
                $link = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($this->EcommerceCountry()->Code, $link);
0 ignored issues
show
Documentation Bug introduced by
The method EcommerceCountry does not exist on object<CountryPrice_Translation>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
228
            }
229
            else {
230
                $link = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($this->EcommerceCountry()->Code, $link);
0 ignored issues
show
Documentation Bug introduced by
The method EcommerceCountry does not exist on object<CountryPrice_Translation>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
231
            }
232
        }
233
        return Director::absoluteURL($link);
234
    }
235
236
    public function requireDefaultRecords()
237
    {
238
        parent::requireDefaultRecords();
239
        if(Config::inst()->get('CountryPrice_Translation', 'automatically_create_dummy_translations_for_products_and_productgroups')) {
240
            $prices = CountryPrice::get();
241
            $ecommerceCountries = array();
242
            foreach($prices as $price) {
243
                if($countryObject = $price->CountryObject()) {
244
                    if($buyable = $price->Buyable()) {
245
                        if($buyable instanceof Product) {
246
                            if($buyable->ID && $countryObject->ID) {
247
                                $filter = array(
248
                                    'EcommerceCountryID' => $countryObject->ID,
249
                                    'ParentID' => $buyable->ID
250
                                );
251
                                $ecommerceCountries[$countryObject->ID] = $countryObject;
252 View Code Duplication
                                if(! CountryPrice_Translation::get()->filter($filter)->first()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
253
                                    DB::alteration_message(
254
                                        'Creating fake translation for '.$buyable->Title.' for country '.$countryObject->Code,
255
                                        'created'
256
                                    );
257
                                    $obj = CountryPrice_Translation::create($filter);
258
                                    $obj->WithoutTranslation = true;
0 ignored issues
show
Documentation introduced by
The property WithoutTranslation does not exist on object<CountryPrice_Translation>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
259
                                    $obj->write();
260
                                }
261
                            }
262
                        }
263
                    }
264
                }
265
            }
266
            if(count($ecommerceCountries)) {
267
                foreach(ProductGroup::get() as $productGroup) {
268
                    foreach($ecommerceCountries as $countryID => $countryObject) {
269
                        $filter = array(
270
                            'EcommerceCountryID' => $countryObject->ID,
271
                            'ParentID' => $productGroup->ID
272
                        );
273 View Code Duplication
                        if(! CountryPrice_Translation::get()->filter($filter)->first()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
274
                            DB::alteration_message(
275
                                'Creating fake translation for '.$productGroup->Title.' for country '.$countryObject->Code,
276
                                'created'
277
                            );
278
                            $obj = CountryPrice_Translation::create($filter);
279
                            $obj->WithoutTranslation = true;
0 ignored issues
show
Documentation introduced by
The property WithoutTranslation does not exist on object<CountryPrice_Translation>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
280
                            $obj->write();
281
                        }
282
                    }
283
                }
284
            }
285
        }
286
    }
287
288
}
289