UploadBehavior   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 24
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B afterSave() 0 16 5
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