Passed
Pull Request — master (#63)
by
unknown
11:38
created

Sale::setData()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 15
ccs 0
cts 0
cp 0
rs 9.6111
cc 5
nc 4
nop 1
crap 30
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\sale;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\customer\CustomerInterface;
15
use hiqdev\php\billing\Exception\CannotReassignException;
16
use hiqdev\php\billing\Exception\ConstraintException;
17
use hiqdev\php\billing\Exception\InvariantException;
18
use hiqdev\php\billing\plan\PlanInterface;
19
use hiqdev\php\billing\target\TargetInterface;
20
21
/**
22
 * Sale.
23
 *
24
 * @author Andrii Vasyliev <[email protected]>
25
 */
26
class Sale implements SaleInterface
27
{
28
    /**
29
     * @var int
30
     */
31
    protected $id;
32
33
    /**
34
     * @var TargetInterface
35
     */
36
    protected $target;
37
38
    /**
39
     * @var CustomerInterface
40
     */
41
    protected $customer;
42
43
    /**
44
     * @var PlanInterface|null
45
     */
46
    protected $plan;
47
48
    /**
49
     * @var DateTimeImmutable
50
     */
51 15
    protected $time;
52
53
    protected ?DateTimeImmutable $closeTime = null;
54
55
    protected mixed $data = null;
56
57
    public function __construct(
58 15
        $id,
59 15
        TargetInterface $target,
60 15
        CustomerInterface $customer,
61 15
        ?PlanInterface $plan = null,
62 15
        ?DateTimeImmutable $time = null,
63 15
        mixed $data = null,
64
    ) {
65 4
        $this->id = $id;
66
        $this->target = $target;
67 4
        $this->customer = $customer;
68
        $this->plan = $plan;
69
        $this->time = $time ?? new DateTimeImmutable();
70 4
        $this->data = $this->setData($data);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->data is correct as $this->setData($data) targeting hiqdev\php\billing\sale\Sale::setData() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
    }
72 4
73
    public function getId()
74
    {
75 4
        return $this->id;
76
    }
77 4
78
    public function getTarget()
79
    {
80 6
        return $this->target;
81
    }
82 6
83
    public function getCustomer()
84
    {
85 12
        return $this->customer;
86
    }
87 12
88
    public function getPlan()
89
    {
90
        return $this->plan;
91
    }
92
93
    public function getTime()
94
    {
95
        return $this->time;
96
    }
97
98
    public function hasId()
99
    {
100
        return $this->id !== null;
101
    }
102
103
    public function getCloseTime(): ?DateTimeImmutable
104
    {
105
        return $this->closeTime;
106
    }
107
108
    public function close(DateTimeImmutable $closeTime): void
109
    {
110
        if ($this->closeTime !== null) {
111
            throw new InvariantException('Sale is already closed');
112
        }
113
114
        if ($closeTime < $this->time) {
115
            throw new ConstraintException('Sale close time MUST be greater than open time');
116
        }
117
118
        $this->closeTime = $closeTime;
119
    }
120
121
    public function setId($id)
122
    {
123
        if ((string) $this->id === (string) $id) {
124
            return;
125
        }
126
        if ($this->hasId()) {
127
            throw new CannotReassignException('sale id');
128
        }
129
        $this->id = $id;
130
    }
131
132
    public function setData(mixed $data = null)
133
    {
134
        if (is_null($data) || empty($data)) {
135
            return ;
136
        }
137
        if ($is_string($data)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $is_string seems to be never defined.
Loading history...
138
            $this->data = json_decode($data, true, 512, JSON_THROW_ON_ERROR);
139
            return ;
140
        }
141
142
        if (is_object($data)) {
143
            $this->data = json_decode(json_encode($data), true, 512, JSON_THROW_ON_ERROR);
144
        }
145
146
        $this->data = $data;
147
    }
148
149
    public function getData()
150
    {
151
        return $this->data;
152
    }
153
154
    public function jsonSerialize(): array
155
    {
156
        return array_filter(get_object_vars($this));
157
    }
158
159
    public function cancelClosing(): void
160
    {
161
        $this->closeTime = null;
162
    }
163
}
164