Passed
Pull Request — master (#8)
by Jason
02:23
created

QuantityFieldExtension::onBeforeRender()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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