Passed
Push — master ( 6e29fd...6a8746 )
by Will
02:54
created

OrderItem::getTableTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace SilverShop\Model\Product;
4
5
use SilverShop\Page\Product;
6
use SilverStripe\Versioned\Versioned;
7
8
/**
9
 * Product - Order Item
10
 * Connects a product to an orde.
11
 *
12
 * @property int $ProductVersion
13
 * @property int $ProductID
14
 */
15
class OrderItem extends \SilverShop\Model\OrderItem
16
{
17
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
18
        'ProductVersion' => 'Int',
19
    ];
20
21
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
22
        'Product' => Product::class,
23
    ];
24
25
    private static $table_name = 'SilverShop_Product_OrderItem';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
26
27
    /**
28
     * the has_one join field to identify the buyable
29
     */
30
    private static $buyable_relationship = 'Product';
0 ignored issues
show
introduced by
The private property $buyable_relationship is not used, and could be removed.
Loading history...
31
32
    /**
33
     * Get related product
34
     *  - live version if in cart, or
35
     *  - historical version if order is placed
36
     *
37
     * @param boolean $forcecurrent - force getting latest version of the product.
38
     *
39
     * @return Product
40
     */
41
    public function Product($forcecurrent = false)
42
    {
43
        //TODO: this might need some unit testing to make sure it compliles with comment description
44
        //ie use live if in cart (however I see no logic for checking cart status)
45
        if ($this->ProductID && $this->ProductVersion && !$forcecurrent) {
46
            return Versioned::get_version(Product::class, $this->ProductID, $this->ProductVersion);
0 ignored issues
show
Bug Best Practice introduced by
The expression return SilverStripe\Vers... $this->ProductVersion) returns the type SilverStripe\ORM\DataObject which is incompatible with the documented return type SilverShop\Page\Product.
Loading history...
47
        } elseif ($this->ProductID
48
            && $product = Versioned::get_one_by_stage(
49
                Product::class,
50
                'Live',
51
                '"SilverShop_Product"."ID"  = ' . $this->ProductID
52
            )
53
        ) {
54
            return $product;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $product returns the type SilverStripe\ORM\DataObject which is incompatible with the documented return type SilverShop\Page\Product.
Loading history...
55
        }
56
        return null;
57
    }
58
59
    public function onPlacement()
60
    {
61
        parent::onPlacement();
62
        if ($product = $this->Product(true)) {
63
            $this->ProductVersion = $product->Version;
64
        }
65
    }
66
67
    public function getTableTitle()
68
    {
69
        $product = $this->Product();
70
        $tabletitle = ($product) ? $product->Title : $this->i18n_singular_name();
0 ignored issues
show
introduced by
$product is of type SilverShop\Page\Product, thus it always evaluated to true.
Loading history...
71
        $this->extend('updateTableTitle', $tabletitle);
72
73
        return $tabletitle;
74
    }
75
76
    public function Link()
77
    {
78
        if ($product = $this->Product()) {
79
            return $product->Link();
80
        }
81
    }
82
}
83