File::push()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 19
cts 19
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * File.php
4
 * @author Revin Roman http://phptime.ru
5
 */
6
7
namespace rmrevin\yii\module\File\models;
8
9
use yii\base\ModelEvent;
10
use yii\helpers\FileHelper;
11
use yii\helpers\StringHelper;
12
13
/**
14
 * Class File
15
 * @package rmrevin\yii\module\File\models
16
 *
17
 * @property integer $id
18
 * @property string $mime
19
 * @property integer $size
20
 * @property string $name
21
 * @property string $origin_name
22
 * @property string $sha1
23
 * @property boolean $image_bad
24
 *
25
 * @method \rmrevin\yii\module\File\models\queries\FileQuery hasMany($class, $link)
26
 * @method \rmrevin\yii\module\File\models\queries\FileQuery hasOne($class, $link)
27
 */
28
class File extends \yii\db\ActiveRecord
29
{
30
31
    /** @var string */
32
    public $path;
33
34
    /**
35
     * @inheritdoc
36
     */
37 6
    public function init()
38
    {
39 6
        parent::init();
40
41 6
        $this->events();
42 6
    }
43
44
    /**
45
     * @return string
46
     */
47 1
    public function __toString()
48
    {
49 1
        return $this->getWebPath();
50
    }
51
52
    /**
53
     * @param bool $recalculate
54
     * @return integer
55
     */
56 1
    public function getSize($recalculate = false)
57
    {
58 1
        return true === $recalculate ? filesize($this->getAbsolutePath()) : $this->size;
59
    }
60
61
    /**
62
     * @param bool $recalculate
63
     * @return string
64
     * @throws \yii\base\InvalidConfigException.
65
     */
66 1
    public function getMime($recalculate = false)
67
    {
68 1
        return true === $recalculate ? FileHelper::getMimeType($this->getAbsolutePath()) : $this->mime;
69
    }
70
71
    /**
72
     * @param bool $recalculate
73
     * @return string
74
     */
75 1
    public function getSha1($recalculate = false)
76
    {
77 1
        return true === $recalculate ? sha1_file($this->getAbsolutePath()) : $this->sha1;
78
    }
79
80
    /**
81
     * @return string
82
     */
83 1
    public function getWebPath()
84
    {
85 1
        $upload_path = \rmrevin\yii\module\File\Module::module()->upload_web_path;
86
87 1
        $p1 = StringHelper::byteSubstr($this->name, 0, 2);
88 1
        $p2 = StringHelper::byteSubstr($this->name, 2, 2);
89
90 1
        return $upload_path . '/' . $p1 . '/' . $p2 . '/' . $this->name;
91
    }
92
93
    /**
94
     * @return string
95
     */
96 4
    public function getAbsolutePath()
97
    {
98 4
        $upload_path = \rmrevin\yii\module\File\Module::module()->upload_path;
99
100 4
        $p1 = StringHelper::byteSubstr($this->name, 0, 2);
101 4
        $p2 = StringHelper::byteSubstr($this->name, 2, 2);
102
103 4
        return $upload_path . DIRECTORY_SEPARATOR . $p1 . DIRECTORY_SEPARATOR . $p2 . DIRECTORY_SEPARATOR . $this->name;
104
    }
105
106
    /**
107
     * @param \rmrevin\yii\module\File\resources\ResourceInterface $Resource
108
     * @return static
109
     * @throws \Exception
110
     */
111 6
    public static function push(\rmrevin\yii\module\File\resources\ResourceInterface $Resource)
112
    {
113 6
        $id = basename($Resource->getTemp());
114 6
        \Yii::beginProfile('pushing file `' . $id . '`', __METHOD__);
115
116 6
        $NewModel = new static();
117 6
        $NewModel->path = $Resource->getTemp();
118
119 6
        $Model = static::find()->bySha1($Resource->getSha1())->one();
120
121 6
        if ($Model instanceof static) {
122 1
            $NewModel = $Model;
123 1
        } else {
124 6
            $result = $Resource->moveToUpload();
125 6
            if (false !== $result) {
126 5
                $NewModel->name = basename($result);
127 5
                $NewModel->origin_name = $Resource->getName();
128 5
                $NewModel->path = $result;
129 5
                $NewModel->insert();
130 5
            } else {
131 1
                throw new \RuntimeException(\Yii::t('service-file', 'Failed to bring the resource directory of uploaded files.'));
132
            }
133
        }
134 5
        \Yii::endProfile('pushing file `' . $id . '`', __METHOD__);
135
136 5
        return $NewModel;
137
    }
138
139
    /**
140
     * @return static
141
     */
142 2
    public static function getNoImage()
143
    {
144 2
        $image = \Yii::getAlias(\rmrevin\yii\module\File\Module::module()->no_image_alias);
145 2
        $Resource = new \rmrevin\yii\module\File\resources\InternalResource($image);
146
147 2
        return static::push($Resource);
148
    }
149
150
    /**
151
     * @return \rmrevin\yii\module\File\ImageWrapper
152
     * @throws \Exception
153
     */
154 4
    public function image()
155
    {
156 4
        \Yii::beginProfile('manipulating with file `' . $this->id . '`', 'services\File\models\File');
157
158 4
        $result = null;
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
159 4
        if ($this->isImage()) {
160 3
            $result = \rmrevin\yii\module\File\ImageWrapper::load($this);
161 3
        } else {
162 1
            $File = static::getNoImage();
163 1
            $result = \rmrevin\yii\module\File\ImageWrapper::load($File);
164 1
            $this->image_bad = true;
165 1
            $this->update();
166
        }
167
168 4
        return $result;
169
    }
170
171
    /**
172
     * @return bool
173
     */
174 4
    public function isImage()
175
    {
176 4
        return @getimagesize($this->getAbsolutePath()) !== false;
177
    }
178
179
    /**
180
     * @return \rmrevin\yii\module\File\models\queries\FileQuery|\yii\db\ActiveQuery
181
     */
182 6
    public static function find()
183
    {
184 6
        return new queries\FileQuery(get_called_class());
185
    }
186
187
    /**
188
     * @inheritdoc
189
     */
190 6
    public static function tableName()
191
    {
192 6
        return '{{%file}}';
193
    }
194
195
    /**
196
     * Events
197
     */
198
    private function events()
199
    {
200 6
        $this->on(static::EVENT_BEFORE_INSERT, function (ModelEvent $Event) {
201
            /** @var static $Model */
202 5
            $Model = $Event->sender;
203 5
            $Model->size = filesize($Model->path);
204 5
            $Model->mime = FileHelper::getMimeType($Model->path);
205 5
            $Model->sha1 = sha1_file($Model->path);
206 6
        });
207
    }
208
}