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

GetTechTips::searchTips()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 25
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 43
ccs 25
cts 25
cp 1
crap 4
rs 9.52
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('sticky', 'DESC')
26 2
                    ->orderBy('created_at', 'DESC')
27 2
                    ->with('SystemTypes')
28 2
                    ->paginate($request->pagination['perPage']
29
            ));
30
        }
31
        else
32
        {
33 6
            $article = Arr::has($searchFields, 'articleType');
34 6
            $system  = Arr::has($searchFields, 'systemType');
35
            //  Search paramaters, filter results
36 6
            $tips = new TechTipsCollection(
37 6
                TechTips::orderBy('sticky', 'DESC')->orderBy('created_at', 'DESC')
38
                    //  Search by id or a phrase in the title or description
39
                    ->where(function($query) use ($searchFields) {
40 6
                        $query->where('subject', 'like', '%'.$searchFields['searchText'].'%')
41 6
                            ->orWhere('tip_id', 'like', '%'.$searchFields['searchText'].'%')
42 6
                            ->orWhere('description', 'like', '%'.$searchFields['searchText'].'%');
43 6
                    })
44
                    ->when($article, function($query) use ($searchFields) {
45 2
                        $query->whereIn('tip_type_id', $searchFields['articleType']);
46 6
                    })
47
                    ->when($system, function($query) use ($searchFields) {
48
                        $query->whereHas('SystemTypes', function($query) use ($searchFields) {
49 2
                            $query->whereIn('system_types.sys_id', $searchFields['systemType']);
50 2
                        });
51 6
                    })
52 6
                    ->with('SystemTypes')
53 6
                    ->paginate($request->pagination['perPage'])
54
            );
55
        }
56
57 8
        Log::debug('Tech Tip search query performed.  Results - ', array($tips));
58 8
        return $tips;
59
    }
60
61
    //  Get the details about a tech tip
62 6
    public function getTipDetails($tipID)
63
    {
64 6
        $tipData = TechTips::where('tip_id', $tipID)->with('User')->with('SystemTypes')->first();
65 6
        $files = TechTipFiles::where('tip_id', $tipID)->with('Files')->get();
66
67 6
        return collect(['details' => $tipData, 'files' => $files]);
68
    }
69
}
70