Passed
Push — 5.0.0 ( dd8bc0...b0cec2 )
by Fèvre
05:00
created

WithSorting::sortBy()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 12
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Traits;
6
7
use Illuminate\Contracts\Database\Query\Builder;
8
9
trait WithSorting
10
{
11
    /**
12
     * Filter the field on component mount regarding the allowed fields.
13
     *
14
     * @return void
15
     */
16
    public function mountWithSorting(): void
17
    {
18
        // Check if the field is allowed before setting it.
19
        if (!in_array($this->sortField, $this->allowedFields)) {
20
            $this->sortField = 'created_at';
0 ignored issues
show
Bug Best Practice introduced by
The property sortField does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
21
        }
22
    }
23
24
    /**
25
     * Determine the direction regarding of the field.
26
     *
27
     * @param string $field The field to sort to.
28
     *
29
     * @return void
30
     */
31
    public function sortBy(string $field): void
32
    {
33
        $this->sortDirection = $this->sortField === $field
0 ignored issues
show
Bug Best Practice introduced by
The property sortDirection does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
            ? $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc'
35
            : 'asc';
36
37
        if (
38
            empty($this->allowedFields) ||
39
            in_array($field, $this->allowedFields) ||
40
            $this->sortField === $field
41
        ) {
42
            $this->sortField = $field;
0 ignored issues
show
Bug Best Practice introduced by
The property sortField does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
        }
44
    }
45
46
    /**
47
     * Apply the orderBy condition with the field and the direction to the query.
48
     *
49
     * @param Builder $query The query to apply the orderBy close.
50
     *
51
     * @return Builder
52
     */
53
    public function applySorting(Builder $query): Builder
54
    {
55
        return $query->orderBy($this->sortField, $this->sortDirection);
56
    }
57
58
    /**
59
     * Filter the field regarding the allowed fields.
60
     *
61
     * @param string $field The new field to check.
62
     *
63
     * @return void
64
     */
65
    public function updatedSortField(string $field): void
66
    {
67
        if (!empty($this->allowedFields) && !in_array($field, $this->allowedFields)) {
68
            $this->sortField = 'created_at';
0 ignored issues
show
Bug Best Practice introduced by
The property sortField does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
69
        }
70
    }
71
}
72