Completed
Push — master ( ebdf7a...2dba39 )
by Vladimir
05:12
created

Conversation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot;
6
7
use FondBot\Conversation\Intent;
8
use FondBot\Conversation\Context;
9
use FondBot\Contracts\Conversable;
10
use FondBot\Events\MessageReceived;
11
use FondBot\Conversation\Interaction;
12
13
class Conversation
14
{
15
    private static $instance;
16
17
    /**
18
     * Message received from user.
19
     *
20
     * @var MessageReceived
21
     */
22
    private $messageReceived;
23
24
    /**
25
     * Conversation context.
26
     *
27
     * @var Context
28
     */
29
    private $context;
30
31
    private $transitioned = false;
32
33
    public function __construct(Channel $channel, MessageReceived $messageReceived)
34
    {
35
        $this->messageReceived = $messageReceived;
36
        $this->context = Context::load($channel, $messageReceived->getChat(), $messageReceived->getFrom());
37
    }
38
39
    public static function create(Channel $channel, MessageReceived $messageReceived)
40
    {
41
        return static::$instance = new static($channel, $messageReceived);
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $instance to at least protected.
Loading history...
42
    }
43
44
    public static function instance(): Conversation
45
    {
46
        return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $instance to at least protected.
Loading history...
47
    }
48
49
    /**
50
     * Get context.
51
     *
52
     * @return Context
53
     */
54
    public function context(): Context
55
    {
56
        return $this->context;
57
    }
58
59
    /**
60
     * Determine if conversation is transitioned.
61
     *
62
     * @return bool
63
     */
64
    public function transitioned(): bool
65
    {
66
        return $this->transitioned;
67
    }
68
69
    /**
70
     * Determine if conversation is still active.
71
     *
72
     * @return bool
73
     */
74
    public function isActive(): bool
75
    {
76
        return $this->context->getInteraction() !== null;
77
    }
78
79
    /**
80
     * Resume active conversation.
81
     */
82
    public function resume(): void
83
    {
84
        $this->converse($this->context->getInteraction());
85
    }
86
87
    /**
88
     * Mark conversation as transitioned.
89
     */
90
    public function markAsTransitioned(): void
91
    {
92
        $this->transitioned = true;
93
    }
94
95
    /**
96
     * Run the conversable.
97
     *
98
     * @param Conversable $conversable
99
     */
100
    public function converse(Conversable $conversable): void
101
    {
102
        $this->context->incrementAttempts();
103
104
        if ($conversable instanceof Intent) {
105
            $this->context->setIntent($conversable)->setInteraction(null);
106
        }
107
108
        $conversable->handle($this->messageReceived);
109
    }
110
111
    /**
112
     * Restart interaction.
113
     *
114
     * @param Interaction $interaction
115
     */
116
    public function restartInteraction(Interaction $interaction): void
117
    {
118
        $this->context->setInteraction(null);
119
120
        $this->converse($interaction);
121
122
        $this->markAsTransitioned();
123
    }
124
}
125