PivotFileSaveBehavior   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
eloc 39
c 2
b 0
f 0
dl 0
loc 74
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A canSetProperty() 0 3 1
A getModelClass() 0 3 1
B setPivots() 0 23 9
A __set() 0 6 2
A savePivots() 0 9 5
A getStoredPivots() 0 7 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 Closure;
10
use yii\db\ActiveRecord;
11
use yii\web\UploadedFile;
12
13
/**
14
 * Class PivotFileSaveBehavior
15
 *
16
 * @package backend\behaviors
17
 * @property PivotTrait|ActiveRecord $owner
18
 */
19
class PivotFileSaveBehavior extends \yii\base\Behavior
20
{
21
    protected $_pivots;
22
    public $attribute;
23
    public $pivotClass;
24
    public $fileProcess;
25
    public $fileClass;
26
    public $pivotAttributes;
27
28
    public function canSetProperty($name, $checkVars = true)
29
    {
30
        return $name === $this->attribute;
31
    }
32
33
    public function __set($name, $value)
34
    {
35
        if ($this->canSetProperty($name)) {
36
            $this->setPivots($value);
37
        } else {
38
            parent::__set($name, $value);
39
        }
40
    }
41
42
    public function getStoredPivots($attribute)
43
    {
44
        if ($attribute === $this->attribute) {
45
            return $this->_pivots;
46
        }
47
48
        return null;
49
    }
50
51
    /**
52
     * @return ActiveRecord|FileUploadTrait
53
     */
54
    protected function getModelClass()
55
    {
56
        return $this->fileClass;
57
    }
58
59
    protected function setPivots($values)
60
    {
61
        if (empty(array_filter((array)$values))) {
62
            $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

62
            $values = UploadedFile::getInstances(/** @scrutinizer ignore-type */ $this->owner, $this->attribute);
Loading history...
63
        }
64
        $class = $this->getModelClass();
65
        $this->_pivots = [];
66
        foreach ((array)$values as $value) {
67
            if (is_numeric($value)) {
68
                $this->_pivots[] = $class::findOne($value);
69
            } elseif ($this->fileProcess instanceof \Closure) {
70
                $value = call_user_func_array($this->fileProcess, [$value, $class]);
71
                if (is_array($value)) {
72
                    $this->_pivots = array_merge($this->_pivots, $value);
73
                } else {
74
                    $this->_pivots[] = $value;
75
                }
76
            } elseif ($value instanceof UploadedFile || is_string($value)) {
77
                $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

77
                $this->_pivots[] = $class::/** @scrutinizer ignore-call */ startUpload($value)->process();
Loading history...
78
            }
79
        }
80
        $eventName = $this->owner->isNewRecord ? ActiveRecord::EVENT_AFTER_INSERT : ActiveRecord::EVENT_AFTER_UPDATE;
81
        $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

81
        $this->owner->/** @scrutinizer ignore-call */ 
82
                      on($eventName, [$this, 'savePivots']);
Loading history...
82
    }
83
84
    public function savePivots()
85
    {
86
        foreach ((array)$this->_pivots as $index => $pv) {
87
            if ($this->pivotAttributes instanceof Closure) {
88
                $attributes = call_user_func_array($this->pivotAttributes, [$pv, $index]);
89
            } else {
90
                $attributes = $this->pivotAttributes ?: [];
91
            }
92
            $this->owner->addPivot($pv, $this->pivotClass, $attributes ?: []);
93
        }
94
    }
95
}