Passed
Push — master ( 1d6d9a...2a90e7 )
by Ron
02:47 queued 12s
created

GetTechTips::searchTips()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 24
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 42
ccs 24
cts 24
cp 1
crap 4
rs 9.536
1
<?php
2
3
namespace App\Domains\TechTips;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Facades\Log;
7
8
use App\TechTips;
9
use App\TechTipFiles;
10
11
use App\Http\Resources\TechTipsCollection;
12
use App\Http\Requests\TechTipSearchRequest;
13
14
class GetTechTips
15
{
16 8
    public function searchTips(TechTipSearchRequest $request)
17
    {
18 8
        $searchFields = $request->search;
19
20
        //  See if there are any search paramaters entered
21 8
        if($searchFields['searchText'] == null && !Arr::has($searchFields, 'articleType') && !Arr::has($searchFields, 'systemType'))
22
        {
23
            //  No search paramaters, send all tech tips
24 2
            $tips = new TechTipsCollection(
25 2
                TechTips::orderBy('created_at', 'DESC')
26 2
                    ->with('SystemTypes')
27 2
                    ->paginate($request->pagination['perPage']
28
            ));
29
        }
30
        else
31
        {
32 6
            $article = Arr::has($searchFields, 'articleType');
33 6
            $system  = Arr::has($searchFields, 'systemType');
34
            //  Search paramaters, filter results
35 6
            $tips = new TechTipsCollection(
36 6
                TechTips::orderBy('created_at', 'DESC')
37
                    //  Search by id or a phrase in the title or description
38
                    ->where(function($query) use ($searchFields) {
39 6
                        $query->where('subject', 'like', '%'.$searchFields['searchText'].'%')
40 6
                            ->orWhere('tip_id', 'like', '%'.$searchFields['searchText'].'%')
41 6
                            ->orWhere('description', 'like', '%'.$searchFields['searchText'].'%');
42 6
                    })
43
                    ->when($article, function($query) use ($searchFields) {
44 2
                        $query->whereIn('tip_type_id', $searchFields['articleType']);
45 6
                    })
46
                    ->when($system, function($query) use ($searchFields) {
47
                        $query->whereHas('SystemTypes', function($query) use ($searchFields) {
48 2
                            $query->whereIn('system_types.sys_id', $searchFields['systemType']);
49 2
                        });
50 6
                    })
51 6
                    ->with('SystemTypes')
52 6
                    ->paginate($request->pagination['perPage'])
53
            );
54
        }
55
56 8
        Log::debug('Tech Tip search query performed.  Results - ', array($tips));
57 8
        return $tips;
58
    }
59
60
    //  Get the details about a tech tip
61 6
    public function getTipDetails($tipID)
62
    {
63 6
        $tipData = TechTips::where('tip_id', $tipID)->with('User')->with('SystemTypes')->first();
64 6
        $files = TechTipFiles::where('tip_id', $tipID)->with('Files')->get();
65
66 6
        return collect(['details' => $tipData, 'files' => $files]);
67
    }
68
}
69