Passed
Pull Request — master (#27)
by Sébastien
02:36
created

WebhookEvent::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 17
ccs 7
cts 7
cp 1
crap 1
rs 9.9666
1
<?php
2
3
namespace Sebdesign\VivaPayments\Events;
4
5
use Sebdesign\VivaPayments\Enums\WebhookEventType;
6
7
/**
8
 * @template TEventData of object
9
 */
10
class WebhookEvent
11
{
12 1
    public function __construct(
13
        public readonly string $Url,
14
        /** @var TEventData */
15
        public readonly object $EventData,
16
        public readonly string $Created,
17
        public readonly string $CorrelationId,
18
        public readonly WebhookEventType $EventTypeId,
19
        public readonly ?string $Delay,
20
        public readonly string $MessageId,
21
        public readonly string $RecipientId,
22
        public readonly int $MessageTypeId,
23
    ) {
24
    }
25
26
    /**
27
     * @phpstan-param  WebhookEventArray  $attributes
28
     * @phpstan-return self<TEventData>
29
     */
30 3
    public static function create(array $attributes): self
31
    {
32 3
        $eventType = WebhookEventType::from($attributes['EventTypeId']);
33
34 3
        $eventData = match ($eventType) {
35
            /** @phpstan-ignore-next-line */
36 3
            WebhookEventType::TransactionPaymentCreated => TransactionPaymentCreated::create($attributes['EventData']),
37
            /** @phpstan-ignore-next-line */
38 3
            WebhookEventType::TransactionFailed => TransactionFailed::create($attributes['EventData']),
39 3
            default => (object) $attributes['EventData'],
40
        };
41
42
        /** @phpstan-ignore-next-line */
43 3
        return new self(...[
44
            ...$attributes,
45
            'EventTypeId' => $eventType,
46
            'EventData' => $eventData,
47
        ]);
48
    }
49
}
50