Completed
Push — master ( 522199...324a18 )
by Dmitry
04:01
created

File::processFiles()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 22
ccs 0
cts 17
cp 0
crap 20
rs 8.9197
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
/**
13
 * Created by PhpStorm.
14
 * User: tofid
15
 * Date: 11.02.15
16
 * Time: 17:59.
17
 */
18
namespace hipanel\behaviors;
19
20
use hipanel\base\Model;
21
use hipanel\components\FileStorage;
22
use Yii;
23
use yii\base\Behavior;
24
use yii\db\BaseActiveRecord;
25
use yii\web\UploadedFile;
26
27
class File extends Behavior
28
{
29
    /**
30
     * @var string the attribute which holds the attachment.
31
     */
32
    public $attribute = 'file';
33
34
    /**
35
     * @var array the scenarios in which the behavior will be triggered
36
     */
37
    public $scenarios = [];
38
39
    /**
40
     * @var string the attribute that will receive the file id
41
     */
42
    public $targetAttribute;
43
44
    /**
45
     * @return array
46
     */
47
    public function events()
48
    {
49
        return [
50
            BaseActiveRecord::EVENT_BEFORE_INSERT => 'processFiles',
51
            BaseActiveRecord::EVENT_BEFORE_UPDATE => 'processFiles',
52
        ];
53
    }
54
55
    /**
56
     * Event handler for beforeInsert and beforeUpdate actions
57
     *
58
     * @param \yii\base\ModelEvent $event
59
     */
60
    public function processFiles($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        /** @var Model $model */
63
        $model = $this->owner;
64
        $ids = [];
65
66
        if (in_array($model->scenario, $this->scenarios, true)) {
67
            $files = UploadedFile::getInstances($model, $this->attribute);
68
69
            foreach ($files as $file) {
70
                $model = $this->uploadFile($file);
71
                $ids[] = $model->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\models\File>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
72
            }
73
74
            if (!empty($ids)) {
75
                $this->owner->{$this->targetAttribute} = implode(',', $ids);
76
            } else {
77
                // Protect attribute
78
                unset($model->{$this->attribute});
79
            }
80
        }
81
    }
82
83
    /**
84
     * Uploads file to the API server
85
     *
86
     * @param UploadedFile $file
87
     * @return \hipanel\models\File
88
     */
89
    private function uploadFile(UploadedFile $file)
90
    {
91
        /** @var FileStorage $fileStorage */
92
        $fileStorage = Yii::$app->get('fileStorage');
93
94
        $filename = $fileStorage->saveUploadedFile($file);
95
        return $fileStorage->put($filename, $file->name);
96
    }
97
}
98