Completed
Pull Request — master (#7)
by Jason
12:37
created

FoxyStripeInventoryManagerExtension   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 14
dl 0
loc 23
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A updateFoxyStripePurchaseForm() 0 18 4
1
<?php
2
3
namespace Dynamic\FoxyStripe\ORM;
4
5
use Dynamic\FoxyStripe\Page\ProductPage;
6
use SilverStripe\Core\Extension;
7
use SilverStripe\Forms\DropdownField;
8
9
class FoxyStripeInventoryManagerExtension extends Extension
10
{
11
    /**
12
     * @param $form
13
     */
14
    public function updateFoxyStripePurchaseForm(&$form)
15
    {
16
        if (!$this->owner->getIsProductAvailable()) {
17
            $form = false;
18
            return;
19
        }
20
21
        if ($this->owner->getHasInventory()) {
22
            $quantityMax = $this->owner->getNumberAvailable();
23
            $count = 1;
24
            $quantity = array();
25
            while ($count <= $quantityMax) {
26
                $countVal = ProductPage::getGeneratedValue($this->owner->Code, 'quantity', $count, 'value');
27
                $quantity[$countVal] = $count;
28
                $count++;
29
            }
30
            $fields = $form->Fields();
31
            $fields->replaceField('quantity', DropdownField::create('quantity', 'Quantity', $quantity));
32
        }
33
    }
34
}