Completed
Pull Request — master (#287)
by Nic
08:24
created

ProductDiscountTier::canDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * Class ProductDiscountTier
5
 */
6
class ProductDiscountTier extends DataObject
7
{
8
9
    /**
10
     * @var string
11
     */
12
    private static $singular_name = 'Discount Tier';
13
    /**
14
     * @var string
15
     */
16
    private static $plural_name = 'Discount Tiers';
17
    /**
18
     * @var string
19
     */
20
    private static $description = 'A discount tier for a Product Discount';
21
22
    /**
23
     * @var array
24
     */
25
    private static $db = array(
26
        'Quantity' => 'Int',
27
        'Percentage' => 'Int'
28
    );
29
30
    /**
31
     * @var array
32
     */
33
    private static $has_one = array(
34
        'Product' => 'FoxyStripeProduct'
35
    );
36
37
    /**
38
     * @var array
39
     */
40
    private static $default_sort = array(
0 ignored issues
show
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...
41
        'Quantity'
42
    );
43
44
    /**
45
     * @var array
46
     */
47
    private static $summary_fields = array(
48
        'Quantity',
49
        'DiscountPercentage'
50
    );
51
52
    /**
53
     * @var array
54
     */
55
    private static $field_labels = array(
0 ignored issues
show
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
        'Quantity' => 'Quantity',
57
        'DiscountPercentage' => 'Discount'
58
    );
59
60
    /**
61
     * @return FieldList
62
     */
63
    public function getCMSFields()
64
    {
65
        $fields = parent::getCMSFields();
66
67
        $fields->removeByName('ProductPageID');
68
69
        $quantity = $fields->dataFieldByName('Quantity');
70
        $quantity->setTitle('Quantity to trigger discount');
71
        $percentage = $fields->dataFieldByName('Percentage');
72
        $percentage->setTitle('Percent discount');
73
74
        $this->extend('updateCMSFields', $fields);
75
        return $fields;
76
    }
77
78
    /**
79
     * @return RequiredFields
80
     */
81
    public function getCMSValidator()
82
    {
83
        return new RequiredFields(array('Quantity', 'Percentage'));
84
    }
85
86
    /**
87
     * @return ValidationResult
88
     *
89
     * TODO implement validation to ensure values aren't duplicated in multiple tiers
90
     */
91
    public function validate()
92
    {
93
        $result = parent::validate();
94
95
        /*$tierQuantity = ProductDiscountTier::get()
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
96
            ->filter(
97
                array(
98
                    'ProductDiscountID' => $this->ProductDiscountID,
99
                    'Quantity' => $this->Quantity
100
                )
101
            )->first();
102
103
        $tierPercentage = ProductDiscountTier::get()
104
            ->filter(
105
                array(
106
                    'ProductDiscountID' => $this->ProductDiscountID,
107
                    'Percentage' => $this->Percentage
108
                )
109
            )->first();
110
111
        if($tierQuantity->ID != 0 && $tierQuantity->ID != $this->ID){
112
            $result->error($this->Quantity." is already used in another discount tier. Please use a different quantity");
113
        }
114
        if($tierPercentage->ID != 0 && $tierPercentage->ID != $this->ID){
115
            $result->error($this->Percentage." is already used in another discount tier. Please use a different percentage");
116
        }*/
117
118
        return $result;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getTitle()
125
    {
126
        return "{$this->Quantity} at {$this->Percentage}%";
0 ignored issues
show
Documentation introduced by
The property Quantity does not exist on object<ProductDiscountTier>. 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 Percentage does not exist on object<ProductDiscountTier>. 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...
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getDiscountPercentage()
133
    {
134
        return "{$this->Percentage}%";
0 ignored issues
show
Documentation introduced by
The property Percentage does not exist on object<ProductDiscountTier>. 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...
135
    }
136
137
    /**
138
     * @param bool $member
139
     * @return bool
140
     */
141
    public function canView($member = false)
142
    {
143
        return true;
144
    }
145
146
    /**
147
     * @param null $member
148
     * @return bool|int
149
     */
150
    public function canEdit($member = null)
151
    {
152
        return Permission::check('Product_CANCRUD');
153
    }
154
155
    /**
156
     * @param null $member
157
     * @return bool|int
158
     */
159
    public function canDelete($member = null)
160
    {
161
        return Permission::check('Product_CANCRUD');
162
    }
163
164
    /**
165
     * @param null $member
166
     * @return bool|int
167
     */
168
    public function canCreate($member = null)
169
    {
170
        return Permission::check('Product_CANCRUD');
171
    }
172
173
}
174