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

FoxyStripeInventoryManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 32
dl 0
loc 93
rs 10
c 0
b 0
f 0

6 Methods

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