Passed
Push — develop ( 83b649...63947e )
by Johnny
02:08
created

ContentLoader::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
nc 8
nop 0
dl 0
loc 13
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Axiom\Rivescript\Cortex\ContentLoader;
4
5
use Axiom\Rivescript\Exceptions\ContentLoadingException;
6
use DirectoryIterator;
7
use SplFileObject;
8
9
/**
10
 * The ContentLoader assists in loading information
11
 * into the Rivescript interpreter. The data will be
12
 * store in a stream for the brain to learn
13
 * from.
14
 *
15
 * @package      Rivescript-php
16
 * @subpackage   Core
17
 * @category     ContentLoader
18
 * @author       Johnny Mast <[email protected]>
19
 */
20
21
/**
22
 * ContentLoader class.
23
 */
24
class ContentLoader
25
{
26
    /**
27
     * The stream resource.
28
     *
29
     * @var false|resource
30
     */
31
    private $stream;
32
33
    /**
34
     * The stream wrapper name.
35
     *
36
     * @var string
37
     */
38
    protected $name = 'rivescript';
39
40
    /**
41
     * ContentLoader constructor.
42
     *
43
     * @throws \Axiom\Rivescript\Exceptions\ContentLoadingException
44
     */
45
    public function __construct()
46
    {
47
        $existed = in_array($this->name, stream_get_wrappers());
48
        if ($existed) {
49
            stream_wrapper_unregister($this->name);
50
        }
51
52
        if (stream_wrapper_register($this->name, ContentStream::class)) {
53
            $this->stream = fopen($this->name . "://input", "r+");
54
        }
55
56
        if (is_resource($this->stream) === false) {
57
            throw new ContentLoadingException("Could not instantiate new rivescript content stream.");
58
        }
59
    }
60
61
    /**
62
     *  ContentLoader deconstruct.
63
     */
64
    public function __destruct()
65
    {
66
        fclose($this->getStream());
67
    }
68
69
    /**
70
     * Return the stream.
71
     *
72
     * @return false|resource
73
     */
74
    public function getStream()
75
    {
76
        return $this->stream;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->stream also could return the type boolean which is incompatible with the documented return type false|resource.
Loading history...
77
    }
78
79
    /**
80
     * Load strings / files or whole directories
81
     * into the brain.
82
     *
83
     * @param mixed $info
84
     *
85
     * @return void
86
     */
87
    public function load($info)
88
    {
89
        if (is_string($info) === true && file_exists($info) === false) {
90
            $this->writeToMemory($info);
91
        } elseif (is_string($info) === true && is_dir($info) === true) {
92
            $this->loadDirectory($info);
93
        } elseif (is_string($info) === true && file_exists($info) === true) {
94
            $this->loadFile($info);
95
        } elseif (is_array($info) === true and count($info) > 0) {
96
            foreach ($info as $file) {
97
                if (file_exists($file)) {
98
                    $this->loadFile($file);
99
                }
100
            }
101
        }
102
    }
103
104
    /**
105
     * Load rivescript files from a given path.
106
     *
107
     * @param string $path Path to the directory to load.
108
     *
109
     * @return void
110
     */
111
    public function loadDirectory(string $path)
112
    {
113
        foreach (new DirectoryIterator($path) as $file) {
114
            if ($file->isDot())
115
                continue;
116
117
            $this->loadFile($file->getPathname());
118
        }
119
    }
120
121
    /**
122
     * Load a single file into memory.
123
     *
124
     * @param string $file The Rivescript file to load.
125
     *
126
     * @return void
127
     */
128
    public function loadFile(string $file)
129
    {
130
        $file = new SplFileObject($file);
131
132
        while (!$file->eof()) {
133
            $line = $file->fgets();
134
            $this->writeToMemory($line);
135
        }
136
    }
137
138
    /**
139
     * Write rivescript interpretable strings into
140
     * its collective brain.
141
     *
142
     * @param string $text The text to write to memory.
143
     *
144
     * @return void
145
     */
146
    public function writeToMemory(string $text)
147
    {
148
        if ($this->stream) {
149
            fwrite($this->stream, $text);
0 ignored issues
show
Bug introduced by
It seems like $this->stream can also be of type true; however, parameter $stream of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
            fwrite(/** @scrutinizer ignore-type */ $this->stream, $text);
Loading history...
150
        }
151
    }
152
}
153