Passed
Push — master ( f76f45...88719b )
by Mattia
04:38
created

AccountRepository::model()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Repositories;
6
7
use App\Models\Account;
8
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
9
use Illuminate\Database\Eloquent\Builder;
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
     * Find account using UUID.
30
     *
31
     * @param string $uuid
32
     * @param array  $columns
33
     *
34
     * @return Account
35
     */
36
    public function findByUuid(string $uuid, $columns = ['*']): ?Account
37
    {
38
        return Account::whereUuid($uuid)
39
            ->select($columns)
40
            ->first();
41
    }
42
43
    /**
44
     * @param string $username
45
     * @param array  $columns
46
     *
47
     * @return Account
48
     */
49
    public function findByUsername(string $username, $columns = ['*']): ?Account
50
    {
51
        return Account::whereUsername($username)
52
            ->select($columns)
53
            ->first();
54
    }
55
56
    /**
57
     * Last updated username.
58
     *
59
     * @param string $uuid
60
     * @param array  $columns
61
     *
62
     * @return Builder|\Illuminate\Database\Eloquent\Model|object|null
63
     */
64
    public function findLastUpdatedByUsername(string $uuid, $columns = ['*'])
65
    {
66
        return Account::query()
67
            ->select($columns)
68
            ->whereUsername($uuid)
69
            ->orderBy('updated_at', 'desc')
70
            ->first();
71
    }
72
73
    /**
74
     * @param array    $filters
75
     * @param int|null $perPage
76
     * @param array    $columns
77
     * @param string   $pageName
78
     * @param int|null $page
79
     *
80
     * @return LengthAwarePaginator
81
     */
82
    public function filterPaginate(
83
        array $filters,
84
        $perPage = null,
85
        $columns = ['*'],
86
        $pageName = 'page',
87
        $page = null
88
    ): LengthAwarePaginator {
89
        return $this->filterQuery($filters)
90
            ->paginate($perPage, $columns, $pageName, $page);
91
    }
92
}
93