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

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