Completed
Push — master ( 24d81b...f92269 )
by Andrii
02:51
created

BaseHandler::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\handlers;
13
14
use Yii;
15
use yii\base\InvalidConfigException;
16
use yii\helpers\ArrayHelper;
17
18
/**
19
 * Base Handler.
20
 * Knows how to parse and render it's file type.
21
 */
22
class BaseHandler extends \yii\base\Object
23
{
24
    /**
25
     * @var Goal
26
     */
27
    public $goal;
28
29
    /**
30
     * @var string template file name to be used for rendering
31
     */
32
    public $template;
33
34
    /**
35
     * @var View object.
36
     */
37
    protected $_view;
38
39
    /**
40
     * Returns the view object that can be used to render views or view files.
41
     * If not set, it will default to the "view" application component.
42
     *
43
     * @return View|\yii\web\View the view object that can be used to render views or view files.
44
     */
45
    public function getView()
46
    {
47
        if ($this->_view === null) {
48
            $this->_view = Yii::$app->getView();
49
        }
50
51
        return $this->_view;
52
    }
53
54
    /**
55
     * Renders prepared data.
56
     * Must be redefined in child.
57
     *
58
     * @param array $data
59
     *
60
     * @throws InvalidConfigException
61
     *
62
     * @return string file content
63
     */
64
    public function renderPrepared(array $data)
65
    {
66
        throw new InvalidConfigException('Render not available');
67
    }
68
69
    /**
70
     * Renders raw data.
71
     * Prepares data with ArrayHelper::toArray.
72
     *
73
     * @param mixed $data
74
     *
75
     * @return string file content
76
     */
77
    public function render($data)
78
    {
79
        return $this->renderPrepared($this->prepareData($data));
80
    }
81
82
    public function prepareData($data)
83
    {
84
        return ArrayHelper::toArray($data, [], false);
85
        #return $data;
86
    }
87
88
    /**
89
     * Renders file content using given data.
90
     * Converts to array with ArrayHelper::toArray.
91
     *
92
     * @param mixed $data
93
     *
94
     * @return string file content
95
     */
96
    public function renderPath($path, $data)
97
    {
98
        return $this->write($path, $this->render($data));
99
    }
100
101
    public function parsePath($path, $minimal = null)
102
    {
103
        return $this->parse($this->read($path));
0 ignored issues
show
Bug introduced by
It seems like $this->read($path) targeting hidev\handlers\BaseHandler::read() can also be of type array; however, hidev\handlers\BaseHandler::parse() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
104
    }
105
106
    /**
107
     * Parses string input. To be redefined in real handlers.
108
     * @param string $json 
109
     * @return array
110
     */
111
    public function parse($json)
112
    {
113
        return [];
114
    }
115
116
    /**
117
     * Writes given content to the file.
118
     * Doesn't touch file if it has exactly same content.
119
     * Creates intermediate directories when necessary.
120
     * @param string $path
121
     * @param string $content
122
     * @return bool true if file was changed
123
     */
124
    public function write($path, $content)
125
    {
126
        if (!is_file($path) || file_get_contents($path) !== $content) {
127
            $this->mkdir(dirname($path));
128
            file_put_contents($path, $content);
129
            Yii::warning('Written file: ' . $path, 'file');
130
131
            return true;
132
        }
133
134
        return false;
135
    }
136
137
    /**
138
     * Creates directory if not exists.
139
     * @param string $path
140
     * @return bool true if directory did not exist and was created
141
     */
142
    public function mkdir($path)
143
    {
144
        $path = trim(trim($path), '/');
145
        if (!file_exists($path)) {
146
            mkdir($path, 0777, true);
147
            Yii::warning('Created dir:  ' . $path . '/', 'file');
148
149
            return true;
150
        }
151
152
        return false;
153
    }
154
155
    /**
156
     * Read file into a string or array.
157
     * @param string $path
158
     * @param bool $asArray
159
     * @return string|array
160
     */
161
    public function read($path, $asArray = false)
162
    {
163
        if (file_exists($path)) {
164
            Yii::info('Read file: ' . $path, 'file');
165
166
            return $asArray ? file($path) : file_get_contents($path);
167
        } else {
168
            Yii::error('Couldn\'t read file: ' . $path, 'file');
169
170
            return;
171
        }
172
    }
173
174
    /**
175
     * Read file into array of strings.
176
     * @param string $path
177
     * @return array
178
     */
179
    public function readArray($path)
180
    {
181
        return $this->read($path, true);
182
    }
183
}
184