Passed
Push — master ( 860fa1...33c253 )
by Will
11:47 queued 09:01
created

OrderItem::onPlacement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace SilverShop\Model\Variation;
4
5
use SilverStripe\Versioned\Versioned;
6
7
/**
8
 * Product Variation - Order Item
9
 * Connects a variation to an order, as a line in the order specifying the particular variation.
10
 *
11
 * @property int $ProductVariationVersion
12
 * @property int $ProductVariationID
13
 */
14
class OrderItem extends \SilverShop\Model\Product\OrderItem
15
{
16
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
17
        'ProductVariationVersion' => 'Int',
18
    ];
19
20
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
21
        'ProductVariation' => Variation::class
22
    ];
23
24
    private static $buyable_relationship = 'ProductVariation';
0 ignored issues
show
introduced by
The private property $buyable_relationship is not used, and could be removed.
Loading history...
25
26
    private static $table_name = 'SilverShop_Variation_OrderItem';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
27
28
    /**
29
     * Overloaded relationship, for getting versioned variations
30
     *
31
     * @param  boolean $current
32
     * @return Variation
33
     */
34
    public function ProductVariation($forcecurrent = false)
35
    {
36
        if ($this->ProductVariationID && $this->ProductVariationVersion && !$forcecurrent) {
37
            return Versioned::get_version(
38
                Variation::class,
39
                $this->ProductVariationID,
40
                $this->ProductVariationVersion
41
            );
42
        } elseif ($this->ProductVariationID
43
            && $product = Variation::get()->byID($this->ProductVariationID)
44
        ) {
45
            return $product;
46
        }
47
        return null;
48
    }
49
    
50
    public function onPlacement()
51
    {
52
        parent::onPlacement();
53
        if ($productVariation = $this->ProductVariation(true)) {
54
            $this->ProductVariationVersion = $productVariation->Version;
0 ignored issues
show
Bug Best Practice introduced by
The property Version does not exist on SilverShop\Model\Variation\Variation. Since you implemented __get, consider adding a @property annotation.
Loading history...
55
        }
56
    }
57
58
    public function SubTitle()
59
    {
60
        if ($this->ProductVariation()) {
61
            return $this->ProductVariation()->getTitle();
62
        }
63
        return false;
64
    }
65
66
    public function Image()
67
    {
68
        if (($variation = $this->ProductVariation()) && $variation->Image()->exists()) {
69
            return $variation->Image();
70
        }
71
        return $this->Product()->Image();
72
    }
73
74
    public function Width()
75
    {
76
        if (($variation = $this->ProductVariation()) && $variation->Width) {
77
            return $variation->Width;
78
        }
79
        return $this->Product()->Width;
80
    }
81
82
    public function Height()
83
    {
84
        if (($variation = $this->ProductVariation()) && $variation->Height) {
85
            return $variation->Height;
86
        }
87
        return $this->Product()->Height;
88
    }
89
90
    public function Depth()
91
    {
92
        if (($variation = $this->ProductVariation()) && $variation->Depth) {
93
            return $variation->Depth;
94
        }
95
        return $this->Product()->Depth;
96
    }
97
98
    public function Weight()
99
    {
100
        if (($variation = $this->ProductVariation()) && $variation->Weight) {
101
            return $variation->Weight;
102
        }
103
        return $this->Product()->Weight;
104
    }
105
}
106