Completed
Push — master ( a2db3f...116d3d )
by Andrii
03:06 queued 50s
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
ccs 0
cts 3
cp 0
rs 9.4285
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) 2014-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 AbstractController implements \yii\base\Arrayable, \ArrayAccess, \IteratorAggregate
23
{
24
    use \hiqdev\yii2\collection\ObjectTrait;
25
26
    /**
27
     * @var string specifies handler to be used
28
     */
29
    public $fileType;
30
31
    /**
32
     * @var array|File the file to be handled.
33
     */
34
    protected $_file;
35
36
    protected $_template;
37
38 1
    public function setTemplate($template)
39
    {
40 1
        $this->_template = $template;
41 1
    }
42
43 1
    public function getTemplate()
44
    {
45 1
        return Helper::file2template($this->_template ?: $this->id);
46
    }
47
48
    /**
49
     * Returns file object.
50
     * Instantiates it if necessary.
51
     *
52
     * @return File
53
     */
54
    public function getFile()
55
    {
56
        if (!is_object($this->_file)) {
57
            $this->_file = Yii::createObject(array_merge([
58
                'class'    => File::className(),
59
                'template' => $this->getTemplate(),
60
                'goal'     => $this,
61
                'path'     => $this->id,
62
            ], is_string($this->_file)
63
                ? ['path' => $this->_file]
64
                : (array) $this->_file
65
            ));
66
        }
67
68
        return $this->_file;
69
    }
70
71
    /**
72
     * Sets file with given info.
73
     *
74
     * @param mixed $info could be anything that is good for File::create
75
     */
76
    public function setFile($info)
77
    {
78
        $this->_file = $info;
79
    }
80
81
    public function getDirname()
82
    {
83
        return $this->getFile()->getDirname();
84
    }
85
86
    public function getPath()
87
    {
88
        return $this->getFile()->getPath();
89
    }
90
91
    public function exists()
92
    {
93
        return $this->getFile()->exists();
94
    }
95
96
    public function read()
97
    {
98
        return $this->getFile()->read();
99
    }
100
101
    public function readArray()
102
    {
103
        return $this->getFile()->readArray();
104
    }
105
106
    public function actionPerform($name = null, $path = null)
107
    {
108
        Yii::trace("Started: '$this->id'");
109
        return $this->runActions(['before', 'make', 'after']);
110
    }
111
112
    public function actionLoad()
113
    {
114
        $data = $this->getFile()->load() ?: [];
115
        if ($data) { /// TODO think what's better
116
        //  $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...
117
            $this->setItems(ArrayHelper::merge($this->toArray(), $data));
118
        //  $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...
119
        }
120
    }
121
122
    public function actionSave()
123
    {
124
        $this->_items = Helper::uniqueConfig($this->_items);
125
        $this->getFile()->save($this);
126
        return 0;
127
    }
128
}
129