QuickStart::addCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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