Passed
Push — master ( 970c61...032e68 )
by Johnny
04:05 queued 38s
created

Rivescript::stream()   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 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Bootstrap the Rivescript client.
5
 *
6
 * @package      Rivescript-php
7
 * @subpackage   Core
8
 * @category     Client
9
 * @author       Shea Lewis <[email protected]>
10
 */
11
12
namespace Axiom\Rivescript;
13
14
use Axiom\Rivescript\Cortex\ContentLoader\ContentLoader;
15
use Axiom\Rivescript\Cortex\Input;
16
use Axiom\Rivescript\Cortex\Output;
17
18
/**
19
 * The main Rivescript client.
20
 */
21
class Rivescript extends ContentLoader
22
{
23
    /**
24
     * Create a new Rivescript instance.
25
     *
26
     * @throws \Axiom\Rivescript\Exceptions\ContentLoadingException
27
     */
28
    public function __construct()
29
    {
30
        parent::__construct();
31
32
        include __DIR__ . '/bootstrap.php';
33
    }
34
35
    /**
36
     * Load Rivescript interpretable content.
37
     * Into the Interpreter.
38
     *
39
     * Please note: This supports
40
     *
41
     * - Directory path to Rivescript interpretable files.
42
     * - Array of absolute paths to Rivescript interpretable files
43
     * - Absolute string containing path to Rivescript interpretable file.
44
     * - A stream of text with Rivescript interpretable script.
45
     *
46
     * Please note 2:
47
     *
48
     * If you profile a directory with rivescript documents make sure they are
49
     * all interpretable rivescript will throw syntax errors while trying to
50
     * parse those files.
51
     *
52
     * @param array<string>|string $info The files to read
53
     *
54
     * @return void
55
     */
56
    public function load($info)
57
    {
58
        parent::load($info);
59
        $this->processInformation();
60
    }
61
62
    /**
63
     * Stream new information into the brain.
64
     *
65
     * @param string $string The string of information to feed the brain.
66
     *
67
     * @return void
68
     */
69
    public function stream(string $string)
70
    {
71
        $this->writeToMemory($string);
72
        $this->processInformation();
73
    }
74
75
    /**
76
     * Process new information in the
77
     * stream.
78
     *
79
     * @return void
80
     */
81
    private function processInformation()
82
    {
83
        synapse()->brain->teach($this->getStream());
84
    }
85
86
    /**
87
     * Make the client respond to a message.
88
     *
89
     * @param string $message The message the client has to process and respond to.
90
     * @param string $user    The user id.
91
     *
92
     * @return string
93
     */
94
    public function reply(string $message, string $user = 'local-user'): string
95
    {
96
        $input = new Input($message, $user);
97
        $output = new Output($input);
98
99
        synapse()->input = $input;
100
101
        $output = $output->process();
102
103
        synapse()->memory->inputs()->push($message);
104
        synapse()->memory->replies()->push($output);
105
106
        return $output;
107
    }
108
}
109