Completed
Push — master ( 5f528b...a6b26f )
by Andrii
03:24
created

BaseHandler::getView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
1
<?php
2
3
/*
4
 * Automation tool mixed with code generator for easier continuous development
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 hidev\helpers\FileHelper;
15
use Yii;
16
use yii\base\InvalidConfigException;
17
use yii\helpers\ArrayHelper;
18
19
/**
20
 * Base Handler.
21
 * Knows how to parse and render it's file type.
22
 */
23
class BaseHandler extends \yii\base\Object
24
{
25
    /**
26
     * @var Goal
27
     */
28
    public $goal;
29
30
    /**
31
     * @var string template file name to be used for rendering
32
     */
33
    public $template;
34
35
    /**
36
     * @var View object
37
     */
38
    protected $_view;
39
40
    /**
41
     * Returns the view object that can be used to render views or view files.
42
     * If not set, it will default to the "view" application component.
43
     *
44
     * @return View|\yii\web\View the view object that can be used to render views or view files
45
     */
46 5
    public function getView()
47
    {
48 5
        if ($this->_view === null) {
49
            $this->_view = Yii::$app->getView();
50
        }
51
52 5
        return $this->_view;
53
    }
54
55
    /**
56
     * Renders prepared data.
57
     * Must be redefined in child.
58
     *
59
     * @param array $data
60
     *
61
     * @throws InvalidConfigException
62
     *
63
     * @return string file content
64
     */
65
    public function renderPrepared(array $data)
66
    {
67
        throw new InvalidConfigException('Render not available');
68
    }
69
70
    /**
71
     * Renders raw data.
72
     * Prepares data with ArrayHelper::toArray.
73
     *
74
     * @param mixed $data
75
     *
76
     * @return string file content
77
     */
78
    public function render($data)
79
    {
80
        return $this->renderPrepared($this->prepareData($data));
81
    }
82
83
    public function prepareData($data)
84
    {
85
        return ArrayHelper::toArray($data, [], false);
86
        //return $data;
87
    }
88
89
    /**
90
     * Renders file content using given data.
91
     * Converts to array with ArrayHelper::toArray.
92
     *
93
     * @param mixed $data
94
     *
95
     * @return string file content
96
     */
97
    public function renderPath($path, $data)
98
    {
99
        return $this->write($path, $this->render($data));
100
    }
101
102
    public function parsePath($path, $minimal = null)
103
    {
104
        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...
105
    }
106
107
    /**
108
     * Parses string input. To be redefined in real handlers.
109
     * @param string $input to parse
110
     * @return array
111
     */
112 5
    public function parse($input)
113
    {
114 5
        return [];
115
    }
116
117
    /**
118
     * Writes given content to the file.
119
     * Doesn't touch file if it has exactly same content.
120
     * Creates intermediate directories when necessary.
121
     * @param string $path
122
     * @param string $content
123
     * @return bool true if file was changed
124
     */
125
    public function write($path, $content)
126
    {
127
        return FileHelper::write($path, $content);
128
    }
129
130
    /**
131
     * Creates directory if not exists.
132
     * @param string $path
133
     * @return bool true if directory did not exist and was created
134
     */
135
    public function mkdir($path)
136
    {
137
        return FileHelper::mkdir($path);
138
    }
139
140
    /**
141
     * Read file into a string or array.
142
     * @param string $path
143
     * @param bool $asArray
144
     * @return string|array
145
     */
146 5
    public function read($path, $asArray = false)
147
    {
148
        if (file_exists($path)) {
149
            Yii::info('Read file: ' . $path, 'file');
150
151
            return $asArray ? file($path) : file_get_contents($path);
152
        } else {
153
            Yii::error('Couldn\'t read file: ' . $path, 'file');
154
155 5
            return;
156
        }
157 3
    }
158
159
    /**
160
     * Read file into array of strings.
161
     * @param string $path
162
     * @return array
163
     */
164
    public function readArray($path)
165
    {
166
        return $this->read($path, true);
167
    }
168
}
169