Completed
Pull Request — master (#6)
by Jason
03:36
created

getNumberAvailable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
3
class FoxyStripeOptionInventoryManager extends DataExtension
4
{
5
    /**
6
     * @var array
7
     */
8
    private static $db = [
9
        'ControlInventory' => 'Boolean',
10
        'PurchaseLimit' => 'Int',
11
    ];
12
13
    /**
14
     * @param FieldList $fields
15
     */
16 View Code Duplication
    public function updateCMSFields(FieldList $fields)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
    {
18
        $fields->removeByName(array(
19
            'PurchaseLimit',
20
            'EmbargoLimit',
21
            'NumberPurchased',
22
        ));
23
24
        $fields->addFieldsToTab('Root.Inventory', array(
25
            CheckboxField::create('ControlInventory', 'Control Inventory?')
26
                ->setDescription('limit the number of this product available for purchase'),
27
            DisplayLogicWrapper::create(
28
                NumericField::create('PurchaseLimit')
29
                    ->setTitle('Number Available')
30
                    ->setDescription('add to cart form will be disabled once number available equals purchased'),
31
                ReadonlyField::create('NumberPurchased', 'Purchased', $this->getNumberPurchased())//,
32
            )->displayIf('ControlInventory')->isChecked()->end(),
33
        ));
34
    }
35
36
    /**
37
     * @return bool
38
     */
39
    public function getHasInventory()
40
    {
41
        return $this->owner->ControlInventory && $this->owner->PurchaseLimit != 0;
42
    }
43
44
    /**
45
     * @return bool
46
     */
47
    public function getIsOptionAvailable()
48
    {
49
        if ($this->getHasInventory()) {
50
            return $this->owner->PurchaseLimit > $this->getNumberPurchased();
51
        }
52
        return true;
53
    }
54
55
    /**
56
     * @return int
57
     */
58
    public function getNumberAvailable()
59
    {
60
        if ($this->getIsOptionAvailable()) {
61
            return (int)$this->owner->PurchaseLimit - (int)$this->getNumberPurchased();
62
        }
63
    }
64
65
    /**
66
     * @return int
67
     */
68 View Code Duplication
    public function getNumberPurchased()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        $ct = 0;
71
        if ($this->getOrders()) {
72
            foreach ($this->getOrders() as $order) {
73
                $ct += $order->Quantity;
74
            }
75
        }
76
        return $ct;
77
    }
78
79
    /**
80
     * @return DataList
81
     */
82
    public function getOrders()
83
    {
84
        if ($this->owner->ID) {
85
            return OrderDetail::get()->filter('Options.ID', $this->owner->ID);
86
        }
87
        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 FoxyStripeOptionInventoryManager::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...
88
    }
89
90
    /**
91
     * @param $available
92
     */
93
    public function updateOptionAvailability(&$available)
94
    {
95
        if ($this->getHasInventory()) {
96
            $available = $this->getIsOptionAvailable();
97
        }
98
    }
99
}