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

PurchaseFormExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 45.83%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 38
ccs 11
cts 24
cp 0.4583
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updatePurchaseFormFields() 0 12 2
A updateFoxyStripePurchaseFormActions() 0 15 3
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