Completed
Push — master ( 44db84...1717c1 )
by
unknown
02:20
created

Distributor::setupUser()   D

Complexity

Conditions 9
Paths 35

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 4.909
c 0
b 0
f 0
cc 9
eloc 24
nc 35
nop 0
1
<?php
2
3
/**
4
 * A country can only be sent goods to from 1 distributor + the default distributor which can send anywhere in the world.
5
 * The default distributor shows prices of the default currency.
6
 * Precondition : There is always a default distributor.
7
 */
8
class Distributor extends DataObject implements PermissionProvider
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...
9
{
10
    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...
11
        'Name' => 'Varchar(255)',
12
        'IsDefault' => 'Boolean',
13
        'Email' => 'Varchar(200)',
14
        'Address1' => "Varchar((255)",
15
        'Address2' => "Varchar(255)",
16
        'Address3' => "Varchar(255)",
17
        'Address4' => "Varchar(255)",
18
        'Address5' => "Varchar(255)",
19
        'Phone' => "Varchar(50)",
20
        'DisplayEmail' => "Varchar(50)",
21
        'WebAddress' => "Varchar(255)",
22
        'DeliveryCostNote' => 'Varchar(255)',
23
        'ShippingEstimation' => 'Varchar(255)',
24
        'ReturnInformation' => 'Varchar(255)'
25
    );
26
27
    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...
28
        'PrimaryCountry' => 'EcommerceCountry'
29
    );
30
31
    private static $has_many = 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_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...
32
        'Countries' => 'EcommerceCountry',
33
        'Members' => 'Member',
34
        'Updates' => 'CountryPrice_DistributorManagementTool_Log'
35
    );
36
37
    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...
38
        'IsDefault' => 'Is Default Distributor?'
39
    );
40
41
    private static $field_labels_right = array(
0 ignored issues
show
Unused Code introduced by
The property $field_labels_right 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
        'IsDefault' => 'Use this only for the  distributor that is applicable when no other distributor applies (e.g. a country that does not have a distributor).',
43
        'Phone' => 'Please format as +64 8 555 5555'
44
    );
45
46
    private static $extensions = array("Versioned('Stage')");
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 $extensions 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...
47
48
    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...
49
        "Name" => "Name",
50
        "IsDefault.Nice" => "Default"
51
    );
52
53
    private static $default_sort = 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 $default_sort 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...
54
        "IsDefault" => "DESC",
55
        "Name" => "Asc"
56
    );
57
58
59
    private static $singular_name = "Distributor";
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 $singular_name 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...
60
61
    /**
62
     * Return the translated Singular name.
63
     *
64
     * @return string
65
     */
66
    public function i18n_singular_name()
67
    {
68
        return _t('Distributor.SINGULAR_NAME', 'Distributor');
69
    }
70
71
72
    private static $plural_name = "Distributors";
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 $plural_name 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...
73
74
    /**
75
     * Return the translated Singular name.
76
     *
77
     * @return string
78
     */
79
    public function i18n_plural_name()
80
    {
81
        return _t('Distributor.PLURAL_NAME', 'Distributors');
82
    }
83
84
85
    /**
86
     * returns the Distributor for the country OR the default Distributor.
87
     * @param String $countryCode = the country code.
88
     *
89
     * @return Distributor
90
     */
91
    public static function get_one_for_country($countryCode = '')
92
    {
93
        $countryObject = CountryPrice_EcommerceCountry::get_real_country($countryCode);
94
        if ($countryObject) {
95
            $distributor = $countryObject->Distributor();
96
            if ($distributor && $distributor->exists()) {
97
                return $distributor;
98
            }
99
        }
100
        return self::get_default_distributor();
101
    }
102
103
    public static function get_default_distributor()
104
    {
105
        return  DataObject::get_one('Distributor', array("IsDefault" => 1));
106
    }
107
108
    public function getCMSFields()
