1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Conversation; |
6
|
|
|
|
7
|
|
|
use FondBot\Drivers\ReceivedMessage; |
8
|
|
|
|
9
|
|
|
class ConversationManager |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Determine if conversation transitioned. |
13
|
|
|
* |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
private $transitioned = false; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Handle received message. |
20
|
|
|
* |
21
|
|
|
* @param ReceivedMessage $message |
22
|
|
|
*/ |
23
|
2 |
|
public function handle(ReceivedMessage $message): void |
24
|
|
|
{ |
25
|
2 |
|
if (!$this->isInConversation()) { |
26
|
1 |
|
$this->converse( |
27
|
1 |
|
$this->findIntent($message) |
|
|
|
|
28
|
|
|
); |
29
|
|
|
} else { |
30
|
1 |
|
$this->converse( |
31
|
1 |
|
session()->getInteraction() |
|
|
|
|
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Close session if conversation has not been transitioned |
36
|
|
|
// Otherwise, save session state |
37
|
2 |
|
if (!$this->transitioned) { |
38
|
2 |
|
kernel()->closeSession(); |
39
|
2 |
|
kernel()->clearContext(); |
40
|
|
|
} |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Start conversation. |
45
|
|
|
* |
46
|
|
|
* @param Conversable|mixed $conversable |
47
|
|
|
*/ |
48
|
5 |
|
public function converse(Conversable $conversable): void |
49
|
|
|
{ |
50
|
5 |
|
if ($conversable instanceof Intent) { |
51
|
2 |
|
$session = kernel()->getSession(); |
52
|
2 |
|
$session->setIntent($conversable); |
53
|
2 |
|
$session->setInteraction(null); |
54
|
|
|
|
55
|
2 |
|
kernel()->setSession($session); |
56
|
|
|
|
57
|
2 |
|
$conversable->handle(); |
58
|
3 |
|
} elseif ($conversable instanceof Interaction) { |
59
|
2 |
|
$conversable->handle(); |
60
|
|
|
} else { |
61
|
1 |
|
$conversable->handle(); |
62
|
|
|
} |
63
|
5 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Transition to intent or interaction. |
67
|
|
|
* |
68
|
|
|
* @param Conversable $conversable |
69
|
|
|
*/ |
70
|
1 |
|
public function transition(Conversable $conversable): void |
71
|
|
|
{ |
72
|
1 |
|
$this->converse($conversable); |
73
|
1 |
|
$this->transitioned = true; |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Restart intent or interaction. |
78
|
|
|
* |
79
|
|
|
* @param Conversable|mixed $conversable |
80
|
|
|
*/ |
81
|
2 |
|
public function restart(Conversable $conversable): void |
82
|
|
|
{ |
83
|
|
|
switch (true) { |
84
|
2 |
|
case $conversable instanceof Intent: |
85
|
1 |
|
$this->converse($conversable); |
86
|
|
|
|
87
|
1 |
|
$this->transitioned = true; |
88
|
1 |
|
break; |
89
|
1 |
|
case $conversable instanceof Interaction: |
90
|
1 |
|
$session = kernel()->getSession(); |
91
|
1 |
|
$session->setInteraction(null); |
92
|
|
|
|
93
|
1 |
|
kernel()->setSession($session); |
94
|
|
|
|
95
|
1 |
|
$this->transitioned = true; |
96
|
|
|
|
97
|
1 |
|
$this->converse($conversable); |
98
|
1 |
|
break; |
99
|
|
|
} |
100
|
2 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Find matching intent. |
104
|
|
|
* |
105
|
|
|
* @param ReceivedMessage $message |
106
|
|
|
* |
107
|
|
|
* @return Intent|null |
108
|
|
|
*/ |
109
|
1 |
|
private function findIntent(ReceivedMessage $message): Intent |
110
|
|
|
{ |
111
|
|
|
/** @var IntentManager $intentManager */ |
112
|
1 |
|
$intentManager = kernel()->resolve(IntentManager::class); |
113
|
|
|
|
114
|
1 |
|
return $intentManager->find($message); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Determine if conversation started. |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
2 |
|
private function isInConversation(): bool |
123
|
|
|
{ |
124
|
2 |
|
return kernel()->getSession()->getInteraction() !== null; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: