OptionGroup::canCreate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 2
crap 1
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
 * Class OptionGroup
12
 * @package Dynamic\FoxyStripe\Model
13
 *
14
 * @property \SilverStripe\ORM\FieldType\DBVarchar Title
15
 *
16
 * @method \SilverStripe\ORM\HasManyList Options
17
 */
18
class OptionGroup extends DataObject
19
{
20
    /**
21
     * @var array
22
     */
23
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
24
        'Title' => 'Varchar(100)',
25
    );
26
27
    /**
28
     * @var array
29
     */
30
    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...
31
        'Options' => OptionItem::class,
32
    );
33
34
    /**
35
     * @var string
36
     */
37
    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...
38
39
    /**
40
     * @var string
41
     */
42
    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...
43
44
    /**
45
     * @var string
46
     */
47
    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...
48
49
    /**
50
     * @var string
51
     */
52
    private static $table_name = 'OptionGroup';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
53
54
    /**
55
     * @throws \SilverStripe\ORM\ValidationException
56
     */
57
    public function requireDefaultRecords()
58
    {
59
        parent::requireDefaultRecords();
60
        // create a catch-all group
61
        if (!self::get()->filter(array('Title' => 'Options'))->first()) {
62
            $do = new self();
63
            $do->Title = 'Options';
0 ignored issues
show
Documentation Bug introduced by
It seems like 'Options' of type string is incompatible with the declared type SilverStripe\ORM\FieldType\DBVarchar of property $Title.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
            $do->write();
65
        }
66
        if (!self::get()->filter(array('Title' => 'Size'))->first()) {
67
            $do = new self();
68
            $do->Title = 'Size';
69
            $do->write();
70
        }
71
        if (!self::get()->filter(array('Title' => 'Color'))->first()) {
72
            $do = new self();
73
            $do->Title = 'Color';
74
            $do->write();
75
        }
76
        if (!self::get()->filter(array('Title' => 'Type'))->first()) {
77
            $do = new self();
78
            $do->Title = 'Type';
79
            $do->write();
80
        }
81
    }
82
83
    /**
84
     * @return RequiredFields
85
     */
86 1
    public function getCMSValidator()
87
    {
88 1
        return new RequiredFields(array('Title'));
89
    }
90
91
    /**
92
     * @return ValidationResult
93
     */
94 13
    public function validate()
95
    {
96 13
        $result = parent::validate();
97
98 13
        $title = $this->Title;
99 13
        $firstChar = substr($title, 0, 1);
100 13
        if (preg_match('/[^a-zA-Z]/', $firstChar)) {
101 1
            $result->addError('The first character of the Title can only be a letter', 'bad');
102
        }
103 13
        if (preg_match('/[^a-zA-Z]\s/', $title)) {
104
            $result->addError('Please only use letters, numbers and spaces in the title', 'bad');
105
        }
106
107 13
        return $result;
108
    }
109
110
    /**
111
     * @throws \SilverStripe\ORM\ValidationException
112
     */
113 2
    public function onBeforeDelete()
114
    {
115 2
        parent::onBeforeDelete();
116
117
        //make sure that if we delete this option group, we reassign the group's option items to the 'None' group.
118 2
        $items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID));
119
120 2
        if (isset($items)) {
121 2
            if ($noneGroup = self::get()->filter(array('Title' => 'Options'))->first()) {
122
                /** @var OptionItem $item */
123 2
                foreach ($items as $item) {
124 1
                    $item->ProductOptionGroupID = $noneGroup->ID;
125 1
                    $item->write();
126
                }
127
            }
128
        }
129
    }
130
131
    /**
132
     * @param bool $member
133
     *
134
     * @return bool
135
     */
136 21
    public function canView($member = null)
137
    {
138 21
        return true;
139
    }
140
141
    /**
142
     * @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...
143
     *
144
     * @return bool|int
145
     */
146 3
    public function canEdit($member = null)
147
    {
148 3
        switch ($this->Title) {
149 3
            case 'Options':
150 2
                return false;
151
                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...
152
            default:
153 3
                return Permission::check('Product_CANCRUD', 'any', $member);
154
                break;
155
        }
156
    }
157
158
    /**
159
     * @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...
160
     *
161
     * @return bool|int
162
     */
163 2
    public function canDelete($member = null)
164
    {
165 2
        return $this->canEdit($member);
166
    }
167
168
    /**
169
     * @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...
170
     * @param array $context
171
     *
172
     * @return bool|int
173
     */
174 1
    public function canCreate($member = null, $context = [])
175
    {
176 1
        return Permission::check('Product_CANCRUD', 'any', $member);
177
    }
178
}
179