Completed
Push — master ( 12bdcf...32b4c2 )
by Nic
04:00
created

PurchaseFormExtension::updatePurchaseFormFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4.1909

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 12
ccs 2
cts 11
cp 0.1818
crap 4.1909
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\FoxyStripe\Extension;
4
5
use Dynamic\FoxyStripe\Model\ProductCartReservation;
6
use Dynamic\FoxyStripe\Page\ProductPage;
7
use SilverStripe\Core\Extension;
8
use SilverStripe\Dev\Debug;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\HeaderField;
11
use SilverStripe\Forms\HiddenField;
12
13
/**
14
 * Class ProductFormExtension
15
 * @package Dynamic\Sheeps\ProductCartExpiry\Extension
16
 */
17
class PurchaseFormExtension extends Extension
18
{
19 1
    public function updatePurchaseFormFields(FieldList $fields)
20
    {
21 1
        if ($this->owner->getProduct()->CartExpiration) {
22
            $fields->insertBefore(
23
                'quantity',
24
                HiddenField::create('expires')
25
                    ->setValue(
26
                        ProductPage::getGeneratedValue(
27
                            $this->owner->getProduct()->Code,
28
                            'expires',
29
                            $this->owner->getProduct()->ExpirationMinutes,
30
                            'value'
31
                        )
32
                    )
33
            );
34
        }
35
    }
36
37
    /**
38
     * @param FieldList $fields
39
     */
40 1
    public function updateFoxyStripePurchaseFormActions(FieldList $fields)
41
    {
42 1
        if ($this->owner->getProduct()->ControlInventory) {
43 1
            $reserved = ProductCartReservation::get()
44 1
                ->filter([
45 1
                    'Code' => $this->owner->getProduct()->Code,
46 1
                    'Expires:GreaterThan' => date('Y-m-d H:i:s', strtotime('now')),
47 1
                ])->count();
48 1
            $sold = $this->owner->getProduct()->getNumberPurchased();
49
50 1
            if ($reserved + $sold >= $this->owner->getProduct()->PurchaseLimit) {
51
                $fields->replaceField(
52
                    'action_',
53
                    HeaderField::create('OutOfStock', 'Out of stock')
54
                        ->setHeadingLevel(3)
55
                );
56
            }
57
        }
58
    }
59
}
60