Passed
Push — dev5 ( c3531a...a24f56 )
by Ron
08:14
created

CustomerSearch::searchCustomer()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 36
ccs 18
cts 18
cp 1
crap 4
rs 9.6666
1
<?php
2
3
namespace App\Domains\Customers;
4
5
use Illuminate\Support\Facades\Log;
6
use Illuminate\Support\Facades\Auth;
7
8
use App\Customers;
9
use App\Http\Requests\CustomerSearchRequest;
10
use App\Http\Resources\CustomersCollection;
11
12
class CustomerSearch
13
{
14
    //  Search for a customer based on all possible paramaters that can be searched for
15 16
    public function searchCustomer(CustomerSearchRequest $request)
16
    {
17 16
        if(isset($request->name) || isset($request->city) || isset($request->system))
18
        {
19 12
            $searchResults = new CustomersCollection(
20 12
                Customers::orderBy($request->sortField, $request->sortType)
21
                    //  Search the name, dba name, and cust id columns
22
                    ->where(function($query) use ($request)
23
                    {
24 12
                        $query->where('name', 'like', '%'.$request->name.'%')
25 12
                            ->orWhere('cust_id', 'like', '%'.$request->name.'%')
26 12
                            ->orWhere('dba_name', 'like', '%'.$request->name.'%');
27 12
                    })
28
                    //  Search the city column
29 12
                    ->where('city', 'like', '%'.$request->city.'%')
30
                    //  Include the customers systems
31 12
                    ->with('CustomerSystems.SystemTypes')
32
                    //  If the system field is present - search for system type
33
                    ->when($request->system, function($query) use ($request)
34
                    {
35
                        $query->whereHas('CustomerSystems.SystemTypes', function($query) use ($request)
36
                        {
37 2
                            $query->where('sys_id', $request->system);
38 2
                        });
39 12
                    })
40 12
                    ->paginate($request->perPage)
41
            );
42
        }
43
        else
44
        {
45 4
            $searchResults = new CustomersCollection(Customers::orderBy($request->sortField, $request->sortType)->with('CustomerSystems.SystemTypes')->paginate($request->perPage));
46
        }
47
48 16
        Log::debug('Performing customer search with paramaters - ', array($request));
49 16
        Log::debug('Performed customer search.  Results - ', array($searchResults));
50 16
        return $searchResults;
51
    }
52
53
    //  Search for a customer ID to see if it is valid
54 4
    public function searchCustomerID($id)
55
    {
56 4
        $cust = Customers::find($id);
57
58 4
        Log::debug('Performed Customer search for Customer ID '.$id.'.  Result - ', array($cust));
59 4
        return $cust;
60
    }
61
}
62