MemberExtension::setCart()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
namespace SilverCommerce\ShoppingCart\Extensions;
4
5
use SilverStripe\ORM\DataExtension;
6
use SilverCommerce\ShoppingCart\Model\ShoppingCart;
7
8
/**
9
 * Customise Member objects
10
 * 
11
 */
12
class MemberExtension extends DataExtension
13
{
14
    /**
15
     * Get the currenty active shopping cart on a member
16
     * 
17
     * @return ShoppingCart
18
     */
19
    public function getCart()
20
    {
21
        return $this
22
            ->getOwner()
23
            ->Contact()
24
            ->Estimates()
25
            ->find("ClassName", ShoppingCart::class);
26
    }
27
28
    /**
29
     * Update the current cart. Also make sure no more than one is
30
     * set at any one time.
31
     * 
32
     * @return self
33
     */
34
    public function setCart(ShoppingCart $cart)
35
    {
36
        $curr = $this->getOwner()->getCart();
37
        $contact = $this->getOwner()->Contact();
38
39
        if (isset($curr) && $curr->ID != $cart->ID) {
40
            $curr->delete();
41
        }
42
43
        $cart->CustomerID = $contact->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property CustomerID does not exist on SilverCommerce\ShoppingCart\Model\ShoppingCart. Since you implemented __set, consider adding a @property annotation.
Loading history...
44
45
        return $this;
46
    }
47
}
48