109
    {
110
        $fields = parent::getCMSFields();
111
        $fieldLabels = $this->FieldLabels();
0 ignored issues
show
Unused Code introduced by
$fieldLabels 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...
112
        $fieldLabelsRight = $this->Config()->get("field_labels_right");
113
        $listOfCountriesCovered = EcommerceCountry::get()->exclude(array("DistributorID" => 0))->map("ID", "Title");
114
        //secondary for another country
115
        $fields->removeByName('Versions');
116
        if ($listOfCountriesCovered && $listOfCountriesCovered->count()) {
117
            $countryArray =  array(" -- please select --") + $listOfCountriesCovered->toArray();
118
            $fields->addFieldToTab("Root.CountryDetails", DropdownField::create("PrimaryCountryID", "Primary Country", $countryArray));
119
        } else {
120
            $fields->removeByName('PrimaryCountryID');
121
        }
122
        if ($this->IsDefault) {
0 ignored issues
show
Documentation introduced by
The property IsDefault does not exist on object<Distributor>. 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...
123
            $fields->removeByName('Countries');
124
        } else {
125
            if (empty($this->ID)) {
126
                $id = 0;
0 ignored issues
show
Unused Code introduced by
$id 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...
127
            } else {
128
                $id = $this->ID;
0 ignored issues
show
Unused Code introduced by
$id 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...
129
            }
130
            if ($this->ID) {
131
                $config = GridFieldConfig_RelationEditor::create();
132
                $config->removeComponentsByType("GridFieldAddNewButton");
133
                $gridField = new GridField('pages', 'All pages', SiteTree::get(), $config);
0 ignored issues
show
Unused Code introduced by
$gridField 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
                $countryField = new GridField(
135
                    'Countries',
136
                    'Countries',
137
                    $this->Countries(),
0 ignored issues
show
Documentation Bug introduced by
The method Countries does not exist on object<Distributor>? 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...
138
                    $config
139
                );
140
                $fields->addFieldToTab("Root.CountryDetails", $countryField);
141
                if ($this->Version > 1) {
0 ignored issues
show
Documentation introduced by
The property Version does not exist on object<Distributor>. 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...
142
                    $columns = array(
143
                        'Version' => 'Version',
144
                        'LastEdited' => 'Date',
145
                        'Name' => 'Name',
146
                        'IsDefault' => 'Default',
147
                        'Email' => 'Email'
148
                    );
149
                    $table = '<table class="versions"><thead><tr><th>' . implode('</th><th>', $columns) . '</th></tr></thead><tbody>';
150
                    $version = $this->Version - 1;
0 ignored issues
show
Documentation introduced by
The property Version does not exist on object<Distributor>. 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...
151
                    while ($version > 0) {
152
                        $versionDO = Versioned::get_version('Distributor', $this->ID, $version--);
153
                        $values = array();
154
                        foreach ($columns as $column => $title) {
155
                            $values[] = $versionDO->$column;
156
                        }
157
                        $table .= '<tr><td>' . implode('</td><td>', $values) . '</td></tr>';
158
                    }
159
                    $table .= '</tbody></table>';
160
                    $table .= "<style type=\"text/css\">
161
                                table.versions {border: 1px solid black; width:100%; border-collapse: collapse;}
162
                                table.versions tr {border: 1px solid black;}
163
                                table.versions tr th, table.versions tr td {border: 1px solid black; width: auto; text-align: center;}
164
                               </style>";
165
                    $fields->addFieldToTab('Root.Versions', new LiteralField('VersionsTable', $table));
166
                }
167
            }
168
        }
169
        $fields->addFieldsToTab(
170
            "Root.ContactDetails",
171
            array(
172
                new TextField("DisplayEmail"),
173
                new TextField("Phone"),
174
                new TextField("Address1"),
175
                new TextField("Address2"),
176
                new TextField("Address3"),
177
                new TextField("Address4"),
178
                new TextField("Address5")
179
            )
180
        );
181
        $fields->addFieldsToTab(
182
            "Root.EcomInfo",
183
            array(
184
                new TextField("DeliveryCostNote"),
185
                new TextField("ShippingEstimation"),
186
                new TextField("ReturnInformation"),
187
                new HTMLEditorField("ProductNotAvailableNote")
188
            )
189
        );
190
        foreach ($fieldLabelsRight as $key => $value) {
191
            $field = $fields->dataFieldByName($key);
192
            if ($field) {
193
                $field->setRightTitle($value);
194
            }
195
        }
196
        $fields->removeFieldFromTab(
197
            'Root',
198
            'Countries'
199
        );
200
        return $fields;
201
    }
202
203
    /**
204
     * returns EcommerceCountries that this Distributor is responsible for.
205
     * return ArrayList
206
     */
207
    public function getCountryList()
208
    {
209
        $filter = $this->IsDefault ? array("ID" => 0) : array("DistributorID" => $this->ID);
0 ignored issues
show
Documentation introduced by
The property IsDefault does not exist on object<Distributor>. 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...
Unused Code introduced by
$filter 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...
210
        return EcommerceCountry::get()
211
            ->filter($array);
0 ignored issues
show
Bug introduced by
The variable $array does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
212
    }
