Passed
Push — dev5 ( f54fc3...b5930c )
by Ron
09:26
created

TechTipsController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Http\Controllers\TechTips;
4
5
use App\Http\Controllers\Controller;
6
7
use App\Domains\Users\UserFavs;
8
use App\Domains\Users\GetUserStats;
9
use App\Domains\TechTips\SetTechTips;
10
use App\Domains\TechTips\GetTechTips;
11
use App\Domains\TechTips\GetTechTipTypes;
12
use App\Domains\Equipment\GetEquipmentData;
13
14
use App\Http\Requests\TechTipNewTipRequest;
15
use App\Http\Requests\TechTipSearchRequest;
16
use App\Http\Requests\TechTipEditTipRequest;
17
use App\Http\Requests\TechTipProcessImageRequest;
18
19
class TechTipsController extends Controller
20
{
21 78
    public function __construct()
22
    {
23 78
        $this->middleware('auth');
24 78
    }
25
26
    //  Tech Tips landing page
27 2
    public function index()
28
    {
29 2
        return view('tips.index', [
30 2
            'tipTypes' => (new GetTechTipTypes)->execute(true),
31 2
            'sysTypes' => (new GetEquipmentData)->getAllEquipmentWithDataList(),
32
        ]);
33
    }
34
35
    //  Search for an existing tip - If no paramaters, return all tips
36 8
    public function search(TechTipSearchRequest $request)
37
    {
38 8
        return (new GetTechTips)->searchTips($request);
39
    }
40
41
    //  Process an image that is attached to a tech tip
42 4
    public function processImage(TechTipProcessImageRequest $request)
43
    {
44 4
        $this->authorize('hasAccess', 'Create Tech Tip');
45
46 2
        $imgLocation = (new SetTechTips)->processTipImage($request);
47 2
        return response()->json(['location' => $imgLocation]);
48
49
    }
50
51
    //  Create a new Tech Tip form
52 4
    public function create()
53
    {
54 4
        $this->authorize('hasAccess', 'Create Tech Tip');
55
56 2
        return view('tips.create', [
57 2
            'tipTypes' => (new GetTechTipTypes)->execute(true),
58 2
            'sysTypes' => (new GetEquipmentData)->getAllEquipmentWithDataList(),
59
        ]);
60
    }
61
62
    //  Submit the form to create a new tech tip
63 6
    public function store(TechTipNewTipRequest $request)
64
    {
65 6
        $this->authorize('hasAccess', 'Create Tech Tip');
66
67 4
        $tipData = (new SetTechTips)->processNewTip($request);
68 4
        return response()->json(['success' => $tipData]);
69
    }
70
71
    //  Details controller - will move to the show controller with just the tech tip id
72 2
    public function details($id)
73
    {
74 2
        $tipData = (new GetTechTips)->getTipDetails($id);
75
76 2
        return view('tips.details', [
77 2
            'details' => $tipData->toJson(),
78 2
            'isFav'   => (new GetUserStats)->checkForTechTipFav($id) ? 'true' : 'false',   // empty($isFav) ? 'false' : 'true',
79
        ]);
80
81
    }
82
83
    //  Show the details about the tech tip
84
    public function show($id)
85
    {
86
        $tipData = (new GetTechTips)->getTipDetails($id);
87
88
        return view('tips.details', [
89
            'details' => $tipData['data'],
90
            'isFav'   => (new GetUserStats)->checkForTechTipFav($id) ? 'true' : 'false',   // empty($isFav) ? 'false' : 'true',
91
            'files'   => $tipData['files'],
92
        ]);
93
    }
94
95
    //  Add or remove this tip as a favorite of the user
96
    public function toggleFav($action, $id)
97
    {
98
        (new UserFavs)->updateTechTipFav($id);
99
        return response()->json(['success' => true]);
100
    }
101
102
    //  Edit an existing tech tip
103 6
    public function edit($id)
104
    {
105 6
        $this->authorize('hasAccess', 'Edit Tech Tip');
106
107 4
        $tipData = (new GetTechTips)->getTipDetails($id);
108 4
        if(!$tipData['details'])
109
        {
110 2
            return view('tips.tipNotFound');
111
        }
112
113 2
        return view('tips.editTip', [
114 2
            'tipTypes' => (new GetTechTipTypes)->execute(true),
115 2
            'sysTypes' => (new GetEquipmentData)->getAllEquipmentWithDataList(),
116 2
            'tipData'  => $tipData,
117
        ]);
118
    }
119
120
    //  Store the edited Tech Tip
121 6
    public function update(TechTipEditTipRequest $request, $id)
122
    {
123 6
        $this->authorize('hasAccess', 'Edit Tech Tip');
124
125 4
        (new SetTechTips)->processEditTip($request, $id);
126
127 4
        return response()->json(['success' => true]);
128
    }
129
130
    //  Soft delet the Tech Tip
131 4
    public function destroy($id)
132
    {
133 4
        $this->authorize('hasAccess', 'Delete Tech Tip');
134
135 2
        (new SetTechTips)->deleteTip($id);
136 2
        return response()->json(['success' => true]);
137
    }
138
}
139