Completed
Push — master ( 32b4c2...be6d62 )
by Matthew
22:47 queued 07:45
created

QuantityFieldExtension::updateQuantity()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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