Completed
Push — master ( dd4738...23e003 )
by Andrii
03:10
created

FileController   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 60%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 26
c 6
b 0
f 2
lcom 1
cbo 5
dl 0
loc 212
ccs 3
cts 5
cp 0.6
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setTemplate() 0 4 1
A getTemplate() 0 4 2
A getFile() 0 16 4
A setFile() 0 4 1
A setPath() 0 4 1
A setCopy() 0 5 1
A getCopy() 0 4 1
A getDirname() 0 4 1
A getPath() 0 4 1
A exists() 0 4 1
A read() 0 4 1
A readArray() 0 4 1
A actionPerform() 0 5 1
A actionLoad() 0 9 3
A actionSave() 0 8 3
A actionModify() 0 9 3
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\controllers;
13
14
use hidev\base\File;
15
use hidev\helpers\Helper;
16
use Yii;
17
use yii\helpers\ArrayHelper;
18
19
/**
20
 * File controller.
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class FileController extends CollectionController
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected $_make = ['load', 'save', 'modify'];
29
30
    /**
31
     * @var string specifies handler to be used.
32
     */
33
    public $fileType;
34
35
    /**
36
     * @var bool Don't touch file if exists.
37
     */
38
    public $once;
39
40
    /**
41
     * @var string Username to change file owner to.
42
     */
43
    public $chown;
44
45
    /**
46
     * @var string Group to change file group to.
47
     */
48
    public $chgrp;
49
50
    /**
51
     * @var string|integer Permissions to change to.
52
     */
53
    public $chmod;
54
55
    /**
56
     * @var array|File the file to be handled.
57
     */
58
    protected $_file;
59
60
    /**
61
     * @var string path to copy from.
62
     */
63
    protected $_copy;
64
65
    /**
66
     * @var string the path to the file.
67
     */
68
    protected $_path;
69
70
    /**
71
     * @var string the template name.
72
     */
73
    protected $_template;
74
75
    /**
76
     * Template setter.
77
     * @param string $template name.
78
     */
79 1
    public function setTemplate($template)
80
    {
81 1
        $this->_template = $template;
82 1
    }
83
84
    /**
85
     * Template getter.
86
     */
87
    public function getTemplate()
88
    {
89
        return Helper::file2template($this->_template ?: $this->id);
90
    }
91
92
    /**
93
     * Returns the file object.
94
     * Instantiates it if necessary.
95
     *
96
     * @return File
97
     */
98
    public function getFile()
99
    {
100
        if (!is_object($this->_file)) {
101
            $this->_file = Yii::createObject(array_merge([
102
                'class'    => File::className(),
103
                'template' => $this->getTemplate(),
104
                'goal'     => $this,
105
                'path'     => $this->_path ?: $this->id,
106
            ], is_string($this->_file)
107
                ? ['path' => $this->_file]
108
                : (array) $this->_file
109
            ));
110
        }
111
112
        return $this->_file;
113
    }
114
115
    /**
116
     * Sets file with given info.
117
     * @param mixed $info could be anything that is good for File::create
118
     */
119
    public function setFile($info)
120
    {
121
        $this->_file = $info;
122
    }
123
124
    /**
125
     * Sets the path to the file, but file info has precendence.
126
     * @param string $value
127
     */
128
    public function setPath($value)
129
    {
130
        $this->_path = $value;
131
    }
132
133
    /**
134
     * Copy setter. Turns this file type to `copy`.
135
     */
136
    public function setCopy($value)
137
    {
138
        $this->fileType = 'copy';
139
        $this->_copy = $value;
140
    }
141
142
    /**
143
     * Copy getter. Processes aliases.
144
     */
145
    public function getCopy()
146
    {
147
        return Yii::getAlias($this->_copy);
148
    }
149
150
    /**
151
     * Dirname getter.
152
     */
153
    public function getDirname()
154
    {
155
        return $this->getFile()->getDirname();
156
    }
157
158
    /**
159
     * Path getter.
160
     */
161
    public function getPath()
162
    {
163
        return $this->getFile()->getPath();
164
    }
165
166
    /**
167
     * Checks if the file exists.
168
     */
169
    public function exists()
170
    {
171
        return $this->getFile()->exists();
172
    }
173
174
    /**
175
     * Read the file.
176
     */
177
    public function read()
178
    {
179
        return $this->getFile()->read();
180
    }
181
182
    /**
183
     * Read the file into array.
184
     * @return array
185
     */
186
    public function readArray()
187
    {
188
        return $this->getFile()->readArray();
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function actionPerform($name = null, $path = null)
195
    {
196
        Yii::trace("Started: '$this->id'");
197
        return $this->runActions(['before', 'make', 'after']);
198
    }
199
200
    public function actionLoad()
201
    {
202
        $data = $this->getFile()->load() ?: [];
203
        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
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function actionSave()
214
    {
215
        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
    public function actionModify()
226
    {
227
        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
    }
234
}
235