OwnerMediafile   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
c 0
b 0
f 0
dl 0
loc 208
rs 10
wmc 15

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getMediaFiles() 0 3 1
A getAppFiles() 0 3 1
A attributeLabels() 0 4 1
A getImageFiles() 0 3 1
A getVideoFiles() 0 3 1
A getOwnerThumbnail() 0 16 2
A rules() 0 32 1
A getModelKeyName() 0 3 1
A getTextFiles() 0 3 1
A tableName() 0 3 1
A getOtherFiles() 0 3 1
A getMediaFile() 0 3 1
A getAudioFiles() 0 3 1
A getMediaFilesQuery() 0 5 1
1
<?php
2
3
namespace Itstructure\MFUploader\models;
4
5
use yii\db\ActiveQuery;
6
use yii\base\InvalidArgumentException;
0 ignored issues
show
Bug introduced by
The type yii\base\InvalidArgumentException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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