Passed
Push — dev5a ( 915c64...c2cba2 )
by Ron
07:58
created

TechTipsController::download()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\TechTips;
4
5
use Illuminate\Http\Request;
6
7
use App\Http\Controllers\Controller;
8
9
use App\Http\Requests\TechTips\NewTipRequest;
10
use App\Http\Requests\TechTips\SearchTipsRequest;
11
use App\Http\Requests\TechTips\UploadImageRequest;
12
13
use App\Domains\TechTips\SearchTips;
14
use App\Domains\TechTips\SetTechTips;
15
use App\Domains\Equipment\GetEquipment;
16
use App\Domains\TechTips\GetTechTips;
17
use App\Domains\TechTips\GetTechTipTypes;
18
use App\Domains\User\GetUserStats;
19
use App\Domains\User\SetUserFavorites;
20
21
use Illuminate\Support\Facades\Auth;
22
23
use PDF;
24
25
class TechTipsController extends Controller
26
{
27
    //  Landing page - tech tip search form
28 2
    public function index()
29
    {
30 2
        return view('tips.index', [
31 2
            'tipTypes'  => (new GetTechTipTypes)->execute(),
32 2
            'equipment' => (new GetEquipment)->getAllEquipment(true),
33
        ]);
34
    }
35
36
    //  Submit a search request
37 2
    public function search(SearchTipsRequest $request)
38
    {
39 2
        return (new SearchTips)->execute($request);
40
    }
41
42
    //  New tech tip form
43 2
    public function create()
44
    {
45 2
        return view('tips.create', [
46 2
            'types' => (new GetTechTipTypes)->execute(),
47 2
            'equip' => (new GetEquipment)->getAllEquipment(true),
48
        ]);
49
    }
50
51
    //  Upload an image file to be included with the tech tip text
52 2
    public function uploadImage(UploadImageRequest $request)
53
    {
54 2
        $local = (new SetTechTips)->processImage($request);
55 2
        return response()->json(['location' => $local]);
56
    }
57
58
    //  Store a new Tech Tip into the database along with any files attached
59 2
    public function store(NewTipRequest $request)
60
    {
61 2
        $tip = (new SetTechTips)->processNewTip($request);
62 2
        return response()->json(['success' => true, 'tip_id' => $tip]);
63
    }
64
65
    //  Show the Tech Tip Details
66 4
    public function show($id)
67
    {
68 4
        $details = (new GetTechTips)->getTip($id);
69
70 4
        if(!$details)
71
        {
72 2
            abort(404, 'The Tech Tip you are looking for does not exist or cannot be found');
73
        }
74
75 2
        $isFav = (new GetUserStats(Auth::user()->user_id))->checkTipForFav($id);
76 2
        return view('tips.details', [
77 2
            'details' => $details,
78 2
            'isFav' => $isFav ? true : false,
79
        ]);
80
    }
81
82
    //  Toggle if a Tech Tip is listed as a users favorite or not
83 4
    public function toggleFav($id)
84
    {
85 4
        $result = (new SetUserFavorites)->toggleTechTipFavorite($id, Auth::user()->user_id);
86 4
        return response()->json(['success' => true, 'favorite' => $result]);
87
    }
88
89
    //  Download a tip as a pdf file
90
    public function download($id)
91
    {
92
        $tipData = (new GetTechTips)->getTip($id);
93
94
        $pdf = PDF::loadView('pdf.techTip', [
95
            'data' => $tipData,
96
        ]);
97
98
        return $pdf->download($tipData->subject.'.pdf');
99
    }
100
101
102
103
104
105
106
107
    /**
108
     * Show the form for editing the specified resource.
109
     *
110
     * @param  int  $id
111
     * @return \Illuminate\Http\Response
112
     */
113
    public function edit($id)
114
    {
115
        //
116
        echo 'just the tip';
117
    }
118
119
    /**
120
     * Update the specified resource in storage.
121
     *
122
     * @param  \Illuminate\Http\Request  $request
123
     * @param  int  $id
124
     * @return \Illuminate\Http\Response
125
     */
126
    public function update(Request $request, $id)
127
    {
128
        //
129
    }
130
131
    /**
132
     * Remove the specified resource from storage.
133
     *
134
     * @param  int  $id
135
     * @return \Illuminate\Http\Response
136
     */
137
    public function destroy($id)
138
    {
139
        //
140
    }
141
}
142