Passed
Pull Request — master (#2)
by Jason
01:58
created

Discount::canCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Dynamic\Foxy\Discount\Model;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\Security\Permission;
8
9
class Discount extends DataObject
10
{
11
    /**
12
     * @var string
13
     */
14
    private static $singular_name = 'Discount';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
15
16
    /**
17
     * @var string
18
     */
19
    private static $plural_name = 'Discounts';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
20
21
    /**
22
     * @var array
23
     */
24
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
25
        'Title' => 'Varchar(255)',
26
        'Quantity' => 'Int',
27
        'Percentage' => 'Int'
28
    );
29
30
    /**
31
     * @var array
32
     */
33
    private static $summary_fields = array(
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
34
        'Title',
35
        'Quantity',
36
        'DiscountPercentage' => [
37
            'title' => 'Discount',
38
        ],
39
    );
40
41
    /**
42
     * @var string
43
     */
44
    private static $table_name = 'FoxyDiscount';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
45
46
    /**
47
     * @return FieldList
48
     */
49
    public function getCMSFields()
50
    {
51
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
52
            $quantity = $fields->dataFieldByName('Quantity');
53
            $quantity->setTitle('Quantity to trigger discount');
54
55
            $percentage = $fields->dataFieldByName('Percentage');
56
            $percentage->setTitle('Percent discount');
57
        });
58
59
        return parent::getCMSFields();
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getDiscountPercentage()
66
    {
67
        return "{$this->Percentage}%";
0 ignored issues
show
Bug Best Practice introduced by
The property Percentage does not exist on Dynamic\Foxy\Discount\Model\Discount. Since you implemented __get, consider adding a @property annotation.
Loading history...
68
    }
69
70
    /**
71
     * Basic permissions, defaults to page perms where possible.
72
     *
73
     * @param \SilverStripe\Security\Member|null $member
74
     * @return boolean
75
     */
76
    public function canView($member = null)
77
    {
78
        $extended = $this->extendedCan(__FUNCTION__, $member);
79
        if ($extended !== null) {
80
            return $extended;
81
        }
82
83
        if ($page = $this->getPage()) {
0 ignored issues
show
Bug introduced by
The method getPage() does not exist on Dynamic\Foxy\Discount\Model\Discount. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        if ($page = $this->/** @scrutinizer ignore-call */ getPage()) {
Loading history...
84
            return $page->canView($member);
85
        }
86
87
        return Permission::check('CMS_ACCESS', 'any', $member);
88
    }
89
90
    /**
91
     * Basic permissions, defaults to page perms where possible.
92
     *
93
     * @param \SilverStripe\Security\Member|null $member
94
     *
95
     * @return boolean
96
     */
97
    public function canEdit($member = null)
98
    {
99
        $extended = $this->extendedCan(__FUNCTION__, $member);
100
        if ($extended !== null) {
101
            return $extended;
102
        }
103
104
        if ($page = $this->getPage()) {
105
            return $page->canEdit($member);
106
        }
107
108
        return Permission::check('CMS_ACCESS', 'any', $member);
109
    }
110
111
    /**
112
     * Basic permissions, defaults to page perms where possible.
113
     *
114
     * Uses archive not delete so that current stage is respected i.e if a
115
     * element is not published, then it can be deleted by someone who doesn't
116
     * have publishing permissions.
117
     *
118
     * @param \SilverStripe\Security\Member|null $member
119
     *
120
     * @return boolean
121
     */
122
    public function canDelete($member = null)
123
    {
124
        $extended = $this->extendedCan(__FUNCTION__, $member);
125
        if ($extended !== null) {
126
            return $extended;
127
        }
128
129
        if ($page = $this->getPage()) {
130
            return $page->canArchive($member);
131
        }
132
133
        return Permission::check('CMS_ACCESS', 'any', $member);
134
    }
135
136
    /**
137
     * Basic permissions, defaults to page perms where possible.
138
     *
139
     * @param \SilverStripe\Security\Member|null $member
140
     * @param array $context
141
     *
142
     * @return boolean
143
     */
144
    public function canCreate($member = null, $context = array())
145
    {
146
        $extended = $this->extendedCan(__FUNCTION__, $member);
147
        if ($extended !== null) {
148
            return $extended;
149
        }
150
151
        return Permission::check('CMS_ACCESS', 'any', $member);
152
    }
153
}
154