Completed
Pull Request — master (#19)
by Romain
02:15
created

EventFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 34
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 15 4
1
<?php
2
namespace Kerox\Messenger\Event;
3
4
class EventFactory
5
{
6
7
    const EVENTS = [
8
        'message' => MessageEvent::class,
9
        'postback' => PostbackEvent::class,
10
        'optin' => OptinEvent::class,
11
        'account_linking' => AccountLinkingEvent::class,
12
        'delivery' => DeliveryEvent::class,
13
        'read' => ReadEvent::class,
14
        'payment' => PaymentEvent::class,
15
        'checkout_update' => CheckoutUpdateEvent::class,
16
    ];
17
18
    /**
19
     * @param array $payload
20
     * @return \Kerox\Messenger\Event\AbstractEvent
21
     */
22 13
    public static function create(array $payload): AbstractEvent
23
    {
24 13
        foreach (array_keys($payload) as $key) {
25 13
            if (array_key_exists($key, self::EVENTS)) {
26 12
                $className = self::EVENTS[$key];
27 12
                if (isset($payload['message']['is_echo'])) {
28 1
                    $className = MessageEchoEvent::class;
29
                }
30
31 13
                return $className::create($payload);
32
            }
33
        }
34
35 1
        return RawEvent::create($payload);
36
    }
37
}
38