Passed
Push — master ( 4812ae...635254 )
by Johnny
06:32
created

Topic   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 0 24 9
1
<?php
2
/*
3
 * This file is part of Rivescript-php
4
 *
5
 * (c) Shea Lewis <[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\Commands;
12
13
use Axiom\Rivescript\Contracts\Command;
14
use Axiom\Rivescript\Cortex\Node;
15
16
/**
17
 * Topic class
18
 *
19
 * This class handles the Topic command (> ...)
20
 * and stores the definition in memory.
21
 *
22
 * PHP version 7.4 and higher.
23
 *
24
 * @category Core
25
 * @package  Cortext\Commands
26
 * @author   Shea Lewis <[email protected]>
27
 * @license  https://opensource.org/licenses/MIT MIT
28
 * @link     https://github.com/axiom-labs/rivescript-php
29
 * @since    0.3.0
30
 */
31
class Topic implements Command
32
{
33
    /**
34
     * Parse the command.
35
     *
36
     * @param Node $node The node is a line from the Rivescript file.
37
     *
38
     * @return void
39
     */
40
    public function parse(Node $node): void
41
    {
42
        if ($node->command() === '>') {
43
            $data = explode(' ', $node->value());
44
45
            if (count($data) === 1) {
46
                $type = "begin";
47
                $topic = "__begin__";
48
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
49
            } else {
50
                [$type, $topic] = explode(' ', $node->value());
51
            }
52
53
            if ($type === 'topic' || $type === "begin") {
54
                if (!synapse()->brain->topic($topic)) {
55
                    synapse()->brain->createTopic($topic);
56
                }
57
58
                synapse()->memory->shortTerm()->put('topic', $topic);
59
            }
60
        }
61
62
        if ($node->command() === '<' && ($node->value() === "topic" || $node->value() === "begin")) {
63
            synapse()->memory->shortTerm()->remove('topic');
64
        }
65
    }
66
}
67