ShopUserInfo   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getLocation() 0 3 1
A setLocationData() 0 3 1
A getLocationData() 0 4 2
A getAddress() 0 10 2
A setLocation() 0 5 1
A setAddress() 0 5 1
1
<?php
2
3
namespace SilverShop;
4
5
use SilverShop\Model\Address;
6
use SilverStripe\Core\Injector\Injectable;
7
8
/**
9
 * Collects and stores data about the user. Keep this data in session.
10
 */
11
class ShopUserInfo
12
{
13
    use Injectable;
14
15
    /**
16
     * Get an array of location data
17
     *
18
     * @return array location data
19
     */
20
    public function getLocation()
21
    {
22
        return $this->getLocationData();
23
    }
24
25
    public function setLocation(array $location)
26
    {
27
        $this->setLocationData($location);
28
29
        return $this;
30
    }
31
32
    /**
33
     * Get location of user
34
     *
35
     * @param  Address $address location
36
     * @return null|Address
37
     */
38
    public function getAddress()
39
    {
40
        $address = null;
41
        if ($data = $this->getLocationData()) {
42
            $address = Address::create();
43
            $address->update($data);
44
            $address->ID = 0; //ensure not in db
45
        }
46
47
        return $address;
48
    }
49
50
    /**
51
     * Set location of user
52
     *
53
     * @param  Address $address location
54
     * @return $this
55
     */
56
    public function setAddress(Address $address)
57
    {
58
        $this->setLocationData($address->toMap());
59
60
        return $this;
61
    }
62
63
    protected function getLocationData()
64
    {
65
        $data = ShopTools::getSession()->get('UserInfo.Location');
66
        return is_array($data) ? $data : [];
67
    }
68
69
    protected function setLocationData(array $data)
70
    {
71
        ShopTools::getSession()->set('UserInfo.Location', $data);
72
    }
73
}
74