Passed
Pull Request — master (#5)
by Jason
01:54
created

Discount::canView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Dynamic\Foxy\Discounts\Model;
4
5
use Dynamic\Products\Page\Product;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
8
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\Security\Permission;
11
use SilverStripe\Versioned\GridFieldArchiveAction;
12
use SilverStripe\Versioned\Versioned;
13
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
14
15
class Discount extends DataObject
16
{
17
    /**
18
     * @var string
19
     */
20
    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...
21
22
    /**
23
     * @var string
24
     */
25
    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...
26
27
    /**
28
     * @var array
29
     */
30
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
31
        'Title' => 'Varchar(255)',
32
        'Quantity' => 'Int',
33
        'Percentage' => 'Int',
34
        'StartTime' => 'DBDatetime',
35
        'EndTime' => 'DBDatetime',
36
    );
37
38
    /**
39
     * @var array
40
     */
41
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
42
        'Products' => Product::class,
43
    ];
44
45
    /**
46
     * @var array
47
     */
48
    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...
49
        'Title',
50
        'DiscountPercentage' => [
51
            'title' => 'Discount',
52
        ],
53
        'StartTime.Nice' => 'Starts',
54
        'EndTime.Nice' => 'Ends',
55
        'IsActive' => 'Active',
56
        'IsGlobal' => 'Global',
57
        'Products.count' => 'Products',
58
    );
59
60
    /**
61
     * @var array
62
     */
63
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
64
        'Quantity' => 1,
65
    ];
66
67
    /**
68
     * @var array
69
     */
70
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
71
        'IsActive' => 'Boolean',
72
        'IsGlobal' => 'Boolean',
73
    ];
74
75
    /**
76
     * @var array
77
     */
78
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
79
        Versioned::class,
80
    ];
81
82
    /**
83
     * @var string
84
     */
85
    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...
86
87
    /**
88
     * @return FieldList
89
     */
90
    public function getCMSFields()
91
    {
92
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
93
            $fields->removeByName([
94
                'Quantity',
95
            ]);
96
97
            //$quantity = $fields->dataFieldByName('Quantity');
98
            //$quantity->setTitle('Quantity to trigger discount');
99
100
            $percentage = $fields->dataFieldByName('Percentage');
101
            $percentage->setTitle('Percent discount');
102
103
            if ($this->ID) {
104
                $field = $fields->dataFieldByName('Products');
105
                $config = $field->getConfig();
106
                $config
107
                    ->removeComponentsByType([
108
                        GridFieldAddExistingAutocompleter::class,
109
                        GridFieldAddNewButton::class,
110
                        GridFieldArchiveAction::class,
111
                    ])
112
                    ->addComponents([
113
                        new GridFieldAddExistingSearchButton(),
114
                    ]);
115
            }
116
        });
117
118
        return parent::getCMSFields();
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getDiscountPercentage()
125
    {
126
        return "{$this->Percentage}%";
0 ignored issues
show
Bug Best Practice introduced by
The property Percentage does not exist on Dynamic\Foxy\Discounts\Model\Discount. Since you implemented __get, consider adding a @property annotation.
Loading history...
127
    }
128
129
    /**
130
     * @return bool
131
     */
132
    public function getIsActive()
133
    {
134
        $date = date('Y-m-d H:i:s', strtotime('now'));
135
        return ($this->owner->StartTime <= $date && $this->owner->EndTime >= $date) && $this->owner->isPublished();
0 ignored issues
show
Bug Best Practice introduced by
The property owner does not exist on Dynamic\Foxy\Discounts\Model\Discount. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method isPublished() does not exist on null. ( Ignorable by Annotation )

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

135
        return ($this->owner->StartTime <= $date && $this->owner->EndTime >= $date) && $this->owner->/** @scrutinizer ignore-call */ isPublished();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    public function getIsGlobal()
142
    {
143
        return $this->Products()->count() === 0;
0 ignored issues
show
Bug introduced by
The method Products() does not exist on Dynamic\Foxy\Discounts\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

143
        return $this->/** @scrutinizer ignore-call */ Products()->count() === 0;
Loading history...
144
    }
145
146
    /**
147
     * Basic permissions, defaults to page perms where possible.
148
     *
149
     * @param \SilverStripe\Security\Member|null $member
150
     * @return boolean
151
     */
152
    public function canView($member = null)
153
    {
154
        $extended = $this->extendedCan(__FUNCTION__, $member);
155
        if ($extended !== null) {
156
            return $extended;
157
        }
158
159
        return Permission::check('CMS_ACCESS', 'any', $member);
160
    }
161
162
    /**
163
     * Basic permissions, defaults to page perms where possible.
164
     *
165
     * @param \SilverStripe\Security\Member|null $member
166
     *
167
     * @return boolean
168
     */
169
    public function canEdit($member = null)
170
    {
171
        $extended = $this->extendedCan(__FUNCTION__, $member);
172
        if ($extended !== null) {
173
            return $extended;
174
        }
175
176
        return Permission::check('CMS_ACCESS', 'any', $member);
177
    }
178
179
    /**
180
     * Basic permissions, defaults to page perms where possible.
181
     *
182
     * Uses archive not delete so that current stage is respected i.e if a
183
     * element is not published, then it can be deleted by someone who doesn't
184
     * have publishing permissions.
185
     *
186
     * @param \SilverStripe\Security\Member|null $member
187
     *
188
     * @return boolean
189
     */
190
    public function canDelete($member = null)
191
    {
192
        $extended = $this->extendedCan(__FUNCTION__, $member);
193
        if ($extended !== null) {
194
            return $extended;
195
        }
196
197
        return Permission::check('CMS_ACCESS', 'any', $member);
198
    }
199
200
    /**
201
     * Basic permissions, defaults to page perms where possible.
202
     *
203
     * @param \SilverStripe\Security\Member|null $member
204
     * @param array $context
205
     *
206
     * @return boolean
207
     */
208
    public function canCreate($member = null, $context = array())
209
    {
210
        $extended = $this->extendedCan(__FUNCTION__, $member);
211
        if ($extended !== null) {
212
            return $extended;
213
        }
214
215
        return Permission::check('CMS_ACCESS', 'any', $member);
216
    }
217
}
218