Completed
Push — master ( 2e8f8e...8bcbf4 )
by Matthew
03:03
created

FoxyStripeInventoryManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 13
eloc 33
dl 0
loc 95
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHasInventory() 0 3 2
A getIsProductAvailable() 0 6 2
A updateCMSFields() 0 22 1
A getOrders() 0 6 2
A getNumberPurchased() 0 13 4
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 1
    public function updateCMSFields(FieldList $fields)
30
    {
31 1
        $fields->removeByName(array(
32 1
            'PurchaseLimit',
33
            'EmbargoLimit',
34
            'NumberPurchased',
35
        ));
36
37 1
        $fields->addFieldsToTab('Root.Inventory', array(
38 1
            CheckboxField::create('ControlInventory', 'Control Inventory?')
39 1
                ->setDescription('limit the number of this product available for purchase'),
40 1
            Wrapper::create(
41 1
                NumericField::create('PurchaseLimit')
42 1
                    ->setTitle('Number Available')
43 1
                    ->setDescription('add to cart form will be disabled once number available equals purchased'),
44 1
                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 1
            )->displayIf('ControlInventory')->isChecked()->end(),
51
        ));
52
    }
53
54
    /**
55
     * @return bool
56
     */
57 1
    public function getHasInventory()
58
    {
59 1
        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 1
    public function getIsProductAvailable()
66
    {
67 1
        if ($this->owner->getHasInventory()) {
68 1
            return $this->owner->PurchaseLimit > $this->getNumberPurchased();
69
        }
70
        return true;
71
    }
72
73
    /**
74
     * @return int
75
     */
76 1
    public function getNumberAvailable()
77
    {
78 1
        if ($this->getIsProductAvailable()) {
79 1
            return (int)$this->owner->PurchaseLimit - (int)$this->getNumberPurchased();
80
        }
81
    }
82
83
    /**
84
     * @return int
85
     */
86 2
    public function getNumberPurchased()
87
    {
88 2
        $ct = 0;
89 2
        if ($this->getOrders()) {
90
            /** @var OrderDetail $order */
91 2
            foreach ($this->getOrders() as $order) {
92 2
                if ($order->OrderID !== 0) {
93 2
                    $ct += $order->Quantity;
94
                }
95
            }
96
        }
97
98 2
        return $ct;
99
    }
100
101
    /**
102
     * @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...
103
     */
104 2
    public function getOrders()
105
    {
106 2
        if ($this->owner->ID) {
107 2
            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...
108
        }
109
        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...
110
    }
111
}
112