Completed
Push — master ( ea324c...2e8bb7 )
by Andrey
01:33
created

Behavior::loadModel()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Itstructure\MFUploader\behaviors;
4
5
use yii\db\{ActiveRecord, ActiveRecordInterface};
6
use yii\base\Behavior as BaseBehavior;
7
use Itstructure\MFUploader\models\album\Album;
8
use Itstructure\MFUploader\models\Mediafile;
9
10
/**
11
 * Class Behavior
12
 * Base Behavior class to add, update and remove owners of media model.
13
 *
14
 * @property string $name Owner name.
15
 * @property array $attributes Owner attribute names.
16
 * @property string $findModelKey Key, which is used to find model record.
17
 *
18
 * @package Itstructure\MFUploader\behaviors
19
 *
20
 * @author Andrey Girnik <[email protected]>
21
 */
22
abstract class Behavior extends BaseBehavior
23
{
24
    /**
25
     * Owner name.
26
     *
27
     * @var string
28
     */
29
    public $name = '';
30
31
    /**
32
     * Owner attribute names.
33
     *
34
     * @var array
35
     */
36
    public $attributes = [];
37
38
    /**
39
     * Key, which is used to find model record.
40
     *
41
     * @var string
42
     */
43
    public $findModelKey = 'id';
44
45
    /**
46
     * Load media model by conditions.
47
     *
48
     * @param array $conditions
49
     *
50
     * @return Mediafile|Album|ActiveRecordInterface|null
51
     */
52
    abstract protected function loadModel(array $conditions);
53
54
    /**
55
     * Remove media model owner.
56
     *
57
     * @param int    $ownerId
58
     * @param string $owner
59
     * @param string $ownerAttribute
60
     *
61
     * @return bool
62
     */
63
    abstract protected function removeOwner(int $ownerId, string $owner, string $ownerAttribute):bool;
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function events()
69
    {
70
        return [
71
            ActiveRecord::EVENT_AFTER_INSERT => 'addOwners',
72
            ActiveRecord::EVENT_AFTER_UPDATE => 'updateOwners',
73
            ActiveRecord::EVENT_BEFORE_DELETE => 'deleteOwners',
74
        ];
75
    }
76
77
    /**
78
     * Add owners to media model
79
     *
80
     * @return void
81
     */
82
    public function addOwners(): void
83
    {
84
        foreach ($this->attributes as $attributeName) {
85
            $this->linkModelWithOwner($attributeName, $this->owner->{$attributeName});
86
        }
87
    }
88
89
    /**
90
     * Update owners of media model
91
     *
92
     * @return void
93
     */
94
    public function updateOwners(): void
95
    {
96
        foreach ($this->attributes as $attributeName) {
97
            $this->removeOwner($this->owner->primaryKey, $this->name, $attributeName);
98
            $this->linkModelWithOwner($attributeName, $this->owner->{$attributeName});
99
        }
100
    }
101
102
    /**
103
     * Delete owners of media model
104
     *
105
     * @return void
106
     */
107
    public function deleteOwners(): void
108
    {
109
        foreach ($this->attributes as $attributeName) {
110
            $this->removeOwner($this->owner->primaryKey, $this->name, $attributeName);
111
        }
112
    }
113
114
    /**
115
     * Link media model with owner.
116
     *
117
     * @param $attributeName
118
     * @param $attributeValue
119
     *
120
     * @return void
121
     */
122
    protected function linkModelWithOwner($attributeName, $attributeValue): void
123
    {
124
        if (is_array($attributeValue)) {
125
            foreach ($attributeValue as $item) {
126
                if (empty($item)) {
127
                    continue;
128
                }
129
                $this->linkModelWithOwner($attributeName, $item);
130
            }
131
132
        } else {
133
            if (!empty($attributeValue) && $model = $this->loadModel([$this->findModelKey => $attributeValue])) {
134
                $model->addOwner($this->owner->primaryKey, $this->name, $attributeName);
135
            }
136
        }
137
    }
138
}
139