213
214
    /**
215
     * link to edit the record
216
     * @param String | Null $action - e.g. edit
217
     * @return String
218
     */
219
    public function CMSEditLink($action = null)
220
    {
221
        return Controller::join_links(
222
            Director::baseURL(),
223
            "/admin/shop/".$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/",
224
            $action
225
        );
226
    }
227
228
    /**
229
     * ensure there is one default Distributor.
230
     *
231
     */
232
    public function onBeforeWrite()
233
    {
234
        parent::onBeforeWrite();
235
        if (Distributor::get()->filter(array("IsDefault" => 1))->count() == 0) {
236
            $this->IsDefault = 1;
0 ignored issues
show
Documentation introduced by
The property IsDefault does not exist on object<Distributor>. 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...
237
        }
238
        if ($this->PrimaryCountryID > 0 && EcommerceCountry::get()->byID(intval($this->PrimaryCountryID))) {
0 ignored issues
show
Documentation introduced by
The property PrimaryCountryID does not exist on object<Distributor>. 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...
239
            $primaryCountry = $this->PrimaryCountry();
0 ignored issues
show
Documentation Bug introduced by
The method PrimaryCountry does not exist on object<Distributor>? 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...
240
            if (! $this->Countries()->byID($this->PrimaryCountryID)) {
0 ignored issues
show
Documentation introduced by
The property PrimaryCountryID does not exist on object<Distributor>. 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...
Documentation Bug introduced by
The method Countries does not exist on object<Distributor>? 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...
241
                $this->Countries()->add($primaryCountry);
0 ignored issues
show
Documentation Bug introduced by
The method Countries does not exist on object<Distributor>? 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...
242
            }
243
        } else {
244
            if ($firstCountry = $this->Countries()->First()) {
0 ignored issues
show
Documentation Bug introduced by
The method Countries does not exist on object<Distributor>? 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...
245
                self::$_ran_after_write = true;
246
                $this->PrimaryCountryID = $firstCountry->ID;
0 ignored issues
show
Documentation introduced by
The property PrimaryCountryID does not exist on object<Distributor>. 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...
247
            }
248
        }
249
    }
250
251
    private static $_ran_after_write = false;
252
    /**
253
     * ensure there is one default Distributor.
254
     *
255
     */
256
    public function onAfterWrite()
257
    {
258
        parent::onAfterWrite();
259
        if (! self::$_ran_after_write) {
260
            self::$_ran_after_write = true;
261
            $this->setupUser();
262
        }
263
    }
264
265
    /**
266
     * @param Member $member
0 ignored issues
show
Documentation introduced by
Should the type for parameter $member not be Member|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
267
     * @return Boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string|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...
268
     */
269
    public function canDelete($member = null)
270
    {
271
        return $this->IsDefault ? false : parent::canEdit($member);
0 ignored issues
show
Documentation introduced by
The property IsDefault does not exist on object<Distributor>. 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...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (canEdit() instead of canDelete()). Are you sure this is correct? If so, you might want to change this to $this->canEdit().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
272
    }
273
274
    public function requireDefaultRecords()
