Passed
Push — master ( 91d0ca...a7e442 )
by Aleksandr
01:33
created

PivotFileSaveBehavior::setPivots()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
cc 7
nc 20
nop 1
1
<?php
2
3
4
namespace carono\yii2file\behaviors;
5
6
7
use carono\yii2file\FileUploadTrait;
8
use carono\yii2migrate\traits\PivotTrait;
9
use yii\db\ActiveRecord;
10
use yii\web\UploadedFile;
11
12
/**
13
 * Class PivotFileSaveBehavior
14
 *
15
 * @package backend\behaviors
16
 * @property PivotTrait|ActiveRecord $owner
17
 */
18
class PivotFileSaveBehavior extends \yii\base\Behavior
19
{
20
    protected $_pivots;
21
    public $attribute;
22
    public $pivotClass;
23
    public $fileProcess;
24
    public $fileClass;
25
26
    public function canSetProperty($name, $checkVars = true)
27
    {
28
        return $name === $this->attribute;
29
    }
30
31
    public function __set($name, $value)
32
    {
33
        if ($this->canSetProperty($name)) {
34
            $this->setPivots($value);
35
        } else {
36
            parent::__set($name, $value);
37
        }
38
    }
39
40
    public function getStoredPivots($attribute)
41
    {
42
        if ($attribute === $this->attribute) {
43
            return $this->_pivots;
44
        }
45
46
        return null;
47
    }
48
49
    /**
50
     * @return ActiveRecord|FileUploadTrait
51
     */
52
    protected function getModelClass()
53
    {
54
        return $this->fileClass;
55
    }
56
57
    protected function setPivots($values)
58
    {
59
        if (empty(array_filter((array)$values))) {
60
            $values = UploadedFile::getInstances($this->owner, $this->attribute);
0 ignored issues
show
Bug introduced by
It seems like $this->owner can also be of type carono\yii2migrate\traits\PivotTrait; however, parameter $model of yii\web\UploadedFile::getInstances() does only seem to accept yii\base\Model, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            $values = UploadedFile::getInstances(/** @scrutinizer ignore-type */ $this->owner, $this->attribute);
Loading history...
61
        }
62
        $class = $this->getModelClass();
63
        $this->_pivots = [];
64
        foreach ((array)$values as $value) {
65
            if (is_numeric($value)) {
66
                $this->_pivots[] = $class::findOne($value);
67
            } elseif ($this->fileProcess instanceof \Closure) {
68
                $this->_pivots[] = call_user_func_array($this->fileProcess, [$value, $class]);
69
            } elseif ($value instanceof UploadedFile) {
70
                $this->_pivots[] = $class::startUpload($value)->process();
0 ignored issues
show
introduced by
The method startUpload() does not exist on yii\db\ActiveRecord. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
                $this->_pivots[] = $class::/** @scrutinizer ignore-call */ startUpload($value)->process();
Loading history...
71
            }
72
        }
73
        $eventName = $this->owner->isNewRecord ? ActiveRecord::EVENT_AFTER_INSERT : ActiveRecord::EVENT_AFTER_UPDATE;
74
        $this->owner->on($eventName, [$this, 'savePivots']);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        $this->owner->/** @scrutinizer ignore-call */ 
75
                      on($eventName, [$this, 'savePivots']);
Loading history...
75
    }
76
77
    public function savePivots()
78
    {
79
        foreach ((array)$this->_pivots as $pv) {
80
            $this->owner->addPivot($pv, $this->pivotClass);
81
        }
82
    }
83
}