EloquentSortingHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 72
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 17 2
A checkForTrait() 0 6 2
A __construct() 0 8 1
A handleSort() 0 4 1
1
<?php
2
3
namespace musa11971\SortRequest;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Http\Request;
7
use musa11971\SortRequest\Exceptions\EloquentSortingException;
8
9
class EloquentSortingHandler {
10
    /** @var Request $request */
11
    private $request;
12
13
    /** @var Builder $builder */
14
    private $builder;
15
16
    /** @var array $rules */
17
    private $rules;
18
19
    /** @var SortableColumnCollection $sortableColumns */
20
    private $sortableColumns;
21
22
    public function __construct($request, $builder)
23
    {
24
        $this->request = $request;
25
        $this->builder = $builder;
26
27
        // Get the rules and sortable columns from the request
28
        $this->rules = $this->request->validatedSortingRules();
29
        $this->sortableColumns = $this->request->transformedSortableColumns();
30
    }
31
32
    /**
33
     * Handles the actual Eloquent sorting and returns the builder.
34
     *
35
     * @return Builder
36
     * @throws EloquentSortingException
37
     */
38
    function handle()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
    {
40
        // Check whether the request uses the trait before continuing
41
        $this->checkForTrait();
42
43
        // Loop through every rule and handle it individually
44
        foreach($this->rules as $rule)
45
        {
46
            // Find the relevant sortable column for this rule
47
            $sortableColumn = $this->sortableColumns->find($rule['column']);
48
49
            // Handle the sorting actions for the column
50
            $this->handleSort($sortableColumn, $rule, $this->builder);
51
        }
52
53
        // Pass back the builder so that it can be chained
54
        return $this->builder;
55
    }
56
57
    /**
58
     * Handle the sorting of a column.
59
     *
60
     * @param SortableColumn $sortableColumn
61
     * @param array $rule
62
     * @param Builder $builder
63
     */
64
    private function handleSort($sortableColumn, $rule, &$builder)
65
    {
66
        // Call the sorter's apply method
67
        $builder = $sortableColumn->sorter->apply(request(), $builder, $rule['direction']);
68
    }
69
70
    /**
71
     * Checks whether the request uses the trait.
72
     *
73
     * @throws EloquentSortingException
74
     */
75
    private function checkForTrait()
76
    {
77
        if(!method_exists($this->request, 'validatedSortingRules')) {
78
            $requestClass = get_class($this->request);
79
80
            throw new EloquentSortingException("{$requestClass} is not using the SortsViaRequest trait.");
81
        }
82
    }
83
}