Completed
Push — master ( 87aff1...17cc82 )
by Vladimir
03:09
created

Bot::sendMessage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 12
nc 2
nop 4
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot;
6
7
use FondBot\Drivers\User;
8
use FondBot\Drivers\Driver;
9
use FondBot\Channels\Channel;
10
use FondBot\Contracts\Container;
11
use FondBot\Conversation\Intent;
12
use FondBot\Conversation\Context;
13
use FondBot\Conversation\Keyboard;
14
use FondBot\Drivers\OutgoingMessage;
15
use FondBot\Conversation\Conversable;
16
use FondBot\Conversation\Interaction;
17
use FondBot\Conversation\IntentManager;
18
use FondBot\Conversation\ContextManager;
19
use FondBot\Drivers\Exceptions\InvalidRequest;
20
use FondBot\Drivers\Extensions\WebhookVerification;
21
22
class Bot
23
{
24
    /** @var Bot */
25
    protected static $instance;
26
27
    private $container;
28
    private $channel;
29
    private $driver;
30
31
    /** @var Context|null */
32
    private $context;
33
34 9
    protected function __construct(
35
        Container $container,
36
        Channel $channel,
37
        Driver $driver
38
    ) {
39 9
        $this->container = $container;
40 9
        $this->channel = $channel;
41 9
        $this->driver = $driver;
42 9
    }
43
44
    /**
45
     * Create new bot instance.
46
     *
47
     * @param Container $container
48
     * @param Channel   $channel
49
     * @param Driver    $driver
50
     * @param array     $request
51
     * @param array     $headers
52
     */
53 9
    public static function createInstance(
54
        Container $container,
55
        Channel $channel,
56
        Driver $driver,
57
        array $request,
58
        array $headers
59
    ): void {
60 9
        static::setInstance(
61 9
            new static($container, $channel, $driver, $request, $headers)
0 ignored issues
show
Unused Code introduced by
The call to Bot::__construct() has too many arguments starting with $request.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
62
        );
63 9
    }
64
65
    /**
66
     * Get current instance.
67
     *
68
     * @return Bot
69
     */
70 9
    public static function getInstance(): Bot
71
    {
72 9
        return static::$instance;
73
    }
74
75
    /**
76
     * Set instance of the bot.
77
     *
78
     * @param Bot $instance
79
     */
80 57
    public static function setInstance(Bot $instance): void
81
    {
82 57
        static::$instance = $instance;
83 57
    }
84
85
    /**
86
     * Get context instance.
87
     *
88
     * @return Context|null
89
     */
90 2
    public function getContext(): ?Context
91
    {
92 2
        return $this->context;
93
    }
94
95
    /**
96
     * Set context instance.
97
     *
98
     * @param Context $context
99
     */
100 2
    public function setContext(Context $context): void
101
    {
102 2
        $this->context = $context;
103 2
    }
104
105
    /**
106
     * Clear context.
107
     */
108 1
    public function clearContext(): void
109
    {
110 1
        if ($this->context !== null) {
111 1
            $this->contextManager()->clear($this->context);
112 1
            $this->context = null;
113
        }
114 1
    }
115
116
    /**
117
     * Resolve from container.
118
     *
119
     * @param string $class
120
     *
121
     * @return mixed
122
     */
123 3
    public function get(string $class)
124
    {
125 3
        return $this->container->make($class);
126
    }
127
128
    /**
129
     * Process webhook request.
130
     *
131
     * @return mixed
132
     */
133 4
    public function process()
134
    {
135
        try {
136
            // Driver has webhook verification
137 4
            if ($this->driver instanceof WebhookVerification && $this->driver->isVerificationRequest()) {
138 1
                return $this->driver->verifyWebhook();
139
            }
140
141
            // Verify request
142 3
            $this->driver->verifyRequest();
143
144
            // Resolve context
145 2
            $this->context = $this->contextManager()->resolve($this->channel->getName(), $this->driver);
146
147 2
            if ($this->context->getInteraction() !== null) {
148 1
                $this->converse($this->context->getInteraction());
149
            } else {
150 1
                $intent = $this->intentManager()->find($this->driver->getMessage());
151
152 1
                if ($intent !== null) {
153 1
                    $this->converse($intent);
154
                }
155
            }
156
157 2
            if ($this->context !== null) {
158 2
                $this->contextManager()->save($this->context);
159
            }
160
161 2
            return 'OK';
162 1
        } catch (InvalidRequest $exception) {
163 1
            return $exception->getMessage();
164
        }
165
    }
166
167
    /**
168
     * Start conversation.
169
     *
170
     * @param Conversable $conversable
171
     */
172 2
    public function converse(Conversable $conversable): void
173
    {
174 2
        if ($conversable instanceof Intent) {
175 1
            $this->context->setIntent($conversable);
176 1
            $this->context->setInteraction(null);
177 1
            $this->context->setValues([]);
178
179 1
            $conversable->handle($this);
180
        } elseif ($conversable instanceof Interaction) {
181 1
            $conversable->handle($this);
182
        }
183 2
    }
184
185
    /**
186
     * Send message.
187
     *
188
     * @param User          $recipient
189
     * @param string        $text
190
     * @param Keyboard|null $keyboard
191
     * @param string|null   $driver
192
     *
193
     * @return OutgoingMessage|null
194
     */
195 2
    public function sendMessage(
196
        User $recipient,
197
        string $text,
198
        Keyboard $keyboard = null,
199
        string $driver = null
200
    ): ?OutgoingMessage {
201 2
        if ($driver !== null && !$this->driver instanceof $driver) {
202 1
            return null;
203
        }
204
205 1
        return $this->driver->sendMessage(
206
            $recipient,
207
            $text,
208
            $keyboard
209
        );
210
    }
211
212
    /**
213
     * Get context manager.
214
     *
215
     * @return ContextManager
216
     */
217 3
    private function contextManager(): ContextManager
218
    {
219 3
        return $this->get(ContextManager::class);
220
    }
221
222
    /**
223
     * Get intent manager.
224
     *
225
     * @return IntentManager
226
     */
227 1
    private function intentManager(): IntentManager
228
    {
229 1
        return $this->get(IntentManager::class);
230
    }
231
}
232