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

Discount::getDiscountPercentage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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
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('DiscountTiers', 'Discount Tiers', $this->owner->DiscountTiers(), $config);
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

106
                $discountGrid = GridField::create('DiscountTiers', 'Discount Tiers', $this->owner->/** @scrutinizer ignore-call */ DiscountTiers(), $config);

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

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