Completed
Push — master ( dfef82...1a995f )
by Andrii
02:58
created

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