Completed
Pull Request — master (#1)
by Jason
05:12
created

getIsProductAvailable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 3
nop 0
crap 3
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->addFieldsToTab('Root.Inventory', array(
20 1
            CheckboxField::create('ControlInventory', 'Control Inventory?')
21 1
                ->setDescription('limit the number of this product available for purchase'),
22 1
            NumericField::create('PurchaseLimit')
23 1
                ->setTitle('Number Available')
24 1
                ->setDescription('add to cart form will be disabled once number available equals purchased'),
25 1
            ReadonlyField::create('NumberPurchased', 'Purchased', $this->getNumberPurchased()),
26 1
            NumericField::create('EmbargoLimit')
27 1
                ->setTitle('Embargo Time')
28 1
                ->setDescription('time in seconds to reserve an item once added to cart'),
29 1
        ));
30 1
    }
31
32
    /**
33
     * @return int
34
     */
35 2
    public function getNumberPurchased()
36
    {
37 2
        $ct = 0;
38 2
        if ($this->getOrders()) {
39 2
            foreach ($this->getOrders() as $order) {
40 2
                $ct += $order->Quantity;
41 2
            }
42 2
        }
43
44 2
        return $ct;
45
    }
46
47
    /**
48
     * @return DataList
49
     */
50 2
    public function getOrders()
51
    {
52 2
        if ($this->owner->ID) {
53 2
            return OrderDetail::get()->filter('ProductID', $this->owner->ID);
54
        }
55
        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...
56
    }
57
58
    /**
59
     * @return bool
60
     */
61 1
    public function getIsProductAvailable()
62
    {
63 1
        return $this->owner->ControlInventory && $this->owner->PurchaseLimit != 0 && $this->owner->PurchaseLimit < $this->getNumberPurchased();
64
    }
65
}
66
67
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...
68
{
69
    /**
70
     * @param $form
71
     */
72 1
    public function updateFoxyStripePurchaseForm(&$form)
73
    {
74 1
        if (!$this->owner->getIsProductAvailable()) {
75 1
            $form = false;
76 1
        }
77
    }
78
}