Completed
Push — master ( d99711...5ac685 )
by Dmitry
03:31
created

AbstractAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 9
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 1
rs 9.9666
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\action;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\customer\CustomerInterface;
15
use hiqdev\php\billing\EntityInterface;
16
use hiqdev\php\billing\sale\SaleInterface;
17
use hiqdev\php\billing\target\TargetInterface;
18
use hiqdev\php\billing\type\TypeInterface;
19
use hiqdev\php\units\QuantityInterface;
20
21
/**
22
 * Chargeable Action.
23
 *
24
 * @see ActionInterface
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28
abstract class AbstractAction implements ActionInterface, EntityInterface
29
{
30
    /** @var int */
31
    protected $id;
32
33
    /** @var TypeInterface */
34
    protected $type;
35
36
    /** @var TargetInterface */
37
    protected $target;
38
39
    /** @var QuantityInterface */
40
    protected $quantity;
41
42
    /** @var CustomerInterface */
43
    protected $customer;
44
45
    /** @var DateTimeImmutable */
46
    protected $time;
47
48
    /** @var SaleInterface */
49
    protected $sale;
50
51
    /** @var ActionState */
52
    protected $state;
53
54
    /** @var ActionInterface */
55
    protected $parent;
56
57
    /**
58
     * @param TypeInterface $type
59
     * @param TargetInterface $target
60
     * @param QuantityInterface $quantity
61
     * @param CustomerInterface $customer
62
     * @param SaleInterface $sale
63
     * @param DateTimeImmutable $time
64
     * @param ActionInterface $parent
65
     */
66 25
    public function __construct(
67
        $id,
68
        TypeInterface $type,
69
        TargetInterface $target,
70
        QuantityInterface $quantity,
71
        CustomerInterface $customer,
72
        DateTimeImmutable $time,
73
        SaleInterface $sale = null,
74
        ActionState $state = null,
75
        ActionInterface $parent = null
76
    ) {
77 25
        $this->id       = $id;
78 25
        $this->type     = $type;
79 25
        $this->target   = $target;
80 25
        $this->quantity = $quantity;
81 25
        $this->customer = $customer;
82 25
        $this->time     = $time;
83 25
        $this->sale     = $sale;
84 25
        $this->state    = $state;
85 25
        $this->parent   = $parent;
86 25
    }
87
88
    /**
89
     * Provides unique string.
90
     * Can be used to compare or aggregate actions.
91
     */
92
    public function getUniqueString(): string
93
    {
94
        $parts = [
95
            'buyer'     => $this->customer->getUniqueId(),
96
            'target'    => $this->target ? $this->target->getUniqueId() : null,
97
            'type'      => $this->type->getUniqueId(),
98
            'time'      => $this->time->format('c'),
99
        ];
100
101
        return implode('-', $parts);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function getId()
108
    {
109
        return $this->id;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 1
    public function getCustomer(): CustomerInterface
116
    {
117 1
        return $this->customer;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 21
    public function getTarget(): TargetInterface
124
    {
125 21
        return $this->target;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 21
    public function getType(): TypeInterface
132
    {
133 21
        return $this->type;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 21
    public function getQuantity(): QuantityInterface
140
    {
141 21
        return $this->quantity;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 2
    public function getSale(): ?SaleInterface
148
    {
149 2
        return $this->sale;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 14
    public function getTime(): DateTimeImmutable
156
    {
157 14
        return $this->time;
158
    }
159
160
    public function setTime(DateTimeImmutable $time)
161
    {
162
        $this->time = $time;
163
    }
164
165
    public function getState(): ?ActionState
166
    {
167
        return $this->state;
168
    }
169
170
    public function setFinished(): void
171
    {
172
        $this->state = ActionState::finished();
173
    }
174
175 4
    public function isFinished(): ?bool
176
    {
177 4
        return $this->state === null ? null : $this->state->isFinished();
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function getParent(): ?ActionInterface
184
    {
185
        return $this->parent;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function hasParent()
192
    {
193
        return $this->parent !== null;
194
    }
195
196
    public function hasId()
197
    {
198
        return $this->id !== null;
199
    }
200
201
    public function setId($id)
202
    {
203
        if ((string) $this->id === (string) $id) {
204
            return;
205
        }
206
        if ($this->hasId()) {
207
            throw new \Exception('cannot reassign action id');
208
        }
209
        $this->id = $id;
210
    }
211
212 8
    public function hasSale()
213
    {
214 8
        return $this->sale !== null;
215
    }
216
217 8
    public function setSale(SaleInterface $sale)
218
    {
219 8
        if ($this->hasSale()) {
220
            throw new \Exception('cannot reassign sale for action');
221
        }
222 8
        $this->sale = $sale;
223 8
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    public function jsonSerialize()
229
    {
230
        return get_object_vars($this);
231
    }
232
}
233