Passed
Pull Request — master (#73)
by
unknown
10:51
created

Customer::fromArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 10
cc 3
nc 2
nop 1
crap 12
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
    /**
41
     * @var string|null
42 40
     */
43 40
    protected ?string $state = null;
44 40
45 40
    public function __construct($id, $login, CustomerInterface $seller = null, ?string $state = null)
46
    {
47 1
        $this->id = $id;
48
        $this->login = $login;
49 1
        $this->seller = $seller;
50
        $this->state = $state;
51
    }
52
53
    public function getId()
54
    {
55 1
        return $this->id;
56
    }
57 1
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getUniqueId()
62
    {
63 6
        return $this->getId() ?: $this->getLogin();
64
    }
65 6
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getLogin()
70
    {
71 5
        return $this->login;
72
    }
73 5
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getSeller()
78
    {
79
        return $this->seller;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getState(): ?string
86
    {
87
        return $this->state;
88
    }
89
90
    public function isDeleted(): bool
91
    {
92
        return CustomerState::isDeleted($this);
93
    }
94
95
    public static function fromArray(array $info)
96
    {
97
        if (!empty($info['seller_id']) && !empty($info['seller'])) {
98
            $seller = new static($info['seller_id'], $info['seller']);
99
        } else {
100
            $seller = null;
101
        }
102
103
        return new static($info['id'], $info['login'], $seller, $info['state'] ?? null);
104
    }
105
106
    public function jsonSerialize(): array
107
    {
108
        return array_filter(get_object_vars($this));
109
    }
110
}
111