Passed
Pull Request — master (#11)
by Jason
02:21
created

Discount::canCreate()   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 2
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\GridField;
8
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
9
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
10
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
11
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
12
use SilverStripe\ORM\DataObject;
13
use SilverStripe\Security\Permission;
14
use SilverStripe\Versioned\GridFieldArchiveAction;
15
use SilverStripe\Versioned\Versioned;
16
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
17
18
/**
19
 * Class Discount
20
 * @package Dynamic\Foxy\Discounts\Model
21
 */
22
class Discount extends DataObject
23
{
24
    /**
25
     * @var string
26
     */
27
    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...
28
29
    /**
30
     * @var string
31
     */
32
    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...
33
34
    /**
35
     * @var array
36
     */
37
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
38
        'Title' => 'Varchar(255)',
39
        'StartTime' => 'DBDatetime',
40
        'EndTime' => 'DBDatetime',
41
        'Type' => 'Enum("Percent, Amount")',
42
    );
43
44
    /**
45
     * @var array
46
     */
47
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
48
        'DiscountTiers' => DiscountTier::class,
49
    ];
50
51
    /**
52
     * @var array
53
     */
54
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
55
        'Products' => Product::class,
56
    ];
57
58
    /**
59
     * @var array
60
     */
61
    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...
62
        'Title',
63
        'StartTime.Nice' => 'Starts',
64
        'EndTime.Nice' => 'Ends',
65
        'IsActive' => 'Active',
66
        'IsGlobal' => 'Global',
67
        'Products.count' => 'Products',
68
    );
69
70
    /**
71
     * @var array
72
     */
73
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
74
        'IsActive' => 'Boolean',
75
        'IsGlobal' => 'Boolean',
76
    ];
77
78
    /**
79
     * @var array
80
     */
81
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
82
        Versioned::class,
83
    ];
84
85
    /**
86
     * @var string
87
     */
88
    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...
89
90
    /**
91
     * @var array
92
     */
93
    private $type_mapping = [
94
        'Percent' => 'discount_quantity_percentage',
95
        'Amount' => 'discount_quantity_amount',
96
    ];
97
98
    /**
99
     * @return FieldList
100
     */
101
    public function getCMSFields()
102
    {
103
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
104
            if ($this->ID) {
105
                $fields->removeByName([
106
                    'DiscountTiers',
107
                ]);
108
109
                // ProductDiscountTiers
110
                $config = GridFieldConfig_RelationEditor::create();
111
                $config
112
                    ->removeComponentsByType([
113
                        GridFieldAddExistingAutocompleter::class,
114
                        GridFieldDeleteAction::class,
115
                    ])
116
                    ->addComponents([
117
                        new GridFieldDeleteAction(false)
118
                    ]);
119
                $discountGrid = GridField::create(
120
                    'DiscountTiers',
121
                    'Discount Tiers',
122
                    $this->owner->DiscountTiers(),
0 ignored issues
show
Bug introduced by
The method DiscountTiers() 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

122
                    $this->owner->/** @scrutinizer ignore-call */ 
123
                                  DiscountTiers(),

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...
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...
123
                    $config
124
                );
125
                $fields->addFieldToTab('Root.Main', $discountGrid);
126
127
                // Products
128
                $field = $fields->dataFieldByName('Products');
129
                $config = $field->getConfig();
130
                $config
131
                    ->removeComponentsByType([
132
                        GridFieldAddExistingAutocompleter::class,
133
                        GridFieldAddNewButton::class,
134
                        GridFieldArchiveAction::class,
135
                    ])
136
                    ->addComponents([
137
                        new GridFieldAddExistingSearchButton(),
138
                    ]);
139
            }
140
        });
141
142
        return parent::getCMSFields();
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function getIsActive()
149
    {
150
        $date = date('Y-m-d H:i:s', strtotime('now'));
151
        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...
152
    }
153
154
    /**
155
     * @return bool
156
     */
157
    public function getIsGlobal()
158
    {
159
        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

159
        return $this->/** @scrutinizer ignore-call */ Products()->count() === 0;
Loading history...
160
    }
161
162
    /**
163
     * @return mixed
164
     */
165
    public function getDiscountType()
166
    {
167
        $types = $this->type_mapping;
168
        return $types[$this->Type];
0 ignored issues
show
Bug Best Practice introduced by
The property Type does not exist on Dynamic\Foxy\Discounts\Model\Discount. Since you implemented __get, consider adding a @property annotation.
Loading history...
169
    }
170
171
    /**
172
     * Basic permissions, defaults to page perms where possible.
173
     *
174
     * @param \SilverStripe\Security\Member|null $member
175
     * @return boolean
176
     */
177
    public function canView($member = null)
178
    {
179
        $extended = $this->extendedCan(__FUNCTION__, $member);
180
        if ($extended !== null) {
181
            return $extended;
182
        }
183
184
        return Permission::check('CMS_ACCESS', 'any', $member);
185
    }
186
187
    /**
188
     * Basic permissions, defaults to page perms where possible.
189
     *
190
     * @param \SilverStripe\Security\Member|null $member
191
     *
192
     * @return boolean
193
     */
194
    public function canEdit($member = null)
195
    {
196
        $extended = $this->extendedCan(__FUNCTION__, $member);
197
        if ($extended !== null) {
198
            return $extended;
199
        }
200
201
        return Permission::check('CMS_ACCESS', 'any', $member);
202
    }
203
204
    /**
205
     * Basic permissions, defaults to page perms where possible.
206
     *
207
     * Uses archive not delete so that current stage is respected i.e if a
208
     * element is not published, then it can be deleted by someone who doesn't
209
     * have publishing permissions.
210
     *
211
     * @param \SilverStripe\Security\Member|null $member
212
     *
213
     * @return boolean
214
     */
215
    public function canDelete($member = null)
216
    {
217
        $extended = $this->extendedCan(__FUNCTION__, $member);
218
        if ($extended !== null) {
219
            return $extended;
220
        }
221
222
        return Permission::check('CMS_ACCESS', 'any', $member);
223
    }
224
225
    /**
226
     * Basic permissions, defaults to page perms where possible.
227
     *
228
     * @param \SilverStripe\Security\Member|null $member
229
     * @param array $context
230
     *
231
     * @return boolean
232
     */
233
    public function canCreate($member = null, $context = array())
234
    {
235
        $extended = $this->extendedCan(__FUNCTION__, $member);
236
        if ($extended !== null) {
237
            return $extended;
238
        }
239
240
        return Permission::check('CMS_ACCESS', 'any', $member);
241
    }
242
}
243