Completed
Push — master ( 43ed69...84af45 )
by Andrii
06:07
created

FileController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 60%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 17
c 4
b 0
f 0
lcom 1
cbo 5
dl 0
loc 105
rs 10
ccs 3
cts 5
cp 0.6

12 Methods

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