Completed
Push — master ( 002f4d...61bfd8 )
by Matthew
22:24 queued 07:35
created

src/ORM/FoxyStripeInventoryManager.php (7 issues)

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
/**
16
 * Class FoxyStripeInventoryManager
17
 * @package Dynamic\FoxyStripe\ORM
18
 *
19
 * @property boolean $ControlInventory
20
 * @property int $PurchaseLimit
21
 * @property int $EmbargoLimit
22
 *
23
 * @property-read \Dynamic\FoxyStripe\Page\ProductPage|\Dynamic\FoxyStripe\ORM\FoxyStripeInventoryManager $owner
24
 */
25
class FoxyStripeInventoryManager extends DataExtension
26
{
27
    /**
28
     * @var array
29 1
     */
30
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
31 1
        'ControlInventory' => 'Boolean',
32 1
        'PurchaseLimit' => 'Int',
33
        'EmbargoLimit' => 'Int',
34
    ];
35
36
    /**
37 1
     * @param FieldList $fields
38 1
     */
39 1
    public function updateCMSFields(FieldList $fields)
40 1
    {
41 1
        $fields->removeByName(array(
42 1
            'PurchaseLimit',
43 1
            'EmbargoLimit',
44 1
            'NumberPurchased',
45
        ));
46
47
        $fields->addFieldsToTab('Root.Inventory', array(
48
            CheckboxField::create('ControlInventory', 'Control Inventory?')
49
                ->setDescription('limit the number of this product available for purchase'),
50 1
            Wrapper::create(
51
                NumericField::create('PurchaseLimit')
52
                    ->setTitle('Number Available')
53
                    ->setDescription('add to cart form will be disabled once number available equals purchased'),
54
                ReadonlyField::create('NumberPurchased', 'Purchased', $this->getNumberPurchased())//,
55
                /*
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...
56
                NumericField::create('EmbargoLimit')
57 1
                ->setTitle('Embargo Time')
58
                ->setDescription('time in seconds to reserve an item once added to cart')
59 1
                */
60
            )->displayIf('ControlInventory')->isChecked()->end(),
61
        ));
62
    }
63
64
    /**
65 1
     * @return bool
66
     */
67 1
    public function getHasInventory()
68 1
    {
69
        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 integer|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...
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function getIsProductAvailable()
76 1
    {
77
        if ($this->owner->getHasInventory()) {
78 1
            return $this->owner->PurchaseLimit > $this->getNumberPurchased();
79 1
        }
80
        return true;
81
    }
82
83
    /**
84
     * @return int
85
     */
86 2
    public function getNumberAvailable()
87
    {
88 2
        if ($this->getIsProductAvailable()) {
89 2
            return (int)$this->owner->PurchaseLimit - (int)$this->getNumberPurchased();
90
        }
91 2
    }
92 2
93 2
    /**
94
     * @return int
95
     */
96
    public function getNumberPurchased()
97
    {
98 2
        $ct = 0;
99
        if ($this->getOrders()) {
100
            /** @var OrderDetail $order */
101
            foreach ($this->getOrders() as $order) {
102
                if ($order->OrderID !== 0) {
103
                    $ct += $order->Quantity;
104 2
                }
105
            }
106 2
        }
107 2
108
        return $ct;
109
    }
110
111
    /**
112
     * @return DataList
0 ignored issues
show
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...
113
     */
114
    public function getOrders()
115
    {
116
        if ($this->owner->ID) {
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist on Dynamic\FoxyStripe\ORM\FoxyStripeInventoryManager. Did you maybe forget to declare it?
Loading history...
117
            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...
118
        }
119
        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...
120
    }
121
}
122