Passed
Push — master ( 4d6f69...91d0ca )
by Aleksandr
01:55
created

PivotSaveBehavior   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 29
c 1
b 0
f 0
dl 0
loc 62
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A canSetProperty() 0 3 1
A setPivots() 0 13 5
A __set() 0 6 2
A getStoredPivots() 0 7 2
A savePivots() 0 8 4
A getModelClass() 0 3 1
1
<?php
2
3
4
namespace app\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 $field;
22
    public $pivotClass;
23
    public $fileClass;
24
25
    public function canSetProperty($name, $checkVars = true)
26
    {
27
        return $name == $this->attribute;
28
    }
29
30
    public function __set($name, $value)
31
    {
32
        if ($this->canSetProperty($name)) {
33
            $this->setPivots($value);
34
        } else {
35
            parent::__set($name, $value);
36
        }
37
    }
38
39
    public function getStoredPivots($attribute)
40
    {
41
        if ($attribute == $this->attribute) {
42
            return $this->_pivots;
43
        }
44
45
        return null;
46
    }
47
48
    /**
49
     * @return ActiveRecord|FileUploadTrait
50
     */
51
    protected function getModelClass()
52
    {
53
        return $this->fileClass;
54
    }
55
56
    protected function setPivots($values)
57
    {
58
        $class = $this->getModelClass();
59
        $this->_pivots = [];
60
        foreach ((array)$values as $value) {
61
            if (is_numeric($value)) {
62
                $this->_pivots[] = $class::findOne($value);
63
            } elseif ($value instanceof $class) {
64
                $this->_pivots[] = $value;
65
            }
66
        }
67
        $eventName = $this->owner->isNewRecord ? ActiveRecord::EVENT_AFTER_INSERT : ActiveRecord::EVENT_AFTER_UPDATE;
68
        $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

68
        $this->owner->/** @scrutinizer ignore-call */ 
69
                      on($eventName, [$this, 'savePivots']);
Loading history...
69
    }
70
71
    public function savePivots()
72
    {
73
        if ($this->_pivots !== null) {
74
            $this->owner->deletePivots($this->pivotClass);
75
        }
76
        if ($this->_pivots) {
77
            foreach ($this->_pivots as $pv) {
78
                $this->owner->addPivot($pv, $this->pivotClass);
79
            }
80
        }
81
    }
82
}