Passed
Push — master ( 6f7c46...5194c3 )
by Mattia
03:37
created

AccountRepository::findByUuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Minepic\Repositories;
6
7
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8
use Illuminate\Database\Eloquent\Builder;
9
use Minepic\Models\Account;
10
11
class AccountRepository
12
{
13
    /**
14
     * @param array $filters
15
     *
16
     * @return Builder
17
     */
18
    public function filterQuery(array $filters = []): Builder
19
    {
20
        $query = Account::query();
21
        if (\array_key_exists('term', $filters)) {
22
            $query->where('username', 'LIKE', '%'.$filters['term'].'%');
23
        }
24
25
        return $query;
26
    }
27
28
    /**
29
     * Last updated username.
30
     *
31
     * @param string $uuid
32
     * @param array  $columns
33
     *
34
     * @return Builder|\Illuminate\Database\Eloquent\Model|object|null
35
     */
36
    public function findLastUpdatedByUsername(string $uuid, $columns = ['*'])
37
    {
38
        return Account::query()
39
            ->select($columns)
40
            ->whereUsername($uuid)
41
            ->orderBy('updated_at', 'desc')
42
            ->first();
43
    }
44
45
    /**
46
     * @param array    $filters
47
     * @param int|null $perPage
48
     * @param array    $columns
49
     * @param string   $pageName
50
     * @param int|null $page
51
     *
52
     * @return LengthAwarePaginator
53
     */
54
    public function filterPaginate(
55
        array $filters,
56
        $perPage = null,
57
        $columns = ['*'],
58
        $pageName = 'page',
59
        $page = null
60
    ): LengthAwarePaginator {
61
        return $this->filterQuery($filters)
62
            ->paginate($perPage, $columns, $pageName, $page);
63
    }
64
}
65