TemplateBackgroundUpload   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 6 1
A attributeLabels() 0 6 1
A upload() 0 18 4
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use app\models\types\Media;
7
8
/**
9
 * This is the model class for template background upload.
10
 *
11
 * @property file $background
12
 */
13
class TemplateBackgroundUpload extends \yii\base\Model
14
{
15
    const PATH = 'background/';
16
    public $background;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function rules()
22
    {
23
        return [
24
            [['background'], 'image', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, gif'],
25
        ];
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function attributeLabels()
32
    {
33
        return [
34
            'background' => Yii::t('app', 'Background upload'),
35
        ];
36
    }
37
38
    /**
39
     * Uploads a new background to storage and saves model.
40
     *
41
     * @param \yii\web\UploadedFile|null $fileInstance
42
     *
43
     * @return bool success
44
     */
45
    public function upload($fileInstance)
46
    {
47
        if ($fileInstance === null) {
48
            return false;
49
        }
50
51
        $this->background = $fileInstance;
52
        $name = $this->background->baseName.'.'.$this->background->extension;
53
        $filepath = Media::BASE_PATH.self::PATH.$name;
54
        if ($this->validate() && $this->background->saveAs($filepath)) {
55
            $model = new TemplateBackground();
56
            $model->webpath = Media::BASE_URI.$filepath;
57
58
            return $model->save();
59
        }
60
61
        return false;
62
    }
63
}
64