Passed
Push — master ( c52187...71f110 )
by Sébastien
02:19
created

WebhookEvent   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 37
ccs 8
cts 8
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 17 1
A __construct() 0 12 1
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