Passed
Push — 1.0 ( 07b100...83f8df )
by Morven
05:31
created

Discount   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A canDelete() 0 8 2
A populateDefaults() 0 3 1
A AddLink() 0 7 1
A generateRandomString() 0 10 2
A canView() 0 8 2
A onBeforeWrite() 0 6 1
A canEdit() 0 8 2
A canCreate() 0 8 2
A getCMSFields() 0 18 2
1
<?php
2
3
namespace SilverCommerce\OrdersAdmin\Model;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Forms\ReadonlyField;
8
use SilverStripe\Security\Group;
9
use SilverStripe\Core\Convert;
10
use SilverStripe\SiteConfig\SiteConfig;
11
use SilverCommerce\OrdersAdmin\Control\ShoppingCart;
0 ignored issues
show
Bug introduced by
The type SilverCommerce\OrdersAdmin\Control\ShoppingCart was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class Discount extends DataObject
14
{
15
    private static $table_name = 'Discount';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
16
17
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
18
        "Title"     => "Varchar",
19
        "Type"      => "Enum('Fixed,Percentage,Free Shipping','Percentage')",
20
        "Code"      => "Varchar(299)",
21
        "Amount"    => "Decimal",
22
        "Country"   => "Varchar(255)",
23
        "Expires"   => "Date"
24
    );
25
26
    private static $has_one = array(
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
27
        "Site"      => SiteConfig::class
28
    );
29
30
    private static $many_many = array(
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
31
        "Groups"    => Group::class
32
    );
33
34
    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...
35
        "Title",
36
        "Code",
37
        "Expires"
38
    );
39
40
    /**
41
     * Generate a random string that we can use for the code by default
42
     *
43
     * @return string
44
     */
45
    private static function generateRandomString($length = 10)
46
    {
47
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
48
        $string = '';
49
50
        for ($i = 0; $i < $length; $i++) {
51
            $string .= $characters[rand(0, strlen($characters) - 1)];
52
        }
53
54
        return $string;
55
    }
56
57
    /**
58
     * Set more complex default data
59
     */
60
    public function populateDefaults()
61
    {
62
        $this->setField('Code', self::generateRandomString());
63
    }
64
65
    /**
66
     * Return a URL that allows this code to be added to a cart
67
     * automatically
68
     *
69
     * @return String
70
     */
71
    public function AddLink()
72
    {
73
        $cart = ShoppingCart::get();
74
        
75
        return Controller::join_links(
76
            $cart->AbsoluteLink("usediscount"),
77
            $this->Code
78
        );
79
    }
80
81
    public function getCMSFields()
82
    {
83
        
84
        $this->beforeUpdateCMSFields(function ($fields) {
85
            if ($this->Code) {
86
                $fields->addFieldToTab(
87
                    "Root.Main",
88
                    ReadonlyField::create(
89
                        "DiscountURL",
0 ignored issues
show
Bug introduced by
'DiscountURL' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

89
                        /** @scrutinizer ignore-type */ "DiscountURL",
Loading history...
90
                        _t("OrdersAdmin.AddDiscountURL", "Add discount URL"),
91
                        $this->AddLink()
92
                    ),
93
                    "Code"
94
                );
95
            }
96
        });
97
98
        return parent::getCMSFields();
99
    }
100
101
    public function onBeforeWrite()
102
    {
103
        parent::onBeforeWrite();
104
105
        // Ensure that the code is URL safe
106
        $this->Code = Convert::raw2url($this->Code);
0 ignored issues
show
Bug Best Practice introduced by
The property Code does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
107
    }
108
109
    public function canView($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

109
    public function canView($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        $extended = $this->extendedCan('canView', $member);
112
        if ($extended !== null) {
113
            return $extended;
114
        }
115
        
116
        return true;
117
    }
118
    
119
    public function canCreate($member = null, $context = [])
120
    {
121
        $extended = $this->extendedCan('canCreate', $member);
122
        if ($extended !== null) {
123
            return $extended;
124
        }
125
        
126
        return true;
127
    }
128
129
    public function canEdit($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

129
    public function canEdit($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
    {
131
        $extended = $this->extendedCan('canEdit', $member);
132
        if ($extended !== null) {
133
            return $extended;
134
        }
135
        
136
        return true;
137
    }
138
139
    public function canDelete($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

139
    public function canDelete($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
140
    {
141
        $extended = $this->extendedCan('canDelete', $member);
142
        if ($extended !== null) {
143
            return $extended;
144
        }
145
        
146
        return true;
147
    }
148
}
149