-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApplicationInstallation.php
344 lines (295 loc) · 10.4 KB
/
ApplicationInstallation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
declare(strict_types=1);
namespace Bitrix24\Lib\ApplicationInstallations\Entity;
use Bitrix24\Lib\AggregateRoot;
use Bitrix24\SDK\Application\ApplicationStatus;
use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Entity\ApplicationInstallationInterface;
use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Entity\ApplicationInstallationStatus;
use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Events;
use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Events\ApplicationInstallationBlockedEvent;
use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Events\ApplicationInstallationCreatedEvent;
use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Events\ApplicationInstallationFinishedEvent;
use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Events\ApplicationInstallationUnblockedEvent;
use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Events\ApplicationInstallationUninstalledEvent;
use Bitrix24\SDK\Application\PortalLicenseFamily;
use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
use Carbon\CarbonImmutable;
use Symfony\Component\Uid\Uuid;
class ApplicationInstallation extends AggregateRoot implements ApplicationInstallationInterface
{
public function __construct(
private readonly Uuid $id,
private ApplicationInstallationStatus $status,
private readonly CarbonImmutable $createdAt,
private CarbonImmutable $updatedAt,
private readonly Uuid $bitrix24AccountId,
private ApplicationStatus $applicationStatus,
private PortalLicenseFamily $portalLicenseFamily,
private ?int $portalUsersCount,
private ?Uuid $contactPersonId,
private ?Uuid $bitrix24PartnerContactPersonId,
private ?Uuid $bitrix24PartnerId,
private ?string $externalId,
private ?string $comment = null,
private $isEmitApplicationInstallationCreatedEvent = false,
) {
$this->addApplicationCreatedEventIfNeeded($this->isEmitApplicationInstallationCreatedEvent);
}
#[\Override]
public function getId(): Uuid
{
return $this->id;
}
#[\Override]
public function getCreatedAt(): CarbonImmutable
{
return $this->createdAt;
}
#[\Override]
public function getUpdatedAt(): CarbonImmutable
{
return $this->updatedAt;
}
#[\Override]
public function getBitrix24AccountId(): Uuid
{
return $this->bitrix24AccountId;
}
#[\Override]
public function getContactPersonId(): ?Uuid
{
return $this->contactPersonId;
}
#[\Override]
public function changeContactPerson(?Uuid $uuid): void
{
$this->updatedAt = new CarbonImmutable();
$this->contactPersonId = $uuid;
$this->events[] = new Events\ApplicationInstallationContactPersonChangedEvent(
$this->id,
$this->updatedAt,
$this->contactPersonId,
$uuid
);
}
#[\Override]
public function getBitrix24PartnerContactPersonId(): ?Uuid
{
return $this->bitrix24PartnerContactPersonId;
}
#[\Override]
public function changeBitrix24PartnerContactPerson(?Uuid $uuid): void
{
$this->updatedAt = new CarbonImmutable();
$this->bitrix24PartnerContactPersonId = $uuid;
$this->events[] = new Events\ApplicationInstallationBitrix24PartnerContactPersonChangedEvent(
$this->id,
$this->updatedAt,
$this->bitrix24PartnerContactPersonId,
$uuid
);
}
#[\Override]
public function getBitrix24PartnerId(): ?Uuid
{
return $this->bitrix24PartnerId;
}
#[\Override]
public function changeBitrix24Partner(?Uuid $uuid): void
{
$this->updatedAt = new CarbonImmutable();
$this->bitrix24PartnerId = $uuid;
$this->events[] = new Events\ApplicationInstallationBitrix24PartnerChangedEvent(
$this->id,
$this->updatedAt,
$this->bitrix24PartnerId,
$uuid
);
}
#[\Override]
public function getExternalId(): ?string
{
return $this->externalId;
}
#[\Override]
public function setExternalId(?string $externalId): void
{
if ('' === $externalId) {
throw new InvalidArgumentException('ExternalId cannot be empty string');
}
$this->externalId = $externalId;
}
#[\Override]
public function getStatus(): ApplicationInstallationStatus
{
return $this->status;
}
#[\Override]
public function applicationInstalled(): void
{
if (
ApplicationInstallationStatus::new !== $this->status
&& ApplicationInstallationStatus::blocked !== $this->status
) {
throw new \LogicException(
sprintf(
'installation was interrupted because status must be in new or blocked,but your status is %s',
$this->status->value
)
);
}
$this->status = ApplicationInstallationStatus::active;
$this->updatedAt = new CarbonImmutable();
$this->events[] = new ApplicationInstallationFinishedEvent(
$this->id,
$this->updatedAt,
$this->bitrix24AccountId,
$this->portalLicenseFamily,
$this->contactPersonId,
$this->bitrix24PartnerContactPersonId,
$this->bitrix24PartnerId
);
}
#[\Override]
public function applicationUninstalled(): void
{
if (
ApplicationInstallationStatus::active !== $this->status
&& ApplicationInstallationStatus::blocked !== $this->status
) {
throw new \LogicException(
sprintf(
'installation was interrupted because status must be in active or blocked, but your status is %s',
$this->status->value
)
);
}
$this->status = ApplicationInstallationStatus::deleted;
$this->updatedAt = new CarbonImmutable();
$this->events[] = new ApplicationInstallationUninstalledEvent(
$this->id,
$this->updatedAt,
$this->bitrix24AccountId,
$this->contactPersonId,
$this->bitrix24PartnerId,
$this->bitrix24PartnerId,
$this->externalId
);
}
#[\Override]
public function markAsActive(?string $comment): void
{
if (ApplicationInstallationStatus::blocked !== $this->status) {
throw new \LogicException(
sprintf(
'you must be in status blocked to complete the installation, now status is «%s»',
$this->status->value
)
);
}
$this->status = ApplicationInstallationStatus::active;
$this->comment = $comment;
$this->updatedAt = new CarbonImmutable();
$this->events[] = new ApplicationInstallationUnblockedEvent(
$this->id,
$this->updatedAt,
$this->comment
);
}
#[\Override]
public function markAsBlocked(?string $comment): void
{
if (
ApplicationInstallationStatus::new !== $this->status
&& ApplicationInstallationStatus::active !== $this->status
) {
throw new \LogicException(sprintf(
'You can block application installation only in status new or active,but your status is «%s»',
$this->status->value
));
}
$this->status = ApplicationInstallationStatus::blocked;
$this->comment = $comment;
$this->updatedAt = new CarbonImmutable();
$this->events[] = new ApplicationInstallationBlockedEvent(
$this->id,
new CarbonImmutable(),
$this->comment
);
}
#[\Override]
public function getApplicationStatus(): ApplicationStatus
{
return $this->applicationStatus;
}
#[\Override]
public function changeApplicationStatus(ApplicationStatus $applicationStatus): void
{
if ($this->applicationStatus !== $applicationStatus) {
$this->applicationStatus = $applicationStatus;
$this->updatedAt = new CarbonImmutable();
$this->events[] = new Events\ApplicationInstallationApplicationStatusChangedEvent(
$this->id,
$this->updatedAt,
$applicationStatus
);
}
}
#[\Override]
public function getPortalLicenseFamily(): PortalLicenseFamily
{
return $this->portalLicenseFamily;
}
#[\Override]
public function changePortalLicenseFamily(PortalLicenseFamily $portalLicenseFamily): void
{
if ($this->portalLicenseFamily !== $portalLicenseFamily) {
$this->updatedAt = new CarbonImmutable();
$this->portalLicenseFamily = $portalLicenseFamily;
$this->events[] = new Events\ApplicationInstallationPortalLicenseFamilyChangedEvent(
$this->id,
$this->updatedAt,
$this->portalLicenseFamily,
$portalLicenseFamily
);
}
}
#[\Override]
public function getPortalUsersCount(): ?int
{
return $this->portalUsersCount;
}
#[\Override]
public function changePortalUsersCount(int $usersCount): void
{
if ($usersCount < 0) {
throw new \InvalidArgumentException('Users count can not be negative');
}
if ($this->portalUsersCount !== $usersCount) {
$this->portalUsersCount = $usersCount;
$this->updatedAt = new CarbonImmutable();
$this->events[] = new Events\ApplicationInstallationPortalUsersCountChangedEvent(
$this->id,
$this->updatedAt,
$this->portalUsersCount,
$usersCount
);
}
}
#[\Override]
public function getComment(): ?string
{
return $this->comment;
}
private function addApplicationCreatedEventIfNeeded(bool $isEmitCreatedEvent): void
{
if ($isEmitCreatedEvent) {
// Создание события и добавление его в массив событий
$this->events[] = new ApplicationInstallationCreatedEvent(
$this->id,
$this->createdAt,
$this->bitrix24AccountId
);
}
}
}