Passed
Pull Request — master (#73)
by
unknown
11:59
created

Customer::setState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\customer;
12
13
/**
14
 * Customer.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class Customer implements CustomerInterface
19
{
20
    /**
21
     * @var integer
22
     */
23
    protected $id;
24
25
    /**
26
     * @var string
27
     */
28
    protected $login;
29
30
    /**
31
     * @var CustomerInterface
32
     */
33
    protected $seller;
34
35
    /**
36
     * @var Customer[]
37
     */
38
    protected $sellers = [];
39
40 40
    protected ?CustomerState $state = null;
41
42 40
    public function __construct($id, $login, CustomerInterface $seller = null, ?CustomerState $state = null)
43 40
    {
44 40
        $this->id = $id;
45 40
        $this->login = $login;
46
        $this->seller = $seller;
47 1
        $this->state = $state;
48
    }
49 1
50
    public function getId()
51
    {
52
        return $this->id;
53
    }
54
55 1
    /**
56
     * {@inheritdoc}
57 1
     */
58
    public function getUniqueId()
59
    {
60
        return $this->getId() ?: $this->getLogin();
61
    }
62
63 6
    /**
64
     * {@inheritdoc}
65 6
     */
66
    public function getLogin()
67
    {
68
        return $this->login;
69
    }
70
71 5
    /**
72
     * {@inheritdoc}
73 5
     */
74
    public function getSeller()
75
    {
76
        return $this->seller;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getState(): ?CustomerState
83
    {
84
        return $this->state;
85
    }
86
87
    public function setState(CustomerState $state): self
88
    {
89
        $this->state = $state;
90
91
        return $this;
92
    }
93
94
    public function isDeleted(): bool
95
    {
96
        return CustomerState::isDeleted($this);
97
    }
98
99
    public static function fromArray(array $info)
100
    {
101
        if (!empty($info['seller_id']) && !empty($info['seller'])) {
102
            $seller = new static($info['seller_id'], $info['seller']);
103
        } else {
104
            $seller = null;
105
        }
106
107
        return new static(
108
            $info['id'],
109
            $info['login'],
110
            $seller,
111
            isset($info['state']) ? CustomerState::from($info['state']) : null,
112
        );
113
    }
114
115
    public function jsonSerialize(): array
116
    {
117
        return array_filter(get_object_vars($this));
118
    }
119
}
120