Completed
Push — master ( f3e44e...1f022d )
by Vladimir
02:48
created

Interaction::jump()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation;
6
7
use FondBot\Events\MessageReceived;
8
use FondBot\Contracts\Conversation\Manager;
9
use FondBot\Contracts\Conversation\Conversable;
10
use FondBot\Conversation\Concerns\SendsMessages;
11
use FondBot\Conversation\Concerns\InteractsWithContext;
12
13
abstract class Interaction implements Conversable
14
{
15
    use InteractsWithContext;
16
    use SendsMessages;
17
18
    /**
19
     * Run interaction.
20
     *
21
     * @param MessageReceived $message
22
     */
23
    abstract public function run(MessageReceived $message): void;
24
25
    /**
26
     * Process received message.
27
     *
28
     * @param MessageReceived $reply
29
     */
30
    abstract public function process(MessageReceived $reply): void;
31
32
    /**
33
     * Jump to interaction.
34
     *
35
     * @throws \InvalidArgumentException
36
     */
37
    public static function jump(): void
38
    {
39
        /** @var Manager $conversation */
40
        $conversation = resolve(Manager::class);
41
        $conversation->converse(resolve(static::class));
42
        $conversation->markAsTransitioned();
43
    }
44
45
    /**
46
     * Restart current interaction.
47
     */
48
    protected function restart(): void
49
    {
50
        /** @var Manager $conversation */
51
        $conversation = resolve(Manager::class);
52
        $conversation->converse($this);
53
        $conversation->markAsTransitioned();
54
    }
55
56
    /**
57
     * Handle interaction.
58
     *
59
     * @param MessageReceived $message
60
     */
61 2
    public function handle(MessageReceived $message): void
62
    {
63 2
        $context = context();
64
65 2
        if ($context->getInteraction() instanceof $this) {
66 1
            $this->process($message);
67
        } else {
68 1
            $context->setInteraction($this);
69 1
            $this->run($message);
70
        }
71 2
    }
72
}
73