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

OwnerMediafile::getAudioFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Itstructure\MFUploader\models;
4
5
use yii\db\ActiveQuery;
6
use yii\base\InvalidArgumentException;
7
use yii\helpers\ArrayHelper;
8
use Itstructure\MFUploader\interfaces\UploadModelInterface;
9
10
/**
11
 * This is the model class for table "owners_mediafiles".
12
 *
13
 * @property int $mediafileId Mediafile id.
14
 * @property Mediafile $mediafile
15
 *
16
 * @package Itstructure\MFUploader\models
17
 *
18
 * @author Andrey Girnik <[email protected]>
19
 */
20
class OwnerMediafile extends Owner
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public static function tableName()
26
    {
27
        return 'owners_mediafiles';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 View Code Duplication
    public function rules()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
34
    {
35
        return ArrayHelper::merge(parent::rules(), [
36
            [
37
                'mediafileId',
38
                'required',
39
            ],
40
            [
41
                'mediafileId',
42
                'integer',
43
            ],
44
            [
45
                [
46
                    'mediafileId',
47
                    'ownerId',
48
                    'owner',
49
                    'ownerAttribute',
50
                ],
51
                'unique',
52
                'targetAttribute' => [
53
                    'mediafileId',
54
                    'ownerId',
55
                    'owner',
56
                    'ownerAttribute',
57
                ],
58
            ],
59
            [
60
                ['mediafileId'],
61
                'exist',
62
                'skipOnError' => true,
63
                'targetClass' => Mediafile::class,
64
                'targetAttribute' => ['mediafileId' => 'id'],
65
            ],
66
        ]);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function attributeLabels()
73
    {
74
        return ArrayHelper::merge(parent::attributeLabels(), [
75
            'mediafileId' => 'Mediafile ID',
76
        ]);
77
    }
78
79
    /**
80
     * @return \yii\db\ActiveQuery
81
     */
82
    public function getMediaFile()
83
    {
84
        return $this->hasOne(Mediafile::class, ['id' => 'mediafileId']);
85
    }
86
87
    /**
88
     * Get all mediafiles by owner.
89
     *
90
     * @param string $owner
91
     * @param int    $ownerId
92
     * @param null|string $ownerAttribute
93
     *
94
     * @return Mediafile[]
95
     */
96 View Code Duplication
    public static function getMediaFiles(string $owner, int $ownerId, string $ownerAttribute = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
97
    {
98
        return static::getMediaFilesQuery([
99
            'owner' => $owner,
100
            'ownerId' => $ownerId,
101
            'ownerAttribute' => $ownerAttribute,
102
        ])->all();
103
    }
104
105
    /**
106
     * Get all mediafiles Query by owner.
107
     *
108
     * @param array $args. It can be an array of the next params: owner{string}, ownerId{int}, ownerAttribute{string}.
0 ignored issues
show
Bug introduced by
There is no parameter named $args.. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
109
     *
110
     * @return ActiveQuery
111
     */
112
    public static function getMediaFilesQuery(array $args = []): ActiveQuery
113
    {
114
        return Mediafile::find()
115
            ->where([
116
                'id' => self::getEntityIdsQuery('mediafileId', $args)->asArray()
117
            ]);
118
    }
119
120
    /**
121
     * Get one owner thumbnail file by owner.
122
     *
123
     * @param string $owner
124
     * @param int    $ownerId
125
     *
126
     * @return array|null|\yii\db\ActiveRecord|Mediafile
127
     */
128
    public static function getOwnerThumbnail(string $owner, int $ownerId)
129
    {
130
        $mediafileId = self::getEntityIdsQuery('mediafileId', [
131
            'owner' => $owner,
132
            'ownerId' => $ownerId,
133
            'ownerAttribute' => UploadModelInterface::FILE_TYPE_THUMB,
134
        ])->one();
135
136
        if (null === $mediafileId) {
137
            return null;
138
        }
139
140
        return Mediafile::find()
141
            ->where([
142
                'id' =>  $mediafileId->mediafileId
143
            ])->one();
144
    }
145
146
    /**
147
     * Get image files by owner.
148
     *
149
     * @param string $owner
150
     * @param int    $ownerId
151
     *
152
     * @return Mediafile[]
153
     */
154
    public static function getImageFiles(string $owner, int $ownerId)
155
    {
156
        return static::getMediaFiles($owner, $ownerId, UploadModelInterface::FILE_TYPE_IMAGE);
157
    }
158
159
    /**
160
     * Get audio files by owner.
161
     *
162
     * @param string $owner
163
     * @param int    $ownerId
164
     *
165
     * @return Mediafile[]
166
     */
167
    public static function getAudioFiles(string $owner, int $ownerId)
168
    {
169
        return static::getMediaFiles($owner, $ownerId, UploadModelInterface::FILE_TYPE_AUDIO);
170
    }
171
172
    /**
173
     * Get video files by owner.
174
     *
175
     * @param string $owner
176
     * @param int    $ownerId
177
     *
178
     * @return Mediafile[]
179
     */
180
    public static function getVideoFiles(string $owner, int $ownerId)
181
    {
182
        return static::getMediaFiles($owner, $ownerId, UploadModelInterface::FILE_TYPE_VIDEO);
183
    }
184
185
    /**
186
     * Get app files by owner.
187
     *
188
     * @param string $owner
189
     * @param int    $ownerId
190
     *
191
     * @return Mediafile[]
192
     */
193
    public static function getAppFiles(string $owner, int $ownerId)
194
    {
195
        return static::getMediaFiles($owner, $ownerId, UploadModelInterface::FILE_TYPE_APP);
196
    }
197
198
    /**
199
     * Get text files by owner.
200
     *
201
     * @param string $owner
202
     * @param int    $ownerId
203
     *
204
     * @return Mediafile[]
205
     */
206
    public static function getTextFiles(string $owner, int $ownerId)
207
    {
208
        return static::getMediaFiles($owner, $ownerId, UploadModelInterface::FILE_TYPE_TEXT);
209
    }
210
211
    /**
212
     * Get other files by owner.
213
     *
214
     * @param string $owner
215
     * @param int    $ownerId
216
     *
217
     * @return Mediafile[]
218
     */
219
    public static function getOtherFiles(string $owner, int $ownerId)
220
    {
221
        return static::getMediaFiles($owner, $ownerId, UploadModelInterface::FILE_TYPE_OTHER);
222
    }
223
224
    /**
225
     * Get model mediafile primary key name.
226
     *
227
     * @return string
228
     */
229
    protected static function getModelKeyName(): string
230
    {
231
        return 'mediafileId';
232
    }
233
}
234