Passed
Push — master ( 2a90e7...a622eb )
by Ron
02:27 queued 14s
created

CustomerSearch::searchCustomerID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
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 12
                        $query->where('name', 'like', '%'.$request->name.'%')
24 12
                            ->orWhere('cust_id', 'like', '%'.$request->name.'%')
25 12
                            ->orWhere('dba_name', 'like', '%'.$request->name.'%');
26 12
                    })
27
                    //  Search the city column
28 12
                    ->where('city', 'like', '%'.$request->city.'%')
29
                    //  Include the customers systems
30 12
                    ->with('CustomerSystems.SystemTypes')
31
                    //  If the system field is present - search for system type
32
                    ->when($request->system, function($query) use ($request) {
33
                        $query->whereHas('CustomerSystems.SystemTypes', function($query) use ($request) {
34 2
                            $query->where('sys_id', $request->system);
35 2
                        });
36 12
                    })
37 12
                    ->paginate($request->perPage)
38
            );
39
        }
40
        else
41
        {
42 4
            $searchResults = new CustomersCollection(Customers::orderBy($request->sortField, $request->sortType)->with('CustomerSystems.SystemTypes')->paginate($request->perPage));
43
        }
44
45 16
        Log::debug('Performing customer search with paramaters - ', array($request));
46 16
        Log::debug('Performed customer search.  Results - ', array($searchResults));
47 16
        return $searchResults;
48
    }
49
50
    //  Search for a customer ID to see if it is valid
51 4
    public function searchCustomerID($id)
52
    {
53 4
        $cust = Customers::find($id);
54
55 4
        Log::debug('Performed Customer search for Customer ID '.$id.'.  Result - ', array($cust));
56 4
        return $cust;
57
    }
58
}
59