Passed
Push — master ( a3f2ba...d44e36 )
by Johnny
08:16
created

ResponseQueueItem::getTopic()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 $changesTopic = false;
78
79
80
    /**
81
     * ResponseQueueItem constructor.
82
     *
83
     * @param string               $command The command prefix.
84
     * @param string               $value   The command value.
85
     * @param string               $type    The type of response.
86
     * @param int                  $order   The order of the response.
87
     * @param array<string,string> $options The local interpreter options.
88
     */
89
    public function __construct(string $command, string $value, string $type, int $order = 0, array $options = [])
90
    {
91
        $this->command = $command;
92
        $this->value = $this->original = $value;
93
        $this->type = $type;
94
        $this->order = $order;
95
        $this->options = $options;
96
    }
97
98
    /**
99
     * Return the command string.
100
     *
101
     * @return string
102
     */
103
    public function getValue(): string
104
    {
105
        return $this->value;
106
    }
107
108
    public function setValue(string $value)
109
    {
110
        $this->value = $value;
111
    }
112
113
    /**
114
     * Return the command string.
115
     *
116
     * @return string
117
     */
118
    public function getCommand(): string
119
    {
120
        return $this->command;
121
    }
122
123
124
    public function isChangingTopic(): bool
125
    {
126
        return $this->changesTopic;
127
    }
128
129
    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...
130
    {
131
        $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...
132
        $this->changesTopic = false;
133
    }
134
135
136
    /**
137
     * Parse the response through the available tags.
138
     *
139
     * @param ResponseQueueItem $response
140
     *
141
     * @return string
142
     */
143
    public function parse(): string
144
    {
145
       // $this->reset();
146
147
        $this->changesTopic = false;
148
149
        $topic = $this->getTopic();
150
151
        foreach (synapse()->tags as $tag) {
152
            $class = "\\Axiom\\Rivescript\\Cortex\\Tags\\{$tag}";
153
            $instance = new $class();
154
155
            $this->value = $instance->parse($this->value, synapse()->input);
156
        }
157
158
        $topicAfter = $this->getTopic();
159
160
        if ($topicAfter !== $topic) {
161
            $this->changesTopic = true;
162
        }
163
164
165
        return $this->value;
166
    }
167
168
    private function getTopic(): string
169
    {
170
        return synapse()->memory->shortTerm()->get('topic') ?? "random";
171
    }
172
}
173