|
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 League\Tactician\Handler\CommandHandlerMiddleware; |
|
16
|
|
|
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor; |
|
17
|
|
|
use League\Tactician\Handler\Locator\InMemoryLocator; |
|
18
|
|
|
use League\Tactician\Handler\MethodNameInflector\HandleInflector; |
|
19
|
|
|
use League\Tactician\Plugins\LockingMiddleware; |
|
20
|
|
|
use LineMob\Core\Factory\MessageFactory; |
|
21
|
|
|
use LineMob\Core\HttpClient\GuzzleHttpClient; |
|
22
|
|
|
use LineMob\Core\Message\AudioMessage; |
|
23
|
|
|
use LineMob\Core\Message\ButtonsMessage; |
|
24
|
|
|
use LineMob\Core\Message\CarouselMessage; |
|
25
|
|
|
use LineMob\Core\Message\ConfirmMessage; |
|
26
|
|
|
use LineMob\Core\Message\ImageMapMessage; |
|
27
|
|
|
use LineMob\Core\Message\ImageMessage; |
|
28
|
|
|
use LineMob\Core\Message\LocationMessage; |
|
29
|
|
|
use LineMob\Core\Message\StickerMessage; |
|
30
|
|
|
use LineMob\Core\Message\TextMessage; |
|
31
|
|
|
use LineMob\Core\Message\VideoMessage; |
|
32
|
|
|
use LineMob\Core\Sender\LineSender; |
|
33
|
|
|
use LineMob\Core\Sender\SenderInterface; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @author Ishmael Doss <[email protected]> |
|
37
|
|
|
*/ |
|
38
|
|
|
class QuickStart |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
private $commands = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
private $middlewares = []; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* QuickStart constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $middlewares |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(array $middlewares = []) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->middlewares = $middlewares; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $command FQCN |
|
62
|
|
|
* @param bool $default |
|
63
|
|
|
* |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
|
|
public function addCommand($command, $default = false) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->commands[$command] = $default; |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return MessageFactory |
|
75
|
|
|
*/ |
|
76
|
|
|
private function createMessageFactory() |
|
77
|
|
|
{ |
|
78
|
|
|
$factory = new MessageFactory(); |
|
79
|
|
|
$factory->add(new CarouselMessage()); |
|
80
|
|
|
$factory->add(new ImageMessage()); |
|
81
|
|
|
$factory->add(new ImageMapMessage()); |
|
82
|
|
|
$factory->add(new TextMessage()); |
|
83
|
|
|
$factory->add(new LocationMessage()); |
|
84
|
|
|
$factory->add(new StickerMessage()); |
|
85
|
|
|
$factory->add(new AudioMessage()); |
|
86
|
|
|
$factory->add(new VideoMessage()); |
|
87
|
|
|
$factory->add(new ConfirmMessage()); |
|
88
|
|
|
$factory->add(new ButtonsMessage()); |
|
89
|
|
|
|
|
90
|
|
|
return $factory; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $lineChannelToken |
|
95
|
|
|
* @param string $lineChannelSecret |
|
96
|
|
|
* @param array $httpConfig |
|
97
|
|
|
* @param SenderInterface|null $sender |
|
98
|
|
|
* |
|
99
|
|
|
* @return Receiver |
|
100
|
|
|
*/ |
|
101
|
|
|
public function setup($lineChannelToken, $lineChannelSecret, array $httpConfig = [], SenderInterface $sender = null) |
|
102
|
|
|
{ |
|
103
|
|
|
$sender = $sender ?: new LineSender( |
|
104
|
|
|
new GuzzleHttpClient($lineChannelToken, $httpConfig), |
|
105
|
|
|
['channelSecret' => $lineChannelSecret] |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
$registry = new Registry(); |
|
109
|
|
|
$handler = new CommandHandler($sender, $this->createMessageFactory()); |
|
110
|
|
|
|
|
111
|
|
|
foreach ($this->commands as $command => $default) { |
|
112
|
|
|
$registry->add($command, $handler, $default); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// should be first of all middlewares |
|
116
|
|
|
array_unshift($this->middlewares, new LockingMiddleware()); |
|
117
|
|
|
|
|
118
|
|
|
// must be end of all middlewares |
|
119
|
|
|
array_push( |
|
120
|
|
|
$this->middlewares, |
|
121
|
|
|
new CommandHandlerMiddleware( |
|
122
|
|
|
new ClassNameExtractor(), |
|
123
|
|
|
new InMemoryLocator($registry->getCommandList()), |
|
124
|
|
|
new HandleInflector() |
|
125
|
|
|
) |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
return new Receiver($sender, $registry, new CommandBus($this->middlewares), $handler); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|