Passed
Push — 1.0 ( 2f087f...a893c6 )
by Morven
03:16 queued 02:03
created

SiteConfigExtension::updateCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 9.4109
c 0
b 0
f 0
cc 1
eloc 37
nc 1
nop 1

How to fix   Long Method   

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
namespace SilverCommerce\Settings\Extensions;
4
5
use SilverStripe\i18n\i18n;
6
use SilverStripe\Assets\Image;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\TextField;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\Forms\TextareaField;
11
use SilverStripe\Forms\DropdownField;
12
use SilverStripe\Forms\CheckboxField;
13
use SilverStripe\Forms\ToggleCompositeField;
14
use SilverStripe\AssetAdmin\Forms\UploadField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\AssetAdmin\Forms\UploadField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
class SiteConfigExtension extends DataExtension
17
{
18
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
        "SiteLocale" => "Varchar(5)",
20
        "ContactPhone" => "Varchar(15)",
21
        "ContactEmail" => "Varchar(255)",
22
        "ContactAddress" => "Text",
23
        "ShowPriceAndTax" => "Boolean",
24
        "ShowPriceTaxString" => "Boolean"
25
    ];
26
27
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
28
        "CardLogos" => Image::class
29
    ];
30
31
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
32
        "InlineContactAddress" => "Text",
33
        "TrimmedContactPhone" => "Varchar(15)"
34
    ];
35
36
    public function getInlineContactAddress()
37
    {
38
        return trim(preg_replace('/\s\s+/', ', ', $this->owner->ContactAddress));
39
    }
40
41
    public function getTrimmedContactPhone()
42
    {
43
        return trim(str_replace(" ","",$this->owner->ContactPhone));
44
    }
45
46
    public function updateCMSFields(FieldList $fields)
47
    {
48
        $fields->removeByName("ContactPhone");
49
        $fields->removeByName("ContactEmail");
50
        $fields->removeByName("ContactAddress");
51
        $fields->removeByName("SiteLocale");
52
        $fields->removeByName("ShowPriceAndTax");
53
        $fields->removeByName("ShowPriceTaxString");
54
55
        $fields->addFieldsToTab(
56
            "Root.Main",
57
            [
58
                DropdownField::create(
59
                    "SiteLocale",
0 ignored issues
show
Bug introduced by
'SiteLocale' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
                    /** @scrutinizer ignore-type */ "SiteLocale",
Loading history...
60
                    $this->owner->fieldLabel("SiteLocale"),
61
                    i18n::getSources()->getKnownLocales()
62
                ),
63
                TextField::create(
64
                    "ContactPhone",
65
                    $this->owner->fieldLabel("ContactPhone")
66
                ),
67
                TextField::create(
68
                    "ContactEmail",
69
                    $this->owner->fieldLabel("ContactEmail")
70
                ),
71
                TextareaField::create(
72
                    "ContactAddress",
73
                    $this->owner->fieldLabel("ContactAddress")
74
                )
75
            ]
76
        );
77
78
        $fields->addFieldToTab(
79
            "Root.Shop",
80
            ToggleCompositeField::create(
81
                'MiscSettings',
82
                _t("Settings.MiscSettings", "Misc Settings"),
83
                [
84
                    CheckboxField::create("ShowPriceAndTax")
85
                        ->setDescription(_t(
86
                            "SilverCommerce\Settings.ShowPriceAndTaxDescription",
87
                            "Show product prices including tax"
88
                        )),
89
                    CheckboxField::create("ShowPriceTaxString")
90
                        ->setDescription(_t(
91
                            "SilverCommerce\Settings.ShowProductTaxStringDescription",
92
                            "Show 'inc/exc TAX' after price"
93
                        )),
94
                    UploadField::create(
95
                        "CardLogos",
96
                        $this->owner->fieldLabel("CardLogos")
97
                    )
98
                ]
99
            )
100
        );
101
    }
102
103
    public function onBeforeWrite()
104
    {
105
        if (empty($this->owner->SiteLocale)) {
106
            $this->owner->SiteLocale = i18n::config()->default_locale;
107
        }
108
    }
109
}