QuantityFieldExtension   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 19
c 1
b 0
f 0
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onBeforeRender() 0 11 3
A updateQuantity() 0 10 4
A updateData() 0 9 3
1
<?php
2
3
namespace Dynamic\Foxy\Inventory\Extension;
4
5
use Dynamic\Foxy\Form\QuantityField;
6
use SilverStripe\Core\Extension;
7
8
/**
9
 * Class QuantityFieldExtension
10
 * @package Dynamic\Foxy\Inventory\Extension
11
 * @property-read QuantityField $owner
12
 */
13
class QuantityFieldExtension extends Extension
14
{
15
    /**
16
     *
17
     */
18
    public function onBeforeRender()
19
    {
20
        if (!$this->owner->getProduct()->hasMethod('getHasInventory')) {
21
            return;
22
        }
23
        if (!$this->owner->getProduct()->getHasInventory()) {
24
            return;
25
        }
26
        $this->owner->setAttribute(
27
            'data-limit',
28
            $this->owner->getProduct()->getNumberAvailable()
29
        );
30
    }
31
32
    /**
33
     * Limit the quantity to the number available
34
     * @param $quantity
35
     */
36
    public function updateQuantity(&$quantity)
37
    {
38
        if (!$this->owner->getProduct()->hasMethod('getHasInventory')) {
39
            return;
40
        }
41
        if (!$this->owner->getProduct()->getHasInventory()) {
42
            return;
43
        }
44
        if ($quantity >= $this->owner->getProduct()->getNumberAvailable()) {
45
            $quantity = $this->owner->getProduct()->getNumberAvailable();
46
        }
47
    }
48
49
    /**
50
     * Adds limit
51
     * @param $data
52
     */
53
    public function updateData(&$data)
54
    {
55
        if (!$this->owner->getProduct()->hasMethod('getHasInventory')) {
56
            return;
57
        }
58
        if (!$this->owner->getProduct()->getHasInventory()) {
59
            return;
60
        }
61
        $data['limit'] = (int) $this->owner->getProduct()->getNumberAvailable();
62
    }
63
}
64