Completed
Push — master ( 60927c...41af59 )
by Andrii
05:05
created

FileController::actionPerform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
ccs 0
cts 0
cp 0
cc 1
eloc 3
nc 1
nop 2
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\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
 */
22
class FileController extends CollectionController
23
{
24
    protected $_make   = ['load', 'save', 'modify'];
25
26
    /**
27
     * @var string specifies handler to be used
28
     */
29
    public $fileType;
30
31
    public $once;
32
    public $chown;
33
    public $chgrp;
34
    public $chmod;
35
36
    /**
37
     * @var array|File the file to be handled.
38
     */
39
    protected $_file;
40
41
    /**
42
     * @var string the path to the file.
43
     */
44
    protected $_path;
45
46
    protected $_template;
47
48 1
    public function setTemplate($template)
49
    {
50 1
        $this->_template = $template;
51 1
    }
52
53
    public function getTemplate()
54
    {
55
        return Helper::file2template($this->_template ?: $this->id);
56
    }
57
58
    /**
59
     * Returns file object.
60
     * Instantiates it if necessary.
61
     *
62
     * @return File
63
     */
64
    public function getFile()
65
    {
66
        if (!is_object($this->_file)) {
67
            $this->_file = Yii::createObject(array_merge([
68
                'class'    => File::className(),
69
                'template' => $this->getTemplate(),
70
                'goal'     => $this,
71
                'path'     => $this->_path ?: $this->id,
72
            ], is_string($this->_file)
73
                ? ['path' => $this->_file]
74
                : (array) $this->_file
75
            ));
76
        }
77
78
        return $this->_file;
79
    }
80
81
    /**
82
     * Sets file with given info.
83
     * @param mixed $info could be anything that is good for File::create
84
     */
85
    public function setFile($info)
86
    {
87
        $this->_file = $info;
88
    }
89
90
    /**
91
     * Sets the path to the file, but file info has precendence.
92
     * @param string $value
93
     */
94
    public function setPath($value)
95
    {
96
        $this->_path = $value;
97
    }
98
99
    public function getDirname()
100
    {
101
        return $this->getFile()->getDirname();
102
    }
103
104
    public function getPath()
105
    {
106
        return $this->getFile()->getPath();
107
    }
108
109
    public function exists()
110
    {
111
        return $this->getFile()->exists();
112
    }
113
114
    public function read()
115
    {
116
        return $this->getFile()->read();
117
    }
118
119
    public function readArray()
120
    {
121
        return $this->getFile()->readArray();
122
    }
123
124
    public function actionPerform($name = null, $path = null)
125
    {
126
        Yii::trace("Started: '$this->id'");
127
        return $this->runActions(['before', 'make', 'after']);
128
    }
129
130
    public function actionLoad()
131
    {
132
        $data = $this->getFile()->load() ?: [];
133
        if ($data) { /// TODO think what's better
134
        //  $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...
135
            $this->setItems(ArrayHelper::merge($this->toArray(), $data));
136
        //  $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...
137
        }
138
    }
139
140
    public function actionSave()
141
    {
142
        if ($this->once && $this->exists()) {
143
            return 0;
144
        }
145
        $this->_items = Helper::uniqueConfig($this->_items);
146
        $this->getFile()->save($this);
147
        return 0;
148
    }
149
150
    public function actionModify()
151
    {
152
        foreach (['chown', 'chgrp', 'chmod'] as $k) {
153
            $v = $this->{$k};
154
            if ($v) {
155
                $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...
156
            }
157
        }
158
    }
159
}
160