Passed
Pull Request — master (#9)
by Jason
02:10
created

Discount::getCMSFields()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 27
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 42
rs 9.488
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
class Discount extends DataObject
19
{
20
    /**
21
     * @var string
22
     */
23
    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...
24
25
    /**
26
     * @var string
27
     */
28
    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...
29
30
    /**
31
     * @var array
32
     */
33
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
34
        'Title' => 'Varchar(255)',
35
        'StartTime' => 'DBDatetime',
36
        'EndTime' => 'DBDatetime',
37
    );
38
39
    /**
40
     * @var array
41
     */
42
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
43
        'DiscountTiers' => DiscountTier::class,
44
    ];
45
46
    /**
47
     * @var array
48
     */
49
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
50
        'Products' => Product::class,
51
    ];
52
53
    /**
54
     * @var array
55
     */
56
    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...
57
        'Title',
58
        'StartTime.Nice' => 'Starts',
59
        'EndTime.Nice' => 'Ends',
60
        'IsActive' => 'Active',
61
        'IsGlobal' => 'Global',
62
        'Products.count' => 'Products',
63
    );
64
65
    /**
66
     * @var array
67
     */
68
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
69
        'IsActive' => 'Boolean',
70
        'IsGlobal' => 'Boolean',
71
    ];
72
73
    /**
74
     * @var array
75
     */
76
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
77
        Versioned::class,
78
    ];
79
80
    /**
81
     * @var string
82
     */
83
    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...
84
85
    /**
86
     * @return FieldList
87
     */
88
    public function getCMSFields()
89
    {
90
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
91
            if ($this->ID) {
92
                $fields->removeByName([
93
                    'DiscountTiers',
94
                ]);
95
96
                // ProductDiscountTiers
97
                $config = GridFieldConfig_RelationEditor::create();
98
                $config
99
                    ->removeComponentsByType([
100
                        GridFieldAddExistingAutocompleter::class,
101
                        GridFieldDeleteAction::class,
102
                    ])
103
                    ->addComponents([
104
                        new GridFieldDeleteAction(false)
105
                    ]);
106
                $discountGrid = GridField::create(
107
                    'DiscountTiers',
108
                    'Discount Tiers',
109
                    $this->owner->DiscountTiers(),
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 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

109
                    $this->owner->/** @scrutinizer ignore-call */ 
110
                                  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...
110
                    $config
111
                );
112
                $fields->addFieldToTab('Root.Main', $discountGrid);
113
114
                // Products
115
                $field = $fields->dataFieldByName('Products');
116
                $config = $field->getConfig();
117
                $config
118
                    ->removeComponentsByType([
119
                        GridFieldAddExistingAutocompleter::class,
120
                        GridFieldAddNewButton::class,
121
                        GridFieldArchiveAction::class,
122
                    ])
123
                    ->addComponents([
124
                        new GridFieldAddExistingSearchButton(),
125
                    ]);
126
            }
127
        });
128
129
        return parent::getCMSFields();
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function getIsActive()
136
    {
137
        $date = date('Y-m-d H:i:s', strtotime('now'));
138
        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...
139
    }
140
141
    /**
142
     * @return bool
143
     */
144
    public function getIsGlobal()
145
    {
146
        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

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