275
    {
276
        parent::RequireDefaultRecords();
277
        $distributorTitleSingular = _t('Distributor.SINGULAR_NAME', 'Distributor');
278
        $distributorTitlePlural = _t('Distributor.PLURAL_NAME', 'Distributors');
279
        $filter = array("Title" => $distributorTitleSingular);
280
        $role = PermissionRole::get()->filter($filter)->first();
281 View Code Duplication
        if (!$role) {
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...
282
            $role = PermissionRole::create($filter);
283
            $role->write();
284
            DB::alteration_message("Creating ".$distributorTitleSingular." role", 'created');
285
        }
286
        $codes = array(
287
            'CMS_ACCESS_SalesAdmin',
288
            'CMS_ACCESS_SalesAdminExtras',
289
            'CMS_ACCESS_SalesAdmin_PROCESS'
290
        );
291
        foreach ($codes as $code) {
292
            $filter = array(
293
                "RoleID" => $role->ID,
294
                "Code" => $code
295
            );
296
            $code = PermissionRoleCode::get()->filter($filter)->first();
297 View Code Duplication
            if (!$code) {
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...
298
                DB::alteration_message("Adding code to ".$distributorTitleSingular." role", 'created');
299
                $code = PermissionRoleCode::create($filter);
300
                $code->write();
301
            }
302
        }
303
304
        $distributorGroup = self::get_distributor_group();
305
        $distributorPermissionCode = Config::inst()->get('Distributor', 'distributor_permission_code');
306
        if (!$distributorGroup) {
307
            $distributorGroup = new Group();
308
            $distributorGroup->Code = $distributorPermissionCode;
309
            $distributorGroup->Title = $distributorTitlePlural;
310
            $distributorGroup->write();
311
            DB::alteration_message($distributorTitlePlural.' Group created', "created");
312
            Permission::grant($distributorGroup->ID, $distributorPermissionCode);
313
        } elseif (DB::query("SELECT * FROM \"Permission\" WHERE \"GroupID\" = '".$distributorGroup->ID."' AND \"Code\" LIKE '".$distributorPermissionCode."'")->numRecords() == 0) {
314
            Permission::grant($distributorGroup->ID, $distributorPermissionCode);
315
            DB::alteration_message($distributorTitlePlural.' group permissions granted', "created");
316
        }
317
        $distributorGroup->Roles()->add($role);
318
        $distributorGroup = self::get_distributor_group();
319
        if (!$distributorGroup) {
320
            user_error("could not create user group");
321
        } else {
322
            DB::alteration_message('distributor group is ready for use');
323
        }
324
        $distributors = Distributor::get();
325
        if ($distributors && $distributors->count()) {
326
            foreach ($distributors as $distributor) {
327
                $distributor->setupUser();
328
            }
329
        }
330
    }
331
332
333
    /**
334
     * @return DataObject (Group)
0 ignored issues
show
Documentation introduced by
Should the return type not be DataObject|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...
335
     **/
336
    public static function get_distributor_group()
337
    {
338
        $distributorPermissionCode = Config::inst()->get('Distributor', 'distributor_permission_code');
339
        return Group::get()
340
            ->where("\"Code\" = '".$distributorPermissionCode."'")
341
            ->First();
342
    }
343
344
345
    /**
346
     * @var string
347
     */
348
    private static $distributor_permission_code = "distributors";
0 ignored issues
show
Unused Code introduced by
The property $distributor_permission_code 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...
349
350
    /**
351
     * {@inheritdoc}
352
     */
353
    public function providePermissions()
354
    {
355
        return array(
356
            $perms[Config::inst()->get('Distributor', 'distributor_permission_code')] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$perms was never initialized. Although not strictly required by PHP, it is generally a good practice to add $perms = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
357
                'name' => _t('Distributor.PLURAL_NAME', 'Distributors'),
358
                'category' => 'E-commerce',
359
                'help' => _t('Distributor.SINGULAR_NAME', 'Distributor').' access to relevant products and sales data.',
360
                'sort' => 98,
361
            )
362
        );
363
    }
364
365
    public function setupUser()
366
    {
367
        $group = Group::get()->filter(array("Code" => $this->Config()->get('distributor_permission_code')))->first();
368
        if ($this->Email) {
0 ignored issues
show
Documentation introduced by
The property Email does not exist on object<Distributor>. 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...
369
            $filter = array("Email" => $this->Email);
0 ignored issues
show
Documentation introduced by
The property Email does not exist on object<Distributor>. 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...
370
            $member = Member::get()
371
                ->filter($filter)
372
                ->First();
373
            if (!$member) {
374
                $member = Member::create($filter);
375
                //$thisMember->SetPassword = substr(session_id, 0, 8);
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
376
            }
377
            $member->FirstName = trim(_t('Distributor.SINGULAR_NAME', 'Distributor') . _t('Distributor.FOR', ' for '));
378
            $member->Surname = $this->Name;
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<Distributor>. 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...
379
            $member->DistributorID = $this->ID;
380
            $member->write();
381
            if ($group) {
382
                $member->addToGroupByCode($group->Code);
383
            }
384
            $member->write();
385
        }
386
        if ($group) {
387
            foreach ($group->Members() as $oldGroupMember) {
388
                $distributor = $oldGroupMember->Distributor();
389
                if ($distributor && $distributor->exists()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
390
                } else {
391
                    $group->Members()->remove($oldGroupMember);
392
                }
393
            }
394
            foreach ($this->Members() as $member) {
0 ignored issues
show
Documentation Bug introduced by
The method Members does not exist on object<Distributor>? 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...
395
                $member->addToGroupByCode($group->Code);
396
            }
397
        }
398
    }
399
}
400