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

WebhookEvent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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

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
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