Completed
Push — master ( edb2b9...da334f )
by Dmitry
13:12
created

src/behaviors/File.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\behaviors;
12
13
use hipanel\base\Model;
14
use hipanel\components\FileStorage;
15
use Yii;
16
use yii\base\Behavior;
17
use yii\db\BaseActiveRecord;
18
use yii\web\UploadedFile;
19
20
class File extends Behavior
21
{
22
    /**
23
     * @var string the attribute which holds the attachment
24
     */
25
    public $attribute = 'file';
26
27
    /**
28
     * @var array the scenarios in which the behavior will be triggered
29
     */
30
    public $scenarios = [];
31
32
    /**
33
     * @var string the attribute that will receive the file id
34
     */
35
    public $targetAttribute;
36
37
    /**
38
     * @return array
39
     */
40
    public function events()
41
    {
42
        return [
43
            BaseActiveRecord::EVENT_BEFORE_INSERT => 'processFiles',
44
            BaseActiveRecord::EVENT_BEFORE_UPDATE => 'processFiles',
45
        ];
46
    }
47
48
    /**
49
     * Event handler for beforeInsert and beforeUpdate actions.
50
     *
51
     * @param \yii\base\ModelEvent $event
52
     */
53
    public function processFiles($event = null)
54
    {
55
        /** @var Model $model */
56
        $model = $this->owner;
57
        $ids = [];
58
59
        if (in_array($model->scenario, $this->scenarios, true)) {
60
            $files = UploadedFile::getInstances($model, $this->attribute);
61
62
            foreach ($files as $file) {
63
                $model = $this->uploadFile($file);
64
                $ids[] = $model->id;
0 ignored issues
show
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...
65
            }
66
67
            if (!empty($ids)) {
68
                $this->owner->{$this->targetAttribute} = implode(',', $ids);
69
            } else {
70
                // Protect attribute
71
                $model->{$this->attribute} = null;
72
            }
73
        }
74
    }
75
76
    /**
77
     * Uploads file to the API server.
78
     *
79
     * @param UploadedFile $file
80
     * @return \hipanel\models\File
81
     */
82
    private function uploadFile(UploadedFile $file)
83
    {
84
        /** @var FileStorage $fileStorage */
85
        $fileStorage = Yii::$app->get('fileStorage');
86
87
        $filename = $fileStorage->saveUploadedFile($file);
88
89
        return $fileStorage->put($filename, $file->name);
90
    }
91
}
92