Passed
Push — 1.0 ( f15436...44527a )
by Morven
01:46
created

Zone::getCountriesList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverCommerce\GeoZones\Model;
4
5
use Locale;
6
use ZoneMigrationTask;
7
use SilverStripe\i18n\i18n;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Forms\ListboxField;
10
use SilverStripe\Forms\DropdownField;
11
use SilverStripe\SiteConfig\SiteConfig;
12
13
/**
14
 * A container of multiple regions 
15
 * 
16
 */
17
class Zone extends DataObject
18
{
19
    private static $table_name = 'GeoZoneZone';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
20
21
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
22
        "Name" => "Varchar",
23
        "Country" => "Varchar",
24
        "AllRegions" => "Boolean",
25
        "Enabled" => "Boolean"
26
    ];
27
28
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
29
        "Site" => SiteConfig::class
30
    ];
31
32
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
33
        "Regions" => Region::class
34
    ];
35
36
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
37
        "CountriesList" => "Varchar"
38
    ];
39
40
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
41
        "Name",
42
        "CountriesList",
43
        "Regions.Count",
44
        "Enabled"
45
    ];
46
47
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
48
        "Name",
49
        "Country",
50
        "Regions.Name",
51
        "Regions.Code",
52
        "Enabled"
53
    ];
54
55
	/**
56
	 * {@inheritdoc}
57
	 */
58
    public function populateDefaults()
59
    {
60
        parent::populateDefaults();
61
62
        $current_region = Locale::getRegion(i18n::get_locale());
63
        $this->Country = i18n::get_locale($current_region);
0 ignored issues
show
Unused Code introduced by
The call to SilverStripe\i18n\i18n::get_locale() has too many arguments starting with $current_region. ( Ignorable by Annotation )

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

63
        /** @scrutinizer ignore-call */ 
64
        $this->Country = i18n::get_locale($current_region);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug Best Practice introduced by
The property Country does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
64
    }
65
66
    /**
67
     * Return an array of all associated countries
68
     * 
69
     * @return array
70
     */
71
    public function getCountriesArray()
72
    {
73
        $return = json_decode($this->Country);
74
75
        if (empty($return) && isset($this->Country)) {
76
            $return = [$this->Country];
77
        }
78
79
        return $return;
80
    }
81
82
    /**
83
     * Return a simple, comma seperated list of associated countries
84
     * 
85
     * @return string
86
     */
87
    public function getCountriesList()
88
    {
89
        return implode(",", $this->getCountriesArray());
90
    }
91
92
	/**
93
	 * {@inheritdoc}
94
	 */    
95
    public function getCMSFields()
96
    {
97
        $this->beforeUpdateCMSFields(function ($fields) {
98
            $fields->replaceField(
99
                "Country",
100
                ListboxField::create(
101
                    'Country',
102
                    $this->fieldLabel("Country"),
103
                    array_change_key_case(
104
                        i18n::getData()->getCountries(),
105
                        CASE_UPPER
106
                    )
107
                )
108
            );
109
        });
110
111
        return parent::getCMSFields();
112
    }
113
114
	/**
115
	 * {@inheritdoc}
116
	 */
117
	public function requireDefaultRecords() {
118
        parent::requireDefaultRecords();
119
120
		if(ZoneMigrationTask::config()->run_during_dev_build) {
121
			$task = new ZoneMigrationTask();
122
			$task->up();
123
		}
124
	}
125
126
	/**
127
	 * {@inheritdoc}
128
	 */
129
    public function onAfterWrite()
130
    {
131
        parent::onAfterWrite();
132
133
        // If this applies to all regions in the country,
134
        // then add them all on save
135
        if ($this->AllRegions && isset($this->Country)) {
0 ignored issues
show
Bug Best Practice introduced by
The property AllRegions does not exist on SilverCommerce\GeoZones\Model\Zone. Since you implemented __get, consider adding a @property annotation.
Loading history...
136
            foreach ($this->getCountriesArray() as $country) {
137
                $regions = Region::get()
138
                    ->filter("CountryCode", $country);
139
                
140
                foreach ($regions as $region) {
141
                    $this
142
                        ->Regions()
0 ignored issues
show
Bug introduced by
The method Regions() does not exist on SilverCommerce\GeoZones\Model\Zone. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

142
                        ->/** @scrutinizer ignore-call */ 
143
                          Regions()
Loading history...
143
                        ->add($region);
144
                }
145
            }
146
        }
147
    }
148
}