Passed
Push — master ( a7c50c...ab63ad )
by Nic
03:11
created

ProductDataExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 6
eloc 11
dl 0
loc 55
rs 10
c 4
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDiscountPrice() 0 7 2
A updateCMSFields() 0 2 1
A getBestDiscount() 0 3 1
A getHasDiscount() 0 7 2
1
<?php
2
3
namespace Dynamic\Foxy\Discounts\Extension;
4
5
use Dynamic\Foxy\Discounts\DiscountHelper;
6
use Dynamic\Foxy\Discounts\Model\Discount;
7
use Dynamic\Foxy\Discounts\Model\DiscountTier;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\ORM\FieldType\DBCurrency;
11
use SilverStripe\ORM\HasManyList;
12
13
/**
14
 * Class ProductDataExtension
15
 * @package Dynamic\Foxy\Discounts\Extension
16
 */
17
class ProductDataExtension extends DataExtension
18
{
19
    /**
20
     * @var string[]
21
     */
22
    private static $db = [
1 ignored issue
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
23
        //'ExcludeFromDiscounts' => 'Boolean',
24
    ];
25
26
    /**
27
     * @var array
28
     */
29
    private static $belongs_many_many = [
0 ignored issues
show
introduced by
The private property $belongs_many_many is not used, and could be removed.
Loading history...
30
        'Discounts' => Discount::class,
31
    ];
32
33
    public function updateCMSFields(FieldList $fields)
34
    {
35
        /*$fields->addFieldToTab(
36
            'Root.'
37
        );//*/
38
    }
39
40
    /**
41
     * @param int $quantity
42
     * @return DiscountHelper
43
     */
44
    public function getBestDiscount($quantity = 1)
45
    {
46
        return DiscountHelper::create($this->owner, $quantity);
47
    }
48
49
    /**
50
     * @param int $quantity
51
     * @return false|DBCurrency
52
     */
53
    public function getDiscountPrice($quantity = 1)
54
    {
55
        if ($discount = $this->getBestDiscount($quantity)) {
56
            return $discount->getDiscountedPrice();
57
        }
58
59
        return false;
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function getHasDiscount()
66
    {
67
        if ($discount = $this->getBestDiscount()) {
68
            return $discount->getDiscountTier() instanceof DiscountTier;
69
        }
70
71
        return false;
72
    }
73
}
74