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

ProductDataExtension::getDiscountPrice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
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