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

TechTipsController::createTip()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 58
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 30
nc 8
nop 1
dl 0
loc 58
ccs 30
cts 30
cp 1
crap 5
rs 9.1288
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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