Test Setup Failed
Push — dev6 ( 81193f...01d104 )
by Ron
22:34
created

CustomerSearchController::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 30
rs 9.7666
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Models\Customer;
6
use App\Http\Controllers\Controller;
7
use App\Http\Requests\Customers\CustomerSearchRequest;
8
9
use Illuminate\Support\Arr;
10
11
class CustomerSearchController extends Controller
12
{
13
    protected $perPage;
14
    protected $sortType;
15
    protected $sortField;
16
17
    /**
18
     *  Search for a customer
19
     */
20
    public function __invoke(CustomerSearchRequest $request)
21
    {
22
        //  Pagination parameters
23
        $this->perPage   = $request->perPage;
24
        $this->sortField = $request->sortField;
25
        $this->sortType  = $request->sortType == 'none' ? 'asc' : $request->sortType;
26
27
        //  If no search parameters are set, list all customers
28
        if(Arr::has($request->toArray(), ['city', 'name', 'equipment']) &&
29
            ($request->city == null &&
30
             $request->name == null &&
31
             $request->equipment == null)
32
            )
33
        {
34
            return $this->getAllCustomers();
35
        }
36
37
        //  Search by given search parameters
38
        return $this->search($request->only(['city', 'name', 'equipment']));
39
    }
40
41
    /*
42
    *   Perform the actual search
43
    */
44
    protected function search($params)
45
    {
46
        return Customer::orderBy($this->sortField, $this->sortType)
47
            //  Search by equipment
48
            ->when(isset($params['equipment']), function($q) use ($params)
49
            {
50
                $q->whereHas('ParentEquipment', function($q2) use ($params)
51
                {
52
                    $q2->where('name', $params['equipment']);
53
                })
54
                ->orWhereHas('EquipmentType', function($q2) use ($params)
55
                {
56
                    $q2->where('name', $params['equipment']);
57
                });
58
            })
59
            //  Search by City
60
            ->when(isset($params['city']), function($q) use ($params)
61
            {
62
                $q->where('city', 'like', '%'.$params['city'].'%');
63
            })
64
            //  Search by Name or ID
65
            ->when(isset($params['name']), function($q) use ($params)
66
            {
67
                $q->where('name', 'like', '%'.$params['name'].'%')
68
                    ->orWhere('cust_id', 'like', '%'.$params['name'].'%')
69
                    ->orWhere('dba_name', 'like', '%'.$params['name'].'%');
70
            })
71
            ->with('CustomerEquipment')
72
            ->with('ParentEquipment')
73
            ->paginate($this->perPage);
74
    }
75
76
    /*
77
    *   Return pagination list of all customers
78
    */
79
    protected function getAllCustomers()
80
    {
81
        return Customer::orderBy($this->sortField, $this->sortType)
82
            ->with('CustomerEquipment')
83
            ->with('ParentEquipment')
84
            ->paginate($this->perPage);
85
    }
86
}
87