PivotSaveBehavior::canSetProperty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
11
/**
12
 * Class PivotSaveBehavior
13
 *
14
 * @package app\behaviors
15
 * @property PivotTrait|ActiveRecord $owner
16
 */
17
class PivotSaveBehavior extends \yii\base\Behavior
18
{
19
    protected $_pivots;
20
    public $attribute;
21
    public $pivotClass;
22
    public $modelClass;
23
    public $prepareValues;
24
    public $deletePivotsBeforeSave = true;
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->modelClass;
55
    }
56
57
    protected function setPivots($values)
58
    {
59
        $class = $this->getModelClass();
60
        $this->_pivots = [];
61
        if ($this->prepareValues instanceof \Closure) {
62
            $values = call_user_func($this->prepareValues, (array)$values);
63
        }
64
        foreach ((array)$values as $value) {
65
            if (is_numeric($value)) {
66
                $this->_pivots[] = $class::findOne($value);
67
            } elseif ($value instanceof $class) {
68
                $this->_pivots[] = $value;
69
            }
70
        }
71
        $eventName = $this->owner->isNewRecord ? ActiveRecord::EVENT_AFTER_INSERT : ActiveRecord::EVENT_AFTER_UPDATE;
72
        $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

72
        $this->owner->/** @scrutinizer ignore-call */ 
73
                      on($eventName, [$this, 'savePivots']);
Loading history...
73
    }
74
75
    public function savePivots()
76
    {
77
        if (!method_exists($this->owner, 'addPivot')) {
78
            throw new \Exception('Class ' . get_class($this->owner) . ' must use carono\yii2migrate\traits\PivotTrait trait');
79
        }
80
        if ($this->deletePivotsBeforeSave && $this->_pivots !== null) {
81
            $this->owner->deletePivots($this->pivotClass);
82
        }
83
        if ($this->_pivots) {
84
            foreach ($this->_pivots as $pv) {
85
                $this->owner->addPivot($pv, $this->pivotClass);
86
            }
87
        }
88
    }
89
}