Completed
Pull Request — master (#7)
by Jason
12:37
created

FoxyStripeOptionInventoryManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 33
dl 0
loc 94
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getOrders() 0 6 2
A updateCMSFields() 0 17 1
A updateOptionAvailability() 0 4 2
A getNumberAvailable() 0 4 2
A getNumberPurchased() 0 9 3
A getHasInventory() 0 3 2
A getIsOptionAvailable() 0 6 2
1
<?php
2
3
namespace Dynamic\FoxyStripe\ORM;
4
5
use Dynamic\FoxyStripe\Model\OrderDetail;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\CheckboxField;
8
use SilverStripe\Forms\NumericField;
9
use SilverStripe\Forms\ReadonlyField;
10
use SilverStripe\ORM\DataExtension;
11
use UncleCheese\DisplayLogic\Forms\Wrapper;
12
13
class FoxyStripeOptionInventoryManager extends DataExtension
14
{
15
    /**
16
     * @var array
17
     */
18
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
        'ControlInventory' => 'Boolean',
20
        'PurchaseLimit' => 'Int',
21
    ];
22
23
    /**
24
     * @param FieldList $fields
25
     */
26
    public function updateCMSFields(FieldList $fields)
27
    {
28
        $fields->removeByName(array(
29
            'PurchaseLimit',
30
            'EmbargoLimit',
31
            'NumberPurchased',
32
        ));
33
34
        $fields->addFieldsToTab('Root.Inventory', array(
35
            CheckboxField::create('ControlInventory', 'Control Inventory?')
36
                ->setDescription('limit the number of this product available for purchase'),
37
            Wrapper::create(
38
                NumericField::create('PurchaseLimit')
39
                    ->setTitle('Number Available')
40
                    ->setDescription('add to cart form will be disabled once number available equals purchased'),
41
                ReadonlyField::create('NumberPurchased', 'Purchased', $this->getNumberPurchased())//,
42
            )->displayIf('ControlInventory')->isChecked()->end(),
43
        ));
44
    }
45
46
    /**
47
     * @return bool
48
     */
49
    public function getHasInventory()
50
    {
51
        return $this->owner->ControlInventory && $this->owner->PurchaseLimit != 0;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->owner->PurchaseLimit of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function getIsOptionAvailable()
58
    {
59
        if ($this->getHasInventory()) {
60
            return $this->owner->PurchaseLimit > $this->getNumberPurchased();
61
        }
62
        return true;
63
    }
64
65
    /**
66
     * @return int
67
     */
68
    public function getNumberAvailable()
69
    {
70
        if ($this->getIsOptionAvailable()) {
71
            return (int)$this->owner->PurchaseLimit - (int)$this->getNumberPurchased();
72
        }
73
    }
74
75
    /**
76
     * @return int
77
     */
78
    public function getNumberPurchased()
79
    {
80
        $ct = 0;
81
        if ($this->getOrders()) {
82
            foreach ($this->getOrders() as $order) {
83
                $ct += $order->Quantity;
84
            }
85
        }
86
        return $ct;
87
    }
88
89
    /**
90
     * @return DataList
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\ORM\DataList 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...
91
     */
92
    public function getOrders()
93
    {
94
        if ($this->owner->ID) {
95
            return OrderDetail::get()->filter('Options.ID', $this->owner->ID);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Dynamic\FoxyStrip....ID', $this->owner->ID) returns the type SilverStripe\ORM\DataList which is incompatible with the documented return type Dynamic\FoxyStripe\ORM\DataList.
Loading history...
96
        }
97
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type Dynamic\FoxyStripe\ORM\DataList.
Loading history...
98
    }
99
100
    /**
101
     * @param $available
102
     */
103
    public function updateOptionAvailability(&$available)
104
    {
105
        if ($this->getHasInventory()) {
106
            $available = $this->getIsOptionAvailable();
107
        }
108
    }
109
}