Completed
Pull Request — master (#291)
by Jason
08:51
created

OptionGroup::validate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 11
cp 0.8182
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 0
crap 3.054
1
<?php
2
/**
3
 *
4
 * @package FoxyStripe
5
 *
6
 */
7
8
class OptionGroup extends DataObject
9
{
10
    /**
11
     * @var array
12
     */
13
    private static $db = array(
14
        'Title' => 'Varchar(100)'
15
    );
16
17
    /**
18
     * @var array
19
     */
20
    private static $has_many = array(
21
        'Options' => 'OptionItem',
22
    );
23
24
    /**
25
     * @var string
26
     */
27
    private static $singular_name = 'Product Option Group';
28
29
    /**
30
     * @var string
31
     */
32
    private static $plural_name = 'Product Option Groups';
33
34
    /**
35
     * @var string
36
     */
37
    private static $description = 'Groups of product options, e.g. size, color, etc';
38
39
    /**
40
     *
41
     */
42
    public function requireDefaultRecords()
43
    {
44
        parent::requireDefaultRecords();
45
        // create a catch-all group
46 View Code Duplication
        if (!OptionGroup::get()->filter(array('Title' => 'Options'))->first()) {
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...
47
            $do = new OptionGroup();
48
            $do->Title = "Options";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OptionGroup>. 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...
49
            $do->write();
50
        }
51 View Code Duplication
        if (!OptionGroup::get()->filter(array('Title' => 'Size'))->first()) {
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...
52
            $do = new OptionGroup();
53
            $do->Title = "Size";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OptionGroup>. 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...
54
            $do->write();
55
        }
56 View Code Duplication
        if (!OptionGroup::get()->filter(array('Title' => 'Color'))->first()) {
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...
57
            $do = new OptionGroup();
58
            $do->Title = "Color";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OptionGroup>. 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...
59
            $do->write();
60
        }
61 View Code Duplication
        if (!OptionGroup::get()->filter(array('Title' => 'Type'))->first()) {
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...
62
            $do = new OptionGroup();
63
            $do->Title = "Type";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OptionGroup>. 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...
64
            $do->write();
65
        }
66
    }
67
68
    /**
69
     * @return RequiredFields
70
     */
71 1
    public function getCMSValidator()
72
    {
73 1
        return new RequiredFields(array('Title'));
74
    }
75
76
    /**
77
     * @return ValidationResult
78
     */
79 12
    public function validate()
80
    {
81 12
        $result = parent::validate();
82
83 12
        $title = $this->Title;
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OptionGroup>. 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...
84 12
        $firstChar = substr($title, 0, 1);
85 12
        if (preg_match('/[^a-zA-Z]/', $firstChar)) {
86 1
            $result->error('The first character of the Title can only be a letter', 'bad');
87 1
        }
88 12
        if (preg_match('/[^a-zA-Z]\s/', $title)) {
89
            $result->error('Please only use letters, numbers and spaces in the title', 'bad');
90
        }
91
92 12
        return $result;
93
    }
94
95
    /**
96
     *
97
     */
98 2
    public function onBeforeDelete()
99
    {
100 2
        parent::onBeforeDelete();
101
102
        //make sure that if we delete this option group, we reassign the group's option items to the 'None' group.
103 2
        $items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID));
104
105 2
        if (isset($items)) {
106 2
            $noneGroup = OptionGroup::get()->filter(array('Title' => 'Options'))->first();
107 2
            foreach ($items as $item) {
108 1
                $item->ProductOptionGroupID = $noneGroup->ID;
109 1
                $item->write();
110 2
            }
111 2
        }
112 2
    }
113
114
    /**
115
     * @param bool $member
116
     * @return bool
117
     */
118 1
    public function canView($member = false)
119
    {
120 1
        return true;
121
    }
122
123
    /**
124
     * @param null $member
125
     * @return bool|int
126
     */
127 3
    public function canEdit($member = null)
128
    {
129 3
        switch ($this->Title) {
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OptionGroup>. 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...
130 3
            case 'Options':
131 2
                return false;
132
                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...
133 3
            default:
134 3
                return Permission::check('Product_CANCRUD', 'any', $member);
135
                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...
136 3
        }
137
    }
138
139
    /**
140
     * @param null $member
141
     * @return bool|int
142
     */
143 2
    public function canDelete($member = null)
144
    {
145 2
        return $this->canEdit($member);
146
    }
147
148
    /**
149
     * @param null $member
150
     * @return bool|int
151
     */
152 1
    public function canCreate($member = null)
153
    {
154 1
        return Permission::check('Product_CANCRUD', 'any', $member);
155
    }
156
}
157