Passed
Push — master ( dd4450...9a9614 )
by Andrey
05:55
created

AdditionFieldsTrait::getMediaFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 18
rs 9.8333
1
<?php
2
3
namespace app\traits;
4
5
use Yii;
6
use yii\data\Pagination;
7
use Itstructure\MFUploader\models\OwnerMediafile;
8
use Itstructure\MFUploader\models\album\Album;
9
10
/**
11
 * Class AdditionFieldsTrait
12
 * @package app\traits
13
 */
14
trait AdditionFieldsTrait
15
{
16
    /**
17
     * @param string $ownerName
18
     * @param int $ownerId
19
     * @param string $ownerAttribute
20
     * @param array $paginationOptions
21
     * @return array
22
     */
23
    protected function getMediaFiles(string $ownerName, int $ownerId, string $ownerAttribute, array $paginationOptions = [])
24
    {
25
        $mediafilesQuery = OwnerMediafile::getMediaFilesQuery([
26
            'owner' => $ownerName,
27
            'ownerId' => $ownerId,
28
            'ownerAttribute' => $ownerAttribute,
29
        ]);
30
        $pagination = new Pagination(array_merge([
31
                'defaultPageSize' => Yii::$app->params['defaultPageSize'],
32
                'totalCount' => $mediafilesQuery->count()
33
            ], $paginationOptions)
34
        );
35
36
        return [
37
            'items' => $mediafilesQuery->offset($pagination->offset)
38
                ->limit($pagination->limit)
39
                ->all(),
40
            'pagination' => $pagination
41
        ];
42
    }
43
44
    /**
45
     * @param string|null $type
46
     * @return array|\yii\db\ActiveRecord[]
47
     */
48
    protected function getAlbums(string $type = null)
49
    {
50
        $albumsQuery = Album::find()->select([
51
            'id', 'title', 'type'
52
        ]);
53
54
        if (!empty($type)) {
55
            $albumsQuery = $albumsQuery->where(['type' => $type]);
56
        }
57
58
        return $albumsQuery->all();
59
    }
60
}
61