Completed
Push — master ( 2e8bb7...08d57c )
by Andrey
01:28
created

Owner::buildFilterOptions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 3
1
<?php
2
3
namespace Itstructure\MFUploader\models;
4
5
use yii\db\ActiveQuery;
6
use yii\base\InvalidArgumentException;
7
8
/**
9
 * This is the base class for owners.
10
 *
11
 * @property int $ownerId Owner id.
12
 * @property string $owner Owner name (post, article, page e.t.c.).
13
 * @property string $ownerAttribute Owner attribute (thumbnail, image e.t.c.).
14
 *
15
 * @package Itstructure\MFUploader\models
16
 *
17
 * @author Andrey Girnik <[email protected]>
18
 */
19
abstract class Owner extends \yii\db\ActiveRecord
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function rules()
25
    {
26
        return [
27
            [
28
                [
29
                    'ownerId',
30
                    'owner',
31
                    'ownerAttribute',
32
                ],
33
                'required',
34
            ],
35
            [
36
                'ownerId',
37
                'integer',
38
            ],
39
            [
40
                [
41
                    'owner',
42
                    'ownerAttribute',
43
                ],
44
                'string',
45
                'max' => 64,
46
            ],
47
        ];
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function attributeLabels()
54
    {
55
        return [
56
            'ownerId' => 'Owner ID',
57
            'owner' => 'Owner',
58
            'ownerAttribute' => 'Owner Attribute',
59
        ];
60
    }
61
62
    /**
63
     * Get model (mediafile/album) primary key name.
64
     *
65
     * @return string
66
     */
67
    abstract protected static function getModelKeyName(): string;
68
69
    /**
70
     * Add owner to mediafiles table.
71
     *
72
     * @param int    $modelId
73
     * @param int    $ownerId
74
     * @param string $owner
75
     * @param string $ownerAttribute
76
     *
77
     * @return bool
78
     */
79
    public static function addOwner(int $modelId, int $ownerId, string $owner, string $ownerAttribute): bool
80
    {
81
        $ownerModel = new static();
82
        $ownerModel->{static::getModelKeyName()} = $modelId;
83
        $ownerModel->ownerId = $ownerId;
84
        $ownerModel->owner = $owner;
85
        $ownerModel->ownerAttribute = $ownerAttribute;
86
87
        return $ownerModel->save();
88
    }
89
90
    /**
91
     * Remove this mediafile/album owner.
92
     *
93
     * @param int $ownerId
94
     * @param string $owner
95
     * @param string|null $ownerAttribute
96
     *
97
     * @return bool
98
     */
99
    public static function removeOwner(int $ownerId, string $owner, string $ownerAttribute = null): bool
100
    {
101
        $deleted = static::deleteAll(static::buildFilterOptions($ownerId, $owner, $ownerAttribute));
102
103
        return $deleted > 0;
104
    }
105
106
    /**
107
     * Getting entity id's which are related with Other owners too.
108
     *
109
     * @param string $owner
110
     * @param int $ownerId
111
     * @param array $entityIds
112
     *
113
     * @return array|\yii\db\ActiveRecord[]
114
     */
115
    public static function filterMultipliedEntityIds(string $owner, int $ownerId, array $entityIds)
116
    {
117
        return static::find()
118
            ->select(static::getModelKeyName())
119
            ->where([static::getModelKeyName() => $entityIds])
120
            ->andWhere([
121
                'OR',
122
                ['!=', 'ownerId', $ownerId],
123
                ['!=', 'owner', $owner]
124
            ])
125
            ->all();
126
    }
127
128
    /**
129
     * Get Id's by owner.
130
     *
131
     * @param string $nameId
132
     * @param array $args It can be an array of the next params: owner{string}, ownerId{int}, ownerAttribute{string}.
133
     *
134
     * @throws InvalidArgumentException
135
     *
136
     * @return ActiveQuery
137
     */
138
    protected static function getEntityIdsQuery(string $nameId, array $args): ActiveQuery
139
    {
140
        $conditions = [];
141
142
        if (!empty($args['owner'])) {
143
            if (!is_string($args['owner'])) {
144
                throw new InvalidArgumentException('Parameter owner must be a string.');
145
            }
146
            $conditions['owner'] = $args['owner'];
147
148 View Code Duplication
            if (!empty($args['ownerId'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
                if (!is_numeric($args['ownerId'])) {
150
                    throw new InvalidArgumentException('Parameter ownerId must be numeric.');
151
                }
152
                $conditions['ownerId'] = $args['ownerId'];
153
            }
154
        }
155
156 View Code Duplication
        if (!empty($args['ownerAttribute'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
            if (!is_string($args['ownerAttribute'])) {
158
                throw new InvalidArgumentException('Parameter ownerAttribute must be a string.');
159
            }
160
            $conditions['ownerAttribute'] = $args['ownerAttribute'];
161
        }
162
163
        $query = static::find()
164
            ->select($nameId);
165
166
        if (count($conditions) > 0) {
167
            return $query->where($conditions);
168
        }
169
170
        return $query;
171
    }
172
173
    /**
174
     * Build filter options for some actions.
175
     *
176
     * @param int $ownerId
177
     * @param string $owner
178
     * @param string|null $ownerAttribute
179
     *
180
     * @return array
181
     */
182
    protected static function buildFilterOptions(int $ownerId, string $owner, string $ownerAttribute = null)
183
    {
184
        return array_merge([
185
            'ownerId' => $ownerId,
186
            'owner' => $owner
187
        ], empty($ownerAttribute) ? [] : ['ownerAttribute' => $ownerAttribute]);
188
    }
189
}
190