Completed
Push — master ( f8f77e...d2a4ab )
by Jason
05:01
created

OptionGroup::requireDefaultRecords()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
ccs 0
cts 18
cp 0
rs 8.5906
cc 5
eloc 17
nc 16
nop 0
crap 30
1
<?php
2
3
namespace Dynamic\FoxyStripe\Model;
4
5
use SilverStripe\Forms\RequiredFields;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\ValidationResult;
8
use SilverStripe\Security\Permission;
9
10
/**
11
 *
12
 */
13
class OptionGroup extends DataObject
14
{
15
    /**
16
     * @var array
17
     */
18
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
        'Title' => 'Varchar(100)',
20
    );
21
22
    /**
23
     * @var array
24
     */
25
    private static $has_many = array(
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
26
        'Options' => OptionItem::class,
27
    );
28
29
    /**
30
     * @var string
31
     */
32
    private static $singular_name = 'Product Option Group';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
33
34
    /**
35
     * @var string
36
     */
37
    private static $plural_name = 'Product Option Groups';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
38
39
    /**
40
     * @var string
41
     */
42
    private static $description = 'Groups of product options, e.g. size, color, etc';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
43
44
    /**
45
     * @var string
46
     */
47
    private static $table_name = 'FS_OptionGroup';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
48
49
    /**
50
     *
51
     */
52
    public function requireDefaultRecords()
53
    {
54
        parent::requireDefaultRecords();
55
        // create a catch-all group
56
        if (!self::get()->filter(array('Title' => 'Options'))->first()) {
57
            $do = new self();
58
            $do->Title = 'Options';
0 ignored issues
show
Bug Best Practice introduced by
The property Title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
            $do->write();
60
        }
61
        if (!self::get()->filter(array('Title' => 'Size'))->first()) {
62
            $do = new self();
63
            $do->Title = 'Size';
64
            $do->write();
65
        }
66
        if (!self::get()->filter(array('Title' => 'Color'))->first()) {
67
            $do = new self();
68
            $do->Title = 'Color';
69
            $do->write();
70
        }
71
        if (!self::get()->filter(array('Title' => 'Type'))->first()) {
72
            $do = new self();
73
            $do->Title = 'Type';
74
            $do->write();
75
        }
76
    }
77
78
    /**
79
     * @return RequiredFields
80
     */
81 1
    public function getCMSValidator()
82
    {
83 1
        return new RequiredFields(array('Title'));
84
    }
85
86
    /**
87
     * @return ValidationResult
88
     */
89 13
    public function validate()
90
    {
91 13
        $result = parent::validate();
92
93 13
        $title = $this->Title;
94 13
        $firstChar = substr($title, 0, 1);
95 13
        if (preg_match('/[^a-zA-Z]/', $firstChar)) {
96 1
            $result->addError('The first character of the Title can only be a letter', 'bad');
97
        }
98 13
        if (preg_match('/[^a-zA-Z]\s/', $title)) {
99
            $result->addError('Please only use letters, numbers and spaces in the title', 'bad');
100
        }
101
102 13
        return $result;
103
    }
104
105
    /**
106
     *
107
     */
108 2
    public function onBeforeDelete()
109
    {
110 2
        parent::onBeforeDelete();
111
112
        //make sure that if we delete this option group, we reassign the group's option items to the 'None' group.
113 2
        $items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID));
114
115 2
        if (isset($items)) {
116 2
            if ($noneGroup = self::get()->filter(array('Title' => 'Options'))->first()) {
117 2
                foreach ($items as $item) {
118 1
                    $item->ProductOptionGroupID = $noneGroup->ID;
119 1
                    $item->write();
120
                }
121
            }
122
        }
123
    }
124
125
    /**
126
     * @param bool $member
127
     *
128
     * @return bool
129
     */
130 49
    public function canView($member = false)
131
    {
132 49
        return true;
133
    }
134
135
    /**
136
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
137
     *
138
     * @return bool|int
139
     */
140 3
    public function canEdit($member = null)
141
    {
142 3
        switch ($this->Title) {
143 3
            case 'Options':
144 2
                return false;
145
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
146
            default:
147 3
                return Permission::check('Product_CANCRUD', 'any', $member);
148
                break;
149
        }
150
    }
151
152
    /**
153
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
154
     *
155
     * @return bool|int
156
     */
157 2
    public function canDelete($member = null)
158
    {
159 2
        return $this->canEdit($member);
160
    }
161
162
    /**
163
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
164
     *
165
     * @return bool|int
166
     */
167 1
    public function canCreate($member = null, $context = [])
168
    {
169 1
        return Permission::check('Product_CANCRUD', 'any', $member);
170
    }
171
}
172