Passed
Push — master ( 9aa048...898b5e )
by Mattia
04:05
created

AccountRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 18
dl 0
loc 80
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findByUsername() 0 5 1
A filterQuery() 0 8 2
A findByUuid() 0 5 1
A findLastUpdatedByUsername() 0 7 1
A filterPaginate() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Minepic\Repositories;
6
7
use Minepic\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