ComplexPriceBuyableDecorator::HasDiscount()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 0
1
<?php
2
3
4
class ComplexPriceBuyableDecorator extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
    private static $has_many = array(
0 ignored issues
show
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
7
        'ComplexPriceObjects' => 'ComplexPriceObject'
8
    );
9
10
    public function updateCMSFields(FieldList $fields)
11
    {
12
        $tabName = 'Root.Pricing';
13
14
        if (class_exists("DataObjectOneFieldUpdateController")) {
15
            $link = DataObjectOneFieldUpdateController::popup_link(
16
                $this->owner->ClassName,
17
                "Price",
18
                $where = "",
19
                $sort = "Price ASC ",
20
                $linkText = "Check all prices..."
21
            );
22
            $fields->AddFieldToTab($tabName, new HeaderField("MetaTitleFixesHeader", "Quick review", 3));
23
            $fields->AddFieldToTab($tabName, new LiteralField("MetaTitleFixes", $link.".<br /><br /><br />"));
24
        }
25
26
        // move price field under new 'Pricing' tab
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
27
//		$priceField = $fields->fieldByName('Root.Details.Price');
28
//		$fields->remove($priceField);
29
30
        $fields->addFieldsToTab(
31
            $tabName,
32
            array(
33
//				$priceField,
34
                new HeaderField("ComplexPricesHeader", "Alternative Pricing", 3),
35
                new LiteralField("ComplexPricesExplanation", "<p>Please enter <i>alternative</i> pricing below. You can enter a price per <a href=\"admin/security/\">security group</a> and/or per country.</p>"),
36
                $this->complexPricesHasManyTable()
37
            )
38
        );
39
    }
40
41
    protected function complexPricesHasManyTable()
42
    {
43
        $gridCfg = new GridFieldConfig_RelationEditor();
44
        $grid = new GridField('ComplexPriceObjects', '', $this->owner->ComplexPriceObjects(), $gridCfg);
45
        return $grid;
46
    }
47
48
    public function HasDiscount()
49
    {
50
        if ($this->owner->Price > 0) {
51
            if ($this->owner->Price > $this->owner->getCalculatedPrice()) {
52
                return true;
53
            }
54
        }
55
        return false;
56
    }
57
58
    public function updateCalculatedPrice(&$startingPrice)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
