Passed
Push — master ( 15544c...e2b3c3 )
by
02:53
created

Receiver::captureReceiveMessageText()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the LineMob package.
5
 *
6
 * (c) Ishmael Doss <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LineMob\Core;
13
14
use League\Tactician\CommandBus;
15
use LineMob\Core\Command\FallbackCommand;
16
use LineMob\Core\Exception\DerailException;
17
18
/**
19
 * @author Ishmael Doss <[email protected]>
20
 */
21
class Receiver
22
{
23
    /**
24
     * @var LineBot
25
     */
26
    private $bot;
27
28
    /**
29
     * @var CommandBus
30
     */
31
    private $commandBus;
32
33
    /**
34
     * @var RegistryInterface
35
     */
36
    private $registry;
37
38
    /**
39
     * @var SenderHandlerInterface
40
     */
41
    private $handler;
42
43
    public function __construct(
44
        LineBot $bot,
45
        RegistryInterface $registry,
46
        CommandBus $commandBus,
47
        SenderHandlerInterface $handler
48
    ) {
49
        $this->bot = $bot;
50
        $this->registry = $registry;
51
        $this->commandBus = $commandBus;
52
        $this->handler = $handler;
53
    }
54
55
    /**
56
     * @param $body
57
     * @param $signature
58
     *
59
     * @return bool
60
     */
61
    public function validate($body, $signature)
62
    {
63
        return $this->bot->validateSignature($body, $signature);
64
    }
65
66
    /**
67
     * @param array $eventData
68
     *
69
     * @return string
70
     */
71
    private function captureReceiveMessageText(array $eventData)
72
    {
73
        $type = strtolower(@$eventData['message']['type']);
74
75
        switch ($type) {
76
            case Constants::REVEIVE_TYPE_MESSAGE_TEXT:
77
                return @$eventData['message']['text'];
78
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
79
80
            default:
81
                throw new \RuntimeException("Unknown message type `$type`.");
82
        }
83
    }
84
85
    /**
86
     * @param array $eventData
87
     *
88
     * @return Input
89
     */
90
    private function captureInput(array $eventData)
91
    {
92
        $inputData = [];
93
94
        if (empty($eventData['replyToken'])) {
95
            throw new \InvalidArgumentException("Require `replyToken` to run.");
96
        }
97
98
        if (empty($eventData['source']['userId'])) {
99
            throw new \InvalidArgumentException("Require `userId` to run.");
100
        }
101
102
        $inputData['replyToken'] = $eventData['replyToken'];
103
        $inputData['userId'] = $eventData['source']['userId'];
104
        $type = strtolower(@$eventData['type']);
105
106
        switch ($type) {
107
            case Constants::REVEIVE_TYPE_FOLLOW:
108
                $inputData['text'] = ':follow';
109
                break;
110
111
            case Constants::REVEIVE_TYPE_MESSAGE:
112
                $inputData['text'] = $this->captureReceiveMessageText($eventData);
113
                break;
114
115
            default:
116
                throw new \RuntimeException("Unsupported type `$type`.");
117
        }
118
119
        if (!$inputData['text']) {
120
            $inputData['text'] = FallbackCommand::CMD;
121
        }
122
123
        return new Input($inputData);
124
    }
125
126
    /**
127
     * @param $payload
128
     *
129
     * @return array
130
     */
131
    public function handle($payload)
132
    {
133
        $data = json_decode($payload, true) ?: [];
134
        $events = (array)@$data['events'];
135
        $results = [];
136
137
        foreach ($events as $event) {
138
            try {
139
                $input = $this->captureInput($event);
140
                $command = $this->registry->findCommand($input);
141
                $command->input = $input;
142
143
                $results[] = $this->commandBus->handle($command);
144
            } catch (DerailException $e) {
145
                $results[] = $this->handler->handle($e->command);
146
            } catch (\Exception $e) {
147
                $results[] = sprintf('ERROR: %s', $e->getMessage());
148
            }
149
        }
150
dump($results);
151
        return $results;
152
    }
153
}
154