Completed
Push — master ( 0728b3...6989aa )
by Nicolaas
03:32
created

BuyableStockDecorator_Extension::index()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 4
nop 0
1
<?php
2
3
/**
4
 * BuyableStockDecorator
5
 * Extension for any buyable - adding stock level capabilities.
6
 */
7
8
class BuyableStockDecorator 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...
9
{
10
11
    /**
12
     * Array of Class Names of classes that are buyables
13
     * @to do, do we need this here, why not use the original buyable dod.
14
     * @var Array
15
     *
16
     */
17
    private static $buyables = array();
0 ignored issues
show
Unused Code introduced by
The property $buyables 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...
18
19
    /**
20
     * Selector for field in the HTML where quantities are set
21
     * @to do - move this out to template...
22
     * @var String
23
     *
24
     */
25
    private static $quantity_field_selector = "";
0 ignored issues
show
Unused Code introduced by
The property $quantity_field_selector 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...
26
27
    /**
28
     * Standard SS method
29
     *
30
     * @return Array
31
     */
32
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db 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...
33
        'MinQuantity' => 'Int',
34
        'MaxQuantity' => 'Int',
35
        'UnlimitedStock' => 'Boolean'
36
    );
37
38
    private static $casting = array(
0 ignored issues
show
Unused Code introduced by
The property $casting 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...
39
        'ActualQuantity' => 'Int'
40
    );
41
42
    private static $defaults = array(
0 ignored issues
show
Unused Code introduced by
The property $defaults 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...
43
        'UnlimitedStock' => 1,
44
        'MinQuantity' => 0,
45
        'MaxQuantity' => 0
46
    );
47
48
    /*
49
     * Allow setting stock level in CMS
50
     */
51
    public function updateCMSFields(FieldList $fields)
52
    {
53
        $tabName = 'Root.Stock';
54
        $fields->addFieldsToTab(
55
            $tabName,
56
            array(
57
                new HeaderField('MinMaxHeader', 'Minimum and Maximum Quantities per Order', 3),
58
                new NumericField('MinQuantity', 'Min. Qty per order'),
59
                new NumericField('MaxQuantity', 'Max. Qty per order'),
60
                new HeaderField('ActualQuantityHeader', 'Stock available', 3),
61
                new CheckboxField('UnlimitedStock', 'Unlimited Stock'),
62
                new NumericField('ActualQuantity', 'Actual Stock Available', $this->getActualQuantity()),
63
                new HeaderField('ActualQuantityAdjustmentHeader', 'Adjust all stock', 3),
64
                new LiteralField('ActualQuantityAdjustmentLink', 'This CMS also provides a <a href="/update-stock/" target="_blank">quick stock adjuster</a>.')
65
            )
66
        );
67
    }
68
69
    /*
70
     * Getter for stock level
71
     */
72
    public function ActualQuantity()
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...
73
    {
74
        return $this->getActualQuantity();
75
    }
76
    public function getActualQuantity()
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...
77
    {
78
        return BuyableStockCalculatedQuantity::get_quantity_by_buyable($this->owner);
79
    }
80
81
    /*
82
     * Setter for stock level
83
     * @param int $value
84
     */
85
    public function setActualQuantity($value)
86
    {
87
        if (!$this->owner->ID) {
88
            $this->owner->write();
89
        }
90
        //only set stock level if it differs from previous
91
        $shopAdminCode = EcommerceConfig::get("EcommerceRole", "admin_permission_code");
92
        if ($this->owner->ID) {
93
            if ($shopAdminCode && Permission::check($shopAdminCode)) {
94
                if ($value != $this->owner->getActualQuantity()) {
95
                    $parent = BuyableStockCalculatedQuantity::get_by_buyable($this->owner);
96
                    if ($parent) {
97
                        $member = Member::currentUser();
98
                        $obj = new BuyableStockManualUpdate();
99
                        $obj->ParentID = $parent->ID;
0 ignored issues
show
Documentation introduced by
The property ParentID does not exist on object<BuyableStockManualUpdate>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100
                        $obj->Quantity = (int)$value;
0 ignored issues
show
Documentation introduced by
The property Quantity does not exist on object<BuyableStockManualUpdate>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101
                        $obj->MemberID = $member->ID;
0 ignored issues
show
Documentation introduced by
The property MemberID does not exist on object<BuyableStockManualUpdate>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
102
                        $obj->write();
103
                    } else {
104
                        user_error("Could not write BuyableStockCalculatedQuantity Object because there was no parent ".$this->owner->Title);
105
                    }
106
                }
107
            } else {
108
                user_error("Could not write BuyableStockCalculatedQuantity Object because you do not have permissions ".$this->owner->Title);
109
            }
110
        }
111
    }
112
113
114
    /**
115
     * This is a pivotal method.
116
     * Only allow purchase if stock levels allow
117
     * TODO: customise this to a certain stock level, on, or off
118
     * @return null (equals TRUE!), true or false
0 ignored issues
show
Documentation introduced by
Should the return type not be null|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
119
     */
120
    public function canPurchase($member = null)
0 ignored issues
show
Unused Code introduced by
The parameter $member is not used and could be removed.

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

Loading history...
121
    {
122
        if ($this->owner->UnlimitedStock) {
123
            return null;
124
        }
125
        if ($this->owner->getActualQuantity() < $this->owner->MinQuantity) {
126
            return false;
127
        }
128
        if ($this->owner->getActualQuantity() <= 0) {
129
            return false;
130
        }
131
        return null; //returning null ensures that the value from this method is ignored.
132
    }
133
134
    /**
135
     * stanard SS metehod
136
     */
137
    public function onAfterWrite()
0 ignored issues
show
Coding Style introduced by
onAfterWrite uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
138
    {
139
        BuyableStockCalculatedQuantity::get_by_buyable($this->owner);
140
        if (isset($_REQUEST["ActualQuantity"])) {
141
            $actualQuantity = intval($_REQUEST["ActualQuantity"]);
142
            if ($actualQuantity != $this->owner->getActualQuantity() && ($actualQuantity === 0 || $actualQuantity)) {
143
                $this->owner->setActualQuantity($actualQuantity);
144
            }
145
        }
146
    }
147
}
148
149
class BuyableStockDecorator_Extension extends Extension
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...
150
{
151
    /**
152
     * TO DO: review method below
153
     *  - move to init???
154
     *  - decorate Ecommerce Quantity Field?
155
     *
156
     **/
157
158
159
    public function index()
160
    {
161
        $min = 0;
162
        $max = 0;
163
        $msg = Config::inst()->get("MinMaxModifier", "sorry_message");
164
        $fieldSelector = Config::inst()->get("BuyableStockDecorator", "quantity_field_selector");
165
        if ($minField = Config::inst()->get("MinMaxModifier", "min_field")) {
166
            $min = $this->owner->$minField;
167
        }
168
        if ($maxField = Config::inst()->get("MinMaxModifier", "max_field")) {
169
            $max = $this->owner->$maxField;
170
        }
171
        $js = '
172
            MinMaxModifierData = [];
173
            MinMaxModifierData.push(
174
                {
175
                    selector: "'.$fieldSelector.'",
176
                    min: '.intval($min).',
177
                    max: '.intval($max).',
178
                    msg: "'.addslashes($msg).'"
179
                }
180
            );';
181
        Requirements::javascript("ecommerce_stockcontrol/javascript/MinMaxModifier.js");
182
        Requirements::customScript($js, $fieldSelector);
183
        return array();
184
    }
185
}
186