Passed
Push — master ( af6300...030a8a )
by Johnny
02:39
created

ResponseQueueItem::getOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of Rivescript-php
4
 *
5
 * (c) Johnny Mast <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Axiom\Rivescript\Cortex\ResponseQueue;
12
13
/**
14
 * ResponseQueueItem class
15
 *
16
 * The ResponseQueueItem represents one response in the ResponseQueue.
17
 *
18
 * PHP version 7.4 and higher.
19
 *
20
 * @category Core
21
 * @package  Cortext\ResponseQueue
22
 * @author   Johnny Mast <[email protected]>
23
 * @license  https://opensource.org/licenses/MIT MIT
24
 * @link     https://github.com/axiom-labs/rivescript-php
25
 * @since    0.4.0
26
 */
27
class ResponseQueueItem
28
{
29
30
    /**
31
     * The command prefix.
32
     *
33
     * @var string
34
     */
35
    public string $command = "";
36
37
    /**
38
     * The response string
39
     *
40
     * @var string
41
     */
42
    public string $value = "";
43
44
    /**
45
     * The original response string
46
     *
47
     * @var string
48
     */
49
    public string $original = "";
50
51
    /**
52
     * The response type.
53
     *
54
     * @var string
55
     */
56
    public string $type = 'atomic';
57
58
    /**
59
     * The sort order of the response.
60
     *
61
     * @var int
62
     */
63
    public int $order = 0;
64
65
    /**
66
     * Local parser options at this item.
67
     *
68
     * @var array<string, string>
69
     */
70
    public array $options = [];
71
72
    /**
73
     * Indicate the response changes topic.
74
     *
75
     * @var bool
76
     */
77
    private bool $topicIsChanged = false;
78
79
    /**
80
     * This is the trigger for this response.
81
     *
82
     * @var string
83
     */
84
    public string $triggerString = '';
85
86
    /**
87
     * This is the topic for this response.
88
     *
89
     * @var string
90
     */
91
    public string $triggerTopic = '';
92
93
    /**
94
     * Set a redirect target.
95
     *
96
     * @var string
97
     */
98
    protected string $redirect = '';
99
100
    /**
101
     * ResponseQueueItem constructor.
102
     *
103
     * @param string               $command The command prefix.
104
     * @param string               $value   The command value.
105
     * @param string               $type    The type of response.
106
     * @param array<string,string> $options The local interpreter options.
107
     */
108
    public function __construct(string $command, string $value, string $type, array $trigger = [], array $options = [])
109
    {
110
        $this->command = $command;
111
        $this->value = $this->original = $value;
112
        $this->type = $type;
113
        $this->options = $options;
114
        $this->triggerString = $trigger['value'];
115
        $this->triggerTopic = $trigger['topic'];
116
    }
117
118
    /**
119
     * Return the command string.
120
     *
121
     * @return string
122
     */
123
    public function getValue(): string
124
    {
125
        return $this->value;
126
    }
127
128
    public function setValue(string $value)
129
    {
130
        $this->value = $value;
131
    }
132
133
    /**
134
     * Return the command string.
135
     *
136
     * @return string
137
     */
138
    public function getCommand(): string
139
    {
140
        return $this->command;
141
    }
142
143
    /**
144
     * Return the trigger.
145
     *
146
     * @return array
147
     */
148
    public function getTrigger(): ?array
149
    {
150
        return synapse()->brain->topic($this->triggerTopic)->triggers()->get($this->triggerString);
151
    }
152
153
    /**
154
     * Return the order of this queue item.
155
     *
156
     * @return int
157
     */
158
    public function getOrder(): int
159
    {
160
        return $this->order;
161
    }
162
163
    public function isRedirect(): bool
164
    {
165
        return ($this->getRedirect() !== '');
166
    }
167
168
    public function isTopicChanged(): bool
169
    {
170
        return $this->topicIsChanged;
171
    }
172
173
    private function reset(): void
0 ignored issues
show
Unused Code introduced by
The method reset() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
174
    {
175
        $this->source = $this->original;
0 ignored issues
show
Bug Best Practice introduced by
The property source does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
176
        $this->topicIsChanged = false;
177
    }
178
179
180
    /**
181
     * Parse the response through the available Tags.
182
     *
183
     * @return string
184
     */
185
    public function parse(): string
186
    {
187
        // $this->reset();
188
189
        $this->topicIsChanged = false;
190
191
        foreach (synapse()->tags as $tag) {
0 ignored issues
show
Bug Best Practice introduced by
The property tags does not exist on Axiom\Rivescript\Cortex\Synapse. Since you implemented __get, consider adding a @property annotation.
Loading history...
192
            $class = "\\Axiom\\Rivescript\\Cortex\\Tags\\{$tag}";
193
            $instance = new $class();
194
195
            $this->value = $instance->parse($this->value, synapse()->input);
196
        }
197
198
        if ($this->triggerTopic !== $this->getTopic()) {
199
            $this->topicIsChanged = true;
200
        }
201
202
        if ($this->command === '@') {
203
            $this->setRedirect($this->value);
204
        }
205
206
        return $this->value;
207
    }
208
209
    public function setRedirect(string $redirect): void
210
    {
211
        $this->redirect = $redirect;
212
    }
213
214
    public function getRedirect(): string
215
    {
216
        return $this->redirect;
217
    }
218
219
    public function getTopic(): string
220
    {
221
        return synapse()->memory->shortTerm()->get('topic') ?? "random";
222
    }
223
}
224