Passed
Push — master ( 4f0c15...22d199 )
by Laurens
01:48
created

Transaction::fromData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 9
crap 1

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
declare(strict_types=1);
4
5
namespace LauLamanApps\eCurring\Resource;
6
7
use DateTimeImmutable;
8
use LauLamanApps\eCurring\Http\Resource\Creatable;
9
use LauLamanApps\eCurring\Resource\Transaction\Event;
10
use LauLamanApps\eCurring\Resource\Transaction\PaymentMethod;
11
use LauLamanApps\eCurring\Resource\Transaction\Status;
12
use Money\Money;
13
use Ramsey\Uuid\UuidInterface;
14
15
final class Transaction implements TransactionInterface, Creatable
16
{
17
    /**
18
     * @var UuidInterface
19
     */
20
    private $id;
21
22
    /**
23
     * @var Status
24
     *
25
     * The current status of the transaction.
26
     */
27
    private $status;
28
29
    /**
30
     * @var DateTimeImmutable
31
     *
32
     * The date on which this transaction was scheduled
33
     * Generally this is the date on which the subscription was activated.
34
     */
35
    private $scheduledOn;
36
37
    /**
38
     * @var DateTimeImmutable|null
39
     *
40
     * The date on which the transaction will be, or was, executed.
41
     * Usually a transaction will be fulfilled within 3 days of this date
42
     * If a transaction was rescheduled after a charge back,
43
     * this attribute will reflect the date of the next attempt.
44
     */
45
    private $dueDate;
46
47
    /**
48
     * @var Money
49
     *
50
     * The amount of the transaction.
51
     */
52
    private $amount;
53
54
    /**
55
     * @var DateTimeImmutable|null
56
     *
57
     * The date on which the transaction was cancelled.
58
     * Only filled if $status is 'cancelled'
59
     */
60
    private $canceledOn;
61
62
    /**
63
     * @var string|null
64
     *
65
     * The webhook URL we will call when the status of this transaction changes,
66
     * inherited from the subscription.
67
     */
68
    private $webhookUrl;
69
70
    /**
71
     * @var PaymentMethod
72
     *
73
     * The payment method used for this transaction.
74
     * For fullfilled transactions this is the actual method used,
75
     * for future transactions this will be payment method used for the first attempt.
76
     * However, the method may change if the attempt fails and the transaction is
77
     * fulfilled through other means (a payment reminder, for example).
78
     */
79
    private $paymentMethod;
80
81
    /**
82
     * @var Event[]
83
     *
84
     * Collection of events with the full life cycle history of the transaction
85
     */
86
    private $history;
87
88
    /**
89
     * @var Subscription|null
90
     */
91
    private $subscription;
92
93 1
    private function __construct(Money $amount)
94
    {
95 1
        $this->amount = $amount;
96 1
        $this->history = [];
97 1
    }
98
99
    public static function new(
100
        Subscription $subscription,
101
        Money $amount,
102
        ?DateTimeImmutable $dueDate = null
103
    ): self {
104
        $self = new self($amount);
105
        $self->dueDate = $dueDate;
106
        $self->subscription = $subscription;
107
108
        return $self;
109
    }
110
111 1
    public static function fromData(
112
        UuidInterface $id,
113
        Status $status,
114
        DateTimeImmutable $scheduledOn,
115
        Money $amount,
116
        PaymentMethod $paymentMethod,
117
        ?DateTimeImmutable $dueDate = null,
118
        ?DateTimeImmutable $canceledOn = null,
119
        ?string $webhookUrl = null,
120
        ?Event ...$history
121
    ): self {
122 1
        $self = new self($amount);
123 1
        $self->id = $id;
124 1
        $self->status = $status;
125 1
        $self->scheduledOn = $scheduledOn;
126 1
        $self->dueDate = $dueDate;
127 1
        $self->amount = $amount;
128 1
        $self->canceledOn = $canceledOn;
129 1
        $self->webhookUrl = $webhookUrl;
130 1
        $self->paymentMethod = $paymentMethod;
131 1
        $self->history = $history;
132
133 1
        return $self;
134
    }
135
136 1
    public function getId(): ?UuidInterface
137
    {
138 1
        return $this->id;
139
    }
140
141 1
    public function getStatus(): ?Status
142
    {
143 1
        return $this->status;
144
    }
145
146 1
    public function getScheduledOn(): ?DateTimeImmutable
147
    {
148 1
        return $this->scheduledOn;
149
    }
150
151 1
    public function getDueDate(): ?DateTimeImmutable
152
    {
153 1
        return $this->dueDate;
154
    }
155
156 1
    public function getAmount(): Money
157
    {
158 1
        return $this->amount;
159
    }
160
161 1
    public function getCanceledOn(): ?DateTimeImmutable
162
    {
163 1
        return $this->canceledOn;
164
    }
165
166 1
    public function getWebhookUrl(): ?string
167
    {
168 1
        return $this->webhookUrl;
169
    }
170
171 1
    public function getPaymentMethod(): PaymentMethod
172
    {
173 1
        return $this->paymentMethod;
174
    }
175
176 1
    public function getHistory(): array
177
    {
178 1
        return $this->history;
179
    }
180
181
    public function getSubscription(): ?Subscription
182
    {
183
        return $this->subscription;
184
    }
185
}
186