UploadBehavior::afterUpload()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace zacksleo\yii2\romrelease\behaviors;
4
5
use yii\web\UploadedFile;
6
use yii\helpers\FileHelper;
7
use yii\base\InvalidParamException;
8
9
class UploadBehavior extends \mongosoft\file\UploadBehavior
10
{
11
    public function afterUpload()
12
    {
13
        $path = $this->getUploadPath($this->attribute);
14
        if (file_exists($path)) {
15
            $model = $this->owner;
16
            /* @var $model \zacksleo\yii2\romrelease\models\RomRelease */
17
            $model->md5 = md5_file($path);
18
            $model->setScenario('save');
19
            $model->save();
20
        }
21
    }
22
23
    /**
24
     * This method is called at the end of inserting or updating a record.
25
     * @throws \yii\base\InvalidParamException
26
     */
27
    public function afterSave()
28
    {
29
        /** @var \yii\db\BaseActiveRecord $model */
30
        $model = $this->owner;
31
        if (in_array($model->scenario, $this->scenarios)) {
32
            if ($this->getUploadedFile() instanceof UploadedFile) {
33
                $path = $this->getUploadPath($this->attribute);
34
                if (is_string($path) && FileHelper::createDirectory(dirname($path))) {
35
                    $this->save($this->getUploadedFile(), $path);
36
                    $this->afterUpload();
37
                } else {
38
                    throw new InvalidParamException("Directory specified in 'path' attribute doesn't exist or cannot be created.");
39
                }
40
            }
41
        }
42
    }
43
}
44