Completed
Push — master ( 3204cd...9afd5c )
by Andrii
02:53
created

FileController::actionSave()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 0
crap 3
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\controllers;
12
13
use hidev\base\File;
14
use hidev\helpers\Helper;
15
use Yii;
16
use yii\helpers\ArrayHelper;
17
18
/**
19
 * File controller.
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class FileController extends CollectionController
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected $_make = ['load', 'save', 'modify'];
28
29
    /**
30
     * @var string specifies handler to be used
31
     */
32
    public $fileType;
33
34
    /**
35
     * @var bool Don't touch file if exists
36
     */
37
    public $once;
38
39
    /**
40
     * @var string Username to change file owner to
41
     */
42
    public $chown;
43
44
    /**
45
     * @var string Group to change file group to
46
     */
47
    public $chgrp;
48
49
    /**
50
     * @var string|integer Permissions to change to
51
     */
52
    public $chmod;
53
54
    /**
55
     * @var array|File the file to be handled
56
     */
57
    protected $_file;
58
59
    /**
60
     * @var string path to copy from
61
     */
62
    protected $_copy;
63
64
    /**
65
     * @var string the path to the file
66
     */
67
    protected $_path;
68
69
    /**
70
     * @var string the template name
71
     */
72
    protected $_template;
73
74
    /**
75
     * Template setter.
76
     * @param string $template name
77
     */
78 1
    public function setTemplate($template)
79
    {
80 1
        $this->_template = $template;
81 1
    }
82
83
    /**
84
     * Template getter.
85
     */
86
    public function getTemplate()
87
    {
88
        return Helper::file2template($this->_template ?: $this->id);
89
    }
90
91
    /**
92
     * Returns the file object.
93
     * Instantiates it if necessary.
94
     *
95
     * @return File
96
     */
97 5
    public function getFile()
98
    {
99
        if (!is_object($this->_file)) {
100 5
            $this->_file = Yii::createObject(array_merge([
101 5
                'class'    => File::class,
102 5
                'template' => $this->getTemplate(),
103 5
                'goal'     => $this,
104 5
                'path'     => $this->_path ?: $this->id,
105
            ], is_string($this->_file)
106 3
                ? ['path' => $this->_file]
107 5
                : (array) $this->_file
108
            ));
109
        }
110
111
        return $this->_file;
112
    }
113
114
    /**
115
     * Sets file with given info.
116
     * @param mixed $info could be anything that is good for File::create
117
     */
118
    public function setFile($info)
119
    {
120
        $this->_file = $info;
121
    }
122
123
    /**
124
     * Sets the path to the file, but file info has precendence.
125
     * @param string $value
126
     */
127
    public function setPath($value)
128
    {
129
        $this->_path = $value;
130
    }
131
132
    /**
133
     * Copy setter. Turns this file type to `copy`.
134
     */
135
    public function setCopy($value)
136
    {
137
        $this->fileType = 'copy';
138
        $this->_copy = $value;
139
    }
140
141
    /**
142
     * Copy getter. Processes aliases.
143
     */
144
    public function getCopy()
145
    {
146
        return Yii::getAlias($this->_copy);
147
    }
148
149
    /**
150
     * Dirname getter.
151
     */
152
    public function getDirname()
153
    {
154
        return $this->getFile()->getDirname();
155
    }
156
157
    /**
158
     * Path getter.
159
     */
160
    public function getPath()
161
    {
162
        return $this->getFile()->getPath();
163
    }
164
165
    /**
166
     * Checks if the file exists.
167
     */
168
    public function exists()
169
    {
170
        return $this->getFile()->exists();
171
    }
172
173
    /**
174
     * Read the file.
175
     */
176
    public function read()
177
    {
178
        return $this->getFile()->read();
179
    }
180
181
    /**
182
     * Read the file into array.
183
     * @return array
184
     */
185
    public function readArray()
186
    {
187
        return $this->getFile()->readArray();
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function actionPerform($name = null, $path = null)
194
    {
195
        Yii::trace("Started: '$this->id'");
196
197
        return $this->runActions(['before', 'make', 'after']);
198
    }
199
200 5
    public function actionLoad()
201
    {
202
        $data = $this->getFile()->load() ?: [];
203 5
        if ($data) { /// TODO think what's better
204
        //  $this->setItems(ArrayHelper::merge($data, $this->toArray()));
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
205
            $this->setItems(ArrayHelper::merge($this->toArray(), $data));
206
        //  $this->setItems($data);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
207
        }
208 5
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213 5
    public function actionSave()
214
    {
215 5
        if ($this->once && $this->exists()) {
216
            return 0;
217
        }
218
        $this->_items = Helper::uniqueConfig($this->_items);
219
        $this->getFile()->save($this);
220
    }
221
222
    /**
223
     * Modify action.
224
     */
225 5
    public function actionModify()
226
    {
227 5
        foreach (['chown', 'chgrp', 'chmod'] as $k) {
228
            $v = $this->{$k};
229
            if ($v) {
230
                $this->file->{$k}($v);
0 ignored issues
show
Documentation introduced by
The property file does not exist on object<hidev\controllers\FileController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
231
            }
232
        }
233 5
    }
234
}
235