Completed
Pull Request — master (#287)
by Nic
05:31
created

ProductDiscountTier::canDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
class ProductDiscountTier extends DataObject {
4
5
	private static $singular_name = 'Discount Tier';
6
	private static $plural_name = 'Discount Tiers';
7
	private static $description = 'A discount tier for a Product Discount';
8
9
	private static $db = array(
10
		'Quantity' => 'Int',
11
		'Percentage' => 'Int'
12
	);
13
	private static $has_one = array(
14
		'Product' => 'FoxyStripeProduct'
15
	);
16
	private static $has_many = array();
17
	private static $many_many = array();
18
	private static $many_many_extraFields = array();
19
	private static $belongs_many_many = array();
20
21
	private static $casting = array();
22
	private static $defaults = array();
23
	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...
24
		'Quantity'
25
	);
26
27
28
	private static $summary_fields = array(
29
		'Quantity',
30
		'DiscountPercentage'
31
	);
32
	private static $searchable_fields = array();
33
	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...
34
		'Quantity' => 'Quantity',
35
		'DiscountPercentage' => 'Discount'
36
	);
37
	private static $indexes = array();
38
39
	public function getCMSFields(){
40
		$fields = parent::getCMSFields();
41
42
		$fields->removeByName('ProductPageID');
43
44
		$quantity = $fields->dataFieldByName('Quantity');
45
		$quantity->setTitle('Quantity to trigger discount');
46
		$percentage = $fields->dataFieldByName('Percentage');
47
		$percentage->setTitle('Percent discount');
48
49
		$this->extend('updateCMSFields', $fields);
50
		return $fields;
51
	}
52
53
	public function getCMSValidator() {
54
		return new RequiredFields(array('Quantity', 'Percentage'));
55
	}
56
57
	/**
58
	 * @return ValidationResult
59
	 *
60
	 * TODO implement validation to ensure values aren't duplicated in multiple tiers
61
	 */
62
	public function validate(){
63
		$result = parent::validate();
64
65
		/*$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...
66
			->filter(
67
				array(
68
					'ProductDiscountID' => $this->ProductDiscountID,
69
					'Quantity' => $this->Quantity
70
				)
71
			)->first();
72
73
		$tierPercentage = ProductDiscountTier::get()
74
			->filter(
75
				array(
76
					'ProductDiscountID' => $this->ProductDiscountID,
77
					'Percentage' => $this->Percentage
78
				)
79
			)->first();
80
81
		if($tierQuantity->ID != 0 && $tierQuantity->ID != $this->ID){
82
			$result->error($this->Quantity." is already used in another discount tier. Please use a different quantity");
83
		}
84
		if($tierPercentage->ID != 0 && $tierPercentage->ID != $this->ID){
85
			$result->error($this->Percentage." is already used in another discount tier. Please use a different percentage");
86
		}*/
87
88
		return $result;
89
	}
90
91
	public function getTitle(){
92
		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...
93
	}
94
95
	public function getDiscountPercentage(){
96
		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...
97
	}
98
99
	public function canView($member = false) {
100
		return true;
101
	}
102
103
	public function canEdit($member = null) {
104
		return Permission::check('Product_CANCRUD');
105
	}
106
107
	public function canDelete($member = null) {
108
		return Permission::check('Product_CANCRUD');
109
	}
110
111
	public function canCreate($member = null) {
112
		return Permission::check('Product_CANCRUD');
113
	}
114
115
}
116