Completed
Push — master ( cdb107...3cb9e5 )
by Jason
9s
created

FoxyStripeInventoryManagerExtension   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 27.78%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 26
ccs 5
cts 18
cp 0.2778
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A updateFoxyStripePurchaseForm() 0 20 4
1
<?php
2
3
class FoxyStripeInventoryManager extends DataExtension
4
{
5
    /**
6
     * @var array
7
     */
8
    private static $db = [
9
        'ControlInventory' => 'Boolean',
10
        'PurchaseLimit' => 'Int',
11
        'EmbargoLimit' => 'Int',
12
    ];
13
14
    /**
15
     * @param FieldList $fields
16
     */
17 1
    public function updateCMSFields(FieldList $fields)
18
    {
19 1
        $fields->removeByName(array(
20 1
            'PurchaseLimit',
21 1
            'EmbargoLimit',
22 1
            'NumberPurchased',
23 1
        ));
24
25 1
        $fields->addFieldsToTab('Root.Inventory', array(
26 1
            CheckboxField::create('ControlInventory', 'Control Inventory?')
27 1
                ->setDescription('limit the number of this product available for purchase'),
28 1
            DisplayLogicWrapper::create(
29 1
                NumericField::create('PurchaseLimit')
30 1
                    ->setTitle('Number Available')
31 1
                    ->setDescription('add to cart form will be disabled once number available equals purchased'),
32 1
                ReadonlyField::create('NumberPurchased', 'Purchased', $this->getNumberPurchased())//,
33
                /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
34
                NumericField::create('EmbargoLimit')
35
                    ->setTitle('Embargo Time')
36
                    ->setDescription('time in seconds to reserve an item once added to cart')
37
                */
38 1
            )->displayIf('ControlInventory')->isChecked()->end(),
39 1
        ));
40 1
    }
41
42
    /**
43
     * @return bool
44
     */
45 1
    public function getHasInventory()
46
    {
47 1
        return $this->owner->ControlInventory && $this->owner->PurchaseLimit != 0;
48
    }
49
50
    /**
51
     * @return bool
52
     */
53 1
    public function getIsProductAvailable()
54
    {
55 1
        if ($this->owner->getHasInventory()) {
56 1
            return $this->owner->PurchaseLimit > $this->getNumberPurchased();
57
        }
58
        return true;
59
    }
60
61
    public function getNumberAvailable()
62
    {
63
        if ($this->getIsProductAvailable()) {
64
            return (int)$this->owner->PurchaseLimit - (int)$this->getNumberPurchased();
65
        }
66
67
    }
68
69
    /**
70
     * @return int
71
     */
72 2
    public function getNumberPurchased()
73
    {
74 2
        $ct = 0;
75 2
        if ($this->getOrders()) {
76 2
            foreach ($this->getOrders() as $order) {
77 2
                $ct += $order->Quantity;
78 2
            }
79 2
        }
80
81 2
        return $ct;
82
    }
83
84
    /**
85
     * @return DataList
86
     */
87 2
    public function getOrders()
88
    {
89 2
        if ($this->owner->ID) {
90 2
            return OrderDetail::get()->filter('ProductID', $this->owner->ID);
91
        }
92
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by FoxyStripeInventoryManager::getOrders of type DataList.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
93
    }
94
}
95
96
class FoxyStripeInventoryManagerExtension extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
97
{
98
    /**
99
     * @param $form
100
     */
101 1
    public function updateFoxyStripePurchaseForm(&$form)
102
    {
103 1
        if (!$this->owner->getIsProductAvailable()) {
104 1
            $form = false;
105 1
            return;
106 1
        }
107
108
        if ($this->owner->getHasInventory()) {
109
            $quantityMax = $this->owner->getNumberAvailable();
110
            $count = 1;
111
            $quantity = array();
112
            while ($count <= $quantityMax) {
113
                $countVal = ProductPage::getGeneratedValue($this->owner->Code, 'quantity', $count, 'value');
114
                $quantity[$countVal] = $count;
115
                $count++;
116
            }
117
            $fields = $form->Fields();
118
            $fields->replaceField('quantity', DropdownField::create('quantity', 'Quantity', $quantity));
119
        }
120
    }
121
}