Passed
Push — dev5a ( c2cba2...4ce5a4 )
by Ron
07:44
created

TechTipsController::edit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace App\Http\Controllers\TechTips;
4
5
use App\Http\Controllers\Controller;
6
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Support\Facades\Auth;
9
10
use App\Http\Requests\TechTips\NewTipRequest;
11
use App\Http\Requests\TechTips\EditTipRequest;
12
use App\Http\Requests\TechTips\SearchTipsRequest;
13
use App\Http\Requests\TechTips\UploadImageRequest;
14
15
use App\Domains\User\GetUserStats;
16
use App\Domains\TechTips\SearchTips;
17
use App\Domains\TechTips\SetTechTips;
18
use App\Domains\TechTips\GetTechTips;
19
use App\Domains\User\SetUserFavorites;
20
use App\Domains\Equipment\GetEquipment;
21
use App\Domains\TechTips\GetTechTipTypes;
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, Auth::user()->user_id);
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
    //  Edit an existing Tech Tip
102 4
    public function edit($id)
103
    {
104 4
        $details = (new GetTechTips)->getTip($id);
105
106 4
        if(!$details)
107
        {
108 2
            abort(404, 'The Tech Tip you are looking for does not exist or cannot be found');
109
        }
110
111 2
        return view('tips.edit', [
112 2
            'types'   => (new GetTechTipTypes)->execute(),
113 2
            'equip'   => (new GetEquipment)->getAllEquipment(true),
114 2
            'details' => $details,
115
        ]);
116
    }
117
118
    //  Submit the edit tip form
119 2
    public function update(EditTipRequest $request, $id)
120
    {
121 2
        (new SetTechTips)->processEditTip($request, $id, Auth::user()->user_Id);
0 ignored issues
show
Bug introduced by
The property user_Id does not exist on App\User. Did you mean user_id?
Loading history...
122 2
        return response()->json(['success' => true]);
123
    }
124
125
    /**
126
     * Remove the specified resource from storage.
127
     *
128
     * @param  int  $id
129
     * @return \Illuminate\Http\Response
130
     */
131 2
    public function destroy($id)
132
    {
133 2
        (new SetTechTips)->deactivateTip($id);
134 2
        Log::notice('User '.Auth::user()->full_name.' has deleted Tech Tip ID '.$id);
135 2
        return response()->json(['success' => true]);
136
    }
137
}
138