Completed
Pull Request — master (#17)
by Romain
03:33
created

EventFactory   C

Complexity

Total Complexity 13

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 20

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 20
dl 0
loc 160
ccs 62
cts 62
cp 1
rs 6.4705
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 13 3
A createRawEvent() 0 8 1
A createMessageEvent() 0 15 2
A createPostbackEvent() 0 9 1
A createOptinEvent() 0 9 1
A createAccountLinkingEvent() 0 9 1
A createDeliveryEvent() 0 8 1
A createReadEvent() 0 9 1
A createPaymentEvent() 0 9 1
A createCheckoutUpdateEvent() 0 9 1
1
<?php
2
namespace Kerox\Messenger\Event;
3
4
use Kerox\Messenger\Helper\UtilityTrait;
5
use Kerox\Messenger\Model\Callback\AccountLinking;
6
use Kerox\Messenger\Model\Callback\CheckoutUpdate;
7
use Kerox\Messenger\Model\Callback\Delivery;
8
use Kerox\Messenger\Model\Callback\Message;
9
use Kerox\Messenger\Model\Callback\MessageEcho;
10
use Kerox\Messenger\Model\Callback\Optin;
11
use Kerox\Messenger\Model\Callback\Payment;
12
use Kerox\Messenger\Model\Callback\Postback;
13
use Kerox\Messenger\Model\Callback\Read;
14
15
class EventFactory
16
{
17
18
    use UtilityTrait;
19
20
    const EVENTS = [
21
        'message',
22
        'postback',
23
        'optin',
24
        'account_linking',
25
        'delivery',
26
        'read',
27
        'payment',
28
        'checkout_update',
29
    ];
30
31
    /**
32
     * @param array $payload
33
     * @return \Kerox\Messenger\Event\AbstractEvent
34
     */
35 13
    public static function create(array $payload): AbstractEvent
36
    {
37 13
        foreach (array_keys($payload) as $key) {
38 13
            if (in_array($key, self::EVENTS)) {
39 12
                $eventName = UtilityTrait::camelize($key);
40 12
                $functionName = 'create' . $eventName . 'Event';
41
42 13
                return self::$functionName($payload);
43
            }
44
        }
45
46 1
        return self::createRawEvent($payload);
47
    }
48
49
    /**
50
     * @param array $payload
51
     * @return \Kerox\Messenger\Event\RawEvent
52
     */
53 1
    public static function createRawEvent(array $payload): RawEvent
54
    {
55 1
        $senderId = $payload['sender']['id'];
56 1
        $recipientId = $payload['recipient']['id'];
57 1
        unset($payload['sender'], $payload['recipient']);
58
59 1
        return new RawEvent($senderId, $recipientId, $payload);
60
    }
61
62
    /**
63
     * @param array $payload
64
     * @return \Kerox\Messenger\Event\MessageEvent|\Kerox\Messenger\Event\MessageEchoEvent
65
     */
66 5
    public static function createMessageEvent(array $payload)
67
    {
68 5
        $senderId = $payload['sender']['id'];
69 5
        $recipientId = $payload['recipient']['id'];
70 5
        $timestamp = $payload['timestamp'];
71
72 5
        $message = Message::create($payload['message']);
73 5
        if (isset($payload['message']['is_echo'])) {
74 1
            $message = MessageEcho::create($payload['message']);
75
76 1
            return new MessageEchoEvent($senderId, $recipientId, $timestamp, $message);
77
        }
78
79 4
        return new MessageEvent($senderId, $recipientId, $timestamp, $message);
80
    }
81
82
    /**
83
     * @param array $payload
84
     * @return \Kerox\Messenger\Event\PostbackEvent
85
     */
86 1
    public static function createPostbackEvent(array $payload): PostbackEvent
87
    {
88 1
        $senderId = $payload['sender']['id'];
89 1
        $recipientId = $payload['recipient']['id'];
90 1
        $timestamp = $payload['timestamp'];
91 1
        $postback = Postback::create($payload['postback']);
92
93 1
        return new PostbackEvent($senderId, $recipientId, $timestamp, $postback);
94
    }
95
96
    /**
97
     * @param array $payload
98
     * @return \Kerox\Messenger\Event\OptinEvent
99
     */
100 1
    public static function createOptinEvent(array $payload): OptinEvent
101
    {
102 1
        $senderId = $payload['sender']['id'];
103 1
        $recipientId = $payload['recipient']['id'];
104 1
        $timestamp = $payload['timestamp'];
105 1
        $optin = Optin::create($payload['optin']);
106
107 1
        return new OptinEvent($senderId, $recipientId, $timestamp, $optin);
108
    }
109
110
    /**
111
     * @param $payload
112
     * @return \Kerox\Messenger\Event\AccountLinkingEvent
113
     */
114 1
    public static function createAccountLinkingEvent(array $payload): AccountLinkingEvent
115
    {
116 1
        $senderId = $payload['sender']['id'];
117 1
        $recipientId = $payload['recipient']['id'];
118 1
        $timestamp = $payload['timestamp'];
119 1
        $accountLinking = AccountLinking::create($payload['account_linking']);
120
121 1
        return new AccountLinkingEvent($senderId, $recipientId, $timestamp, $accountLinking);
122
    }
123
124
    /**
125
     * @param $payload
126
     * @return \Kerox\Messenger\Event\DeliveryEvent
127
     */
128 1
    public static function createDeliveryEvent(array $payload): DeliveryEvent
129
    {
130 1
        $senderId = $payload['sender']['id'];
131 1
        $recipientId = $payload['recipient']['id'];
132 1
        $delivery = Delivery::create($payload['delivery']);
133
134 1
        return new DeliveryEvent($senderId, $recipientId, $delivery);
135
    }
136
137
    /**
138
     * @param $payload
139
     * @return \Kerox\Messenger\Event\ReadEvent
140
     */
141 1
    public static function createReadEvent($payload): ReadEvent
142
    {
143 1
        $senderId = $payload['sender']['id'];
144 1
        $recipientId = $payload['recipient']['id'];
145 1
        $timestamp = $payload['timestamp'];
146 1
        $read = Read::create($payload['read']);
147
148 1
        return new ReadEvent($senderId, $recipientId, $timestamp, $read);
149
    }
150
151
    /**
152
     * @param array $payload
153
     * @return \Kerox\Messenger\Event\PaymentEvent
154
     */
155 1
    public static function createPaymentEvent(array $payload): PaymentEvent
156
    {
157 1
        $senderId = $payload['sender']['id'];
158 1
        $recipientId = $payload['recipient']['id'];
159 1
        $timestamp = $payload['timestamp'];
160 1
        $payment = Payment::create($payload['payment']);
161
162 1
        return new PaymentEvent($senderId, $recipientId, $timestamp, $payment);
163
    }
164
165 1
    public static function createCheckoutUpdateEvent(array $payload)
166
    {
167 1
        $senderId = $payload['sender']['id'];
168 1
        $recipientId = $payload['recipient']['id'];
169 1
        $timestamp = $payload['timestamp'];
170 1
        $checkoutUpdate = CheckoutUpdate::create($payload['checkout_update']);
171
172 1
        return new CheckoutUpdateEvent($senderId, $recipientId, $timestamp, $checkoutUpdate);
173
    }
174
}
175