UploadBehavior::afterSave()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 5
eloc 10
nc 4
nop 0
1
<?php
2
3
namespace zacksleo\yii2\post\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
12
    /**
13
     * This method is called at the end of inserting or updating a record.
14
     * @throws \yii\base\InvalidParamException
15
     */
16
    public function afterSave()
17
    {
18
        /** @var \yii\db\BaseActiveRecord $model */
19
        $model = $this->owner;
20
        if (in_array($model->scenario, $this->scenarios)) {
21
            if ($this->getUploadedFile() instanceof UploadedFile) {
22
                $path = $this->getUploadPath($this->attribute);
23
                if (is_string($path) && FileHelper::createDirectory(dirname($path))) {
24
                    $this->save($this->getUploadedFile(), $path);
25
                    $this->afterUpload();
26
                } else {
27
                    throw new InvalidParamException("Directory specified in 'path' attribute doesn't exist or cannot be created.");
28
                }
29
            }
30
        }
31
    }
32
}
33