EcommerceSecurityBaseClass   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 219
Duplicated Lines 13.7 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 29
lcom 2
cbo 9
dl 30
loc 219
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A i18n_singular_name() 0 4 1
A i18n_plural_name() 0 4 1
A find_or_create() 0 19 4
A canCreate() 0 4 1
A canView() 15 15 4
A canEdit() 15 15 4
A canDelete() 0 4 1
A getCMSFields() 0 30 2
A getType() 0 4 1
A getSimplerName() 0 4 1
A hasRisks() 0 4 4
A isSafe() 0 4 2
A hasOpinion() 0 4 2
A requireDefaultRecords() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
4
class EcommerceSecurityBaseClass 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...
5
{
6
7
8
    /**
9
     * standard SS variable
10
     * @Var String
11
     */
12
    private static $singular_name = "Blacklisted Item";
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...
13
    public function i18n_singular_name()
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...
14
    {
15
        return Config::inst()->get($this->ClassName, 'singular_name');
16
    }
17
    /**
18
     * standard SS variable
19
     * @Var String
20
     */
21
    private static $plural_name = "Blacklisted Items";
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...
22
    public function i18n_plural_name()
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...
23
    {
24
        return Config::inst()->get($this->ClassName, 'plural_name');
25
    }
26
27
    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...
28
        'Title' => 'Varchar(200)',
29
        'Status' => 'Enum("Unknown, Good, Bad", "Unknown")'
30
    );
31
32
    private static $belongs_many_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 $belongs_many_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...
33
        'SecurityChecks' => 'OrderStatusLog_SecurityCheck'
34
    );
35
36
    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...
37
        'Type' => 'Varchar',
38
        'SimplerName' => 'Varchar'
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
        'Created' => 'Created',
43
        'LastEdited.Ago' => 'Last Edit',
44
        'SimplerName' => 'Type',
45
        'Title' => 'Value',
46
        'Status' => 'Status'
47
    );
48
49
    private static $indexes = 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 $indexes 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...
50
        'ClassName_Title' => array('type' => 'unique', 'value' => '"ClassName","Title"'),
51
        'Title' => true,
52
        'ClassName' => true,
53
    );
54
55
    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...
56
        'Title' => 'Value'
57
    );
58
59
    private static $searchable_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 $searchable_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...
60
        'Title' => 'PartialMatchFilter',
61
        'ClassName' => array(
62
            'filter' => 'PartialMatchFilter',
63
            'title' => 'Type'
64
        ),
65
        'Status' => 'PartialMatchFilter'
66
    );
67
68
    private static $default_sort = 'Status DESC';
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...
69
70
    /**
71
     * filter value examples are:
72
     * ```php
73
     *     array('Title' => 'Foo')
74
     * ```
75
     * you can not provide multi-dimensional arrays
76
     *
77
     * @param  array $filterArray  associative array of filter values
78
     * @param  bool $filterArray   if a new one is created, should it be written
79
     * @return DataObject
80
     */
81
    public static function find_or_create($filterArray, $write = true)
82
    {
83
        $className = get_called_class();
84
        //we dont want empty ones so we just return a temp object...
85
        if (empty($filterArray['Title'])) {
86
            $obj = EcommerceSecurityBaseClass::create();
87
        } else {
88
            $filterArray['ClassName'] = $className;
89
            $obj = $className::get()->filter($filterArray)->first();
90
            if (! $obj) {
91
                $obj = $className::create($filterArray);
92
                if ($write) {
93
                    $obj->write();
94
                }
95
            }
96
        }
97
98
        return $obj;
99
    }
100
101
    public function canCreate($member = null)
102
    {
103
        return true;
104
    }
105
106 View Code Duplication
    public function canView($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
107
    {
108
        if (! $member) {
109
            $member = Member::currentUser();
110
        }
111
        $extended = $this->extendedCan(__FUNCTION__, $member);
112
        if ($extended !== null) {
113
            return $extended;
114
        }
115
        if (Permission::checkMember($member, Config::inst()->get('EcommerceRole', 'admin_permission_code'))) {
116
            return true;
117
        }
118
119
        return parent::canView($member);
120
    }
121
122 View Code Duplication
    public function canEdit($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
123
    {
124
        if (! $member) {
125
            $member = Member::currentUser();
126
        }
127
        $extended = $this->extendedCan(__FUNCTION__, $member);
128
        if ($extended !== null) {
129
            return $extended;
130
        }
131
        if (Permission::checkMember($member, Config::inst()->get('EcommerceRole', 'admin_permission_code'))) {
132
            return true;
133
        }
134
135
        return parent::canEdit($member);
136
    }
137
138
    public function canDelete($member = null)
139
    {
140
        return false;
141
    }
142
143
    /**
144
     * CMS Fields
145
     * @return FieldList
146
     */
147
    public function getCMSFields()
148
    {
149
        $fields = parent::getCMSFields();
150
        if ($this->exists()) {
151
            $labels = $this->fieldLabels();
152
            $fields->addFieldToTab(
153
                'Root.Main',
154
                $type = ReadonlyField::create('Type', 'Type', $labels['Title']),
155
                'Title'
156
            );
157
            $fields->replaceField(
158
                'Title',
159
                $fields->dataFieldByName('Title')->setTitle($labels["Title"])->performReadonlyTransformation()
160
            );
161
        } else {
162
            $availableClasses = ClassInfo::subclassesFor($this->ClassName);
163
            unset($availableClasses[$this->ClassName]);
164
            $fields->addFieldToTab(
165
                'Root.Main',
166
                EcommerceClassNameOrTypeDropdownField::create(
167
                    'ClassName',
168
                    'Type',
169
                    'EcommerceSecurityBaseClass',
170
                    $availableClasses
171
                )->addExtraClass('dropdown')
172
            );
173
            $fields->dataFieldByName('Title')->setTitle('Value');
174
        }
175
        return $fields;
176
    }
177
178
    public function getType()
179
    {
180
        return $this->singular_name();
181
    }
182
183
    public function getSimplerName()
184
    {
185
        return str_replace('Blacklisted ', '', $this->singular_name());
186
    }
187
188
    /**
189
     *
190
     *
191
     * @return bool
192
     */
193
    public function hasRisks()
194
    {
195
        return $this->Title && $this->ID && $this->Status == 'Bad' ? true : false;
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<EcommerceSecurityBaseClass>. 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 introduced by
The property Status does not exist on object<EcommerceSecurityBaseClass>. 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...
196
    }
197
198
    /**
199
     *
200
     *
201
     * @return bool
202
     */
203
    public function isSafe()
204
    {
205
        return $this->Status == 'Good' ? true : false;
0 ignored issues
show
Documentation introduced by
The property Status does not exist on object<EcommerceSecurityBaseClass>. 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...
206
    }
207
208
    /**
209
     *
210
     *
211
     * @return bool
212
     */
213
    public function hasOpinion()
214
    {
215
        return $this->Status !== 'Unknown' ? true : false;
0 ignored issues
show
Documentation introduced by
The property Status does not exist on object<EcommerceSecurityBaseClass>. 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...
216
    }
217
218
    public function requireDefaultRecords()
219
    {
220
        parent::requireDefaultRecords();
221
    }
222
}
223