Completed
Push — master ( 820d33...b51aa1 )
by Andrey
01:56
created

Owner::removeOwner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
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' => 255,
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 $ownerAttribute
96
     *
97
     * @return bool
98
     */
99
    public static function removeOwner(int $ownerId, string $owner, string $ownerAttribute): bool
100
    {
101
        $deleted = static::deleteAll([
102
            'ownerId' => $ownerId,
103
            'owner' => $owner,
104
            'ownerAttribute' => $ownerAttribute,
105
        ]);
106
107
        return $deleted > 0;
108
    }
109
110
    /**
111
     * Get Id's by owner.
112
     *
113
     * @param string $nameId
114
     * @param array $args It can be an array of the next params: owner{string}, ownerId{int}, ownerAttribute{string}.
115
     *
116
     * @throws InvalidArgumentException
117
     *
118
     * @return ActiveQuery
119
     */
120
    protected static function getEntityIdsQuery(string $nameId, array $args): ActiveQuery
121
    {
122
        $conditions = [];
123
124
        if (isset($args['owner'])) {
125
            if (!is_string($args['owner']) || empty($args['owner'])) {
126
                throw new InvalidArgumentException('Parameter owner must be a string.');
127
            }
128
            $conditions['owner'] = $args['owner'];
129
130
            if (isset($args['ownerId'])) {
131
                if (!is_numeric($args['ownerId'])) {
132
                    throw new InvalidArgumentException('Parameter ownerId must be numeric.');
133
                }
134
                $conditions['ownerId'] = $args['ownerId'];
135
            }
136
        }
137
138
        if (isset($args['ownerAttribute'])) {
139
            if (!is_string($args['ownerAttribute']) || empty($args['ownerAttribute'])) {
140
                throw new InvalidArgumentException('Parameter ownerAttribute must be a string.');
141
            }
142
            $conditions['ownerAttribute'] = $args['ownerAttribute'];
143
        }
144
145
        $query = static::find()
146
            ->select($nameId);
147
148
        if (count($conditions) > 0) {
149
            return $query->where($conditions);
150
        }
151
152
        return $query;
153
    }
154
}
155