59
    {
60
        $newPrice = -1;
61
        $fieldName = $this->owner->ClassName."ID";
62
        $singleton = ComplexPriceObject::get()->first();
63
        if ($singleton) {
64
            // Check that ComplexPriceObject can be joined to this type of object
65
            if (!$singleton->hasField($fieldName)) {
66
                $ancestorArray = ClassInfo::ancestry($this->owner, true);
0 ignored issues
show
Documentation introduced by
$this->owner is of type object, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
                foreach ($ancestorArray as $ancestor) {
68
                    $fieldName = $ancestor."ID";
69
                    if ($singleton->hasField($fieldName)) {
70
                        break;
71
                    }
72
                }
73
            }
74
75
            // Load up the alternate prices for this product
76
            $prices = ComplexPriceObject::get()
77
                ->filter(array($fieldName => $this->owner->ID, "NoLongerValid" => 0))
78
                ->sort("NewPrice", "DESC");
79
            ;
80
            $memberGroupsArray = array();
81
            if ($prices->count()) {
82
                // Load up the groups for the current memeber, if any
83
                if ($member = Member::currentUser()) {
84
                    if ($memberGroupComponents = $member->getManyManyComponents('Groups')) {
85
                        if ($memberGroupComponents && $memberGroupComponents->count()) {
86
                            $memberGroupsArray = $memberGroupComponents->column("ID");
87
                            if (!is_array($memberGroupsArray)) {
88
                                $memberGroupsArray = array();
89
                            }
90
                        }
91
                    }
92
                }
93
94
                $countryID = EcommerceCountry::get_country_id();
95
96
                // Look at each price and see if it can be used
97
                foreach ($prices as $price) {
98
                    $priceCanBeUsed = true;
99
100
                    // Does it pass the group filter?
101
                    if ($priceGroupComponents = $price->getManyManyComponents('Groups')) {
102
                        if ($priceGroupComponents && $priceGroupComponents->count()) {
103
                            $priceCanBeUsed = false;
104
                            $priceGroupArray = $priceGroupComponents->column("ID");
105
                            if (!is_array($priceGroupArray)) {
106
                                $priceGroupArray = array();
107
                            }
108
                            $interSectionArray = array_intersect($priceGroupArray, $memberGroupsArray);
109
                            if (is_array($interSectionArray) && count($interSectionArray)) {
110
                                $priceCanBeUsed = true;
111
                            }
112
                        }
113
                    }
114
115
                    // Does it pass the country filter?
116
                    if ($priceCanBeUsed) {
117
                        if ($priceCountryComponents = $price->getManyManyComponents('EcommerceCountries')) {
118
                            if ($priceCountryComponents && $priceCountryComponents->count()) {
119
                                $priceCanBeUsed = false;
120
                                $priceCountryArray = $priceCountryComponents->column("ID");
121
                                if (!is_array($priceCountryArray)) {
122
                                    $priceCountryArray = array();
123
                                }
124
                                if ($countryID && in_array($countryID, $priceCountryArray)) {
125
                                    $priceCanBeUsed = true;
126
                                }
127
                            }
128
                        }
129
                    }
130
131
                    // Does it pass the date filter?
132 View Code Duplication
                    if ($priceCanBeUsed) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
                        $nowTS = strtotime("now");
134
                        if ($price->From) {
135
                            $priceCanBeUsed = false;
136
                            $fromTS = strtotime($price->From);
137
                            if ($fromTS && $fromTS < $nowTS) {
138
                                $priceCanBeUsed = true;
139
                            }
140
                        }
141
                    }
142
143 View Code Duplication
                    if ($priceCanBeUsed) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
                        if ($price->Until) {
145
                            $priceCanBeUsed = false;
146
                            $untilTS = strtotime($price->Until);
147
                            if ($untilTS && $untilTS > $nowTS) {
0 ignored issues
show
Bug introduced by
The variable $nowTS does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
148
                                $priceCanBeUsed = true;
149
                            }
150
                        }
151
                    }
152
153
                    // If so, apply the price
154
                    if ($priceCanBeUsed) {
155
                        $newPrice = $price->getCalculatedPrice();
156
                    }
157
                }
158
            }
159
        }
160
161
        if ($newPrice > -1) {
162
            $startingPrice = $newPrice;
163
        }
164
165
        return $startingPrice;
166
    }
167
}
168
169
170
class ComplexPriceBuyableDecorator_ComplexPriceObject extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
171
{
172
    public static function get_extra_config($class, $extension, $args)
173
    {
174
        $buyables = EcommerceConfig::get("EcommerceDBConfig", "array_of_buyables");
175
        $hasOneArray = array();
176
        if ($buyables && is_array($buyables) && count($buyables)) {
177
            foreach ($buyables as $item) {
178
                $hasOneArray[$item] = $item;
179
            }
180
            return array(
181
                'has_one' => $hasOneArray
182
            );
183
        }
184
        return array();
185
    }
186
187
    public function updateCMSFields(FieldList $fields)
188
    {
189
        $this->owner->getBuyable();
190
        $buyables = EcommerceConfig::get("EcommerceDBConfig", "array_of_buyables");
191
        if ($buyables && is_array($buyables) && count($buyables)) {
192
            foreach ($buyables as $item) {
193
                $fields->replaceField($item."ID", new HiddenField($item."ID"));
194
            }
195
        }
196
        $fields->replaceField("From", new TextField("From"));
197
        return $fields;
198
    }
199
}
200