Passed
Push — dev5 ( ecdb11...2a90e7 )
by Ron
02:51 queued 12s
created

SetTechTips::updateTipDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Domains\TechTips;
4
5
use Illuminate\Http\File;
6
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Storage;
10
use Illuminate\Support\Facades\Notification;
11
12
use App\Http\Requests\TechTipNewTipRequest;
13
use App\Http\Requests\TechTipProcessImageRequest;
14
15
use App\Domains\FilesDomain;
16
17
use App\User;
18
use App\Files;
19
use App\Http\Requests\TechTipEditTipRequest;
20
use App\TechTips;
21
use App\TechTipFiles;
22
use App\TechTipSystems;
23
use App\TechTipFavs;
24
25
use App\Notifications\NewTechTip;
26
27
class SetTechTips extends FilesDomain
28
{
29
    //  Store an image that has been uploaded to assign to a Tech Tip
30 2
    public function processTipImage(TechTipProcessImageRequest $request)
31
    {
32
        //  Generate a unique hash as the file name and store it in a publicly accessable folder
33 2
        $path = 'img/tip_img';
34 2
        $location = Storage::disk('public')->putFile($path, new File($request->file));
35 2
        Log::info('Image uploaded for tech tip.  Image Data - ', array($request));
36
37
        //  Return the full url path to the image
38 2
        return Storage::url($location);
39
    }
40
41
    //  Process the new tip form to see if a file is being uploaded, or upload is completed
42 4
    public function processNewTip(TechTipNewTipRequest $request)
43
    {
44 4
        if(isset($request->file))
45
        {
46 2
            $fileID = $this->processFileChunk($request);
47 2
            if($fileID)
48
            {
49 2
                $fileArr = session('newTipFile') != null ? session('newTipFile') : [];
50 2
                $fileArr[] = $fileID;
51 2
                session(['newTipFile' => $fileArr]);
52
            }
53
54 2
            return false;
55
        }
56
57 4
        return $this->createTip($request);
58
    }
59
60
    //  Update an existin gTech Tip
61 4
    public function processEditTip(TechTipEditTipRequest $request, $tipID)
62
    {
63 4
        if(isset($request->file))
64
        {
65 2
            $this->path = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID;
66 2
            $fileID = $this->processFileChunk($request);
67 2
            if($fileID)
68
            {
69
                //  Attach file to Tech Tip
70 2
                TechTipFiles::create([
71 2
                    'tip_id' => $tipID,
72 2
                    'file_id' => $fileID,
73
                ]);
74
            }
75
76 2
            return false;
77
        }
78
79 2
        $this->updateTipDetails($request);
80 2
        $this->updateTipEquipment($request->system_types, $tipID);
81 2
        if(isset($request->deletedFileList))
82
        {
83
            $this->updateTipFiles($request->deletedFileList);
84
        }
85
86 2
        return true;
87
    }
88
89
    //  Delete the Tech Tip
90 2
    public function deleteTip($tipID)
91
    {
92 2
        TechTipFavs::where('tip_id', $tipID)->delete();
93
94
        //  Disable the tip
95 2
        TechTips::find($tipID)->delete();
96 2
        Log::warning('User - '.Auth::user()->full_name.' deleted Tech Tip ID - '.$tipID);
97
98 2
        return true;
99
    }
100
101
    //  Create the Tech Tip in the datbase
102 4
    protected function createTip($tipData)
103
    {
104
        //  Remove any forward slash (/) from the Subject Field
105 4
        $tipData->merge(['subject' => str_replace('/', '-', $tipData->subject)]);
106
107
        //  Enter the tip details and return the tip ID
108 4
        $tip = TechTips::create([
109 4
            'tip_type_id' => $tipData->tip_type_id,
110 4
            'subject'     => $tipData->subject,
111 4
            'description' => $tipData->description,
112 4
            'user_id'     => Auth::user()->user_id
113
        ]);
114 4
        $tipID = $tip->tip_id;
115
116 4
        $this->processTipEquipment($tipData->equipment, $tipID);
117 4
        $this->processFiles($tipID);
118 4
        $this->sendNotification($tipData->noEmail, $tipID);
119
120 4
        return $tipID;
121
    }
122
123
    //  Update the tip details
124 2
    protected function updateTipDetails($data)
125
    {
126
        //  Update the base information
127 2
        TechTips::find($data->tip_id)->update([
128 2
            'tip_type_id' => $data->tip_type_id,
129 2
            'subject'     => $data->subject,
130 2
            'description' => $data->description,
131
        ]);
132
133 2
        return true;
134
    }
135
136
    //  Add or remove any system types attached to the tip
137 2
    protected function updateTipEquipment($equipTypes, $tipID)
138
    {
139 2
        $current = TechTipSystems::where('tip_id', $tipID)->get();
140 2
        $newEquip = [];
141
142 2
        foreach($equipTypes as $equip)
143
        {
144 2
            if(isset($equip['laravel_through_key']))
145
            {
146
                $current = $current->filter(function($item) use ($equip)
147
                {
148
                    return $item->sys_id != $equip['sys_id'];
149
                });
150
            }
151
            else
152
            {
153 2
                $newEquip[] = $equip;
154
            }
155
        }
156
157 2
        $this->processTipEquipment($newEquip, $tipID);
158
159 2
        foreach($current as $cur)
160
        {
161 2
            TechTipSystems::find($cur->tip_tag_id)->delete();
162
        }
163
164 2
        return true;
165
    }
166
167
    //  Remove any files that were deleted from the tip
168
    protected function updateTipFiles($deletedList)
169
    {
170
        foreach($deletedList as $delFile)
171
        {
172
            $details = TechTipFiles::find($delFile);
173
            $fileID = $details->file_id;
174
            $details->delete();
175
176
            $this->deleteFile($fileID);
177
        }
178
    }
179
180
    //  Store any equipment types that were attached to the tip
181 6
    protected function processTipEquipment($equipment, $tipID)
182
    {
183 6
        foreach($equipment as $sys)
184
        {
185 6
            TechTipSystems::create([
186 6
                'tip_id' => $tipID,
187 6
                'sys_id' => $sys['sys_id']
188
            ]);
189
        }
190
191 6
        return true;
192
    }
193
194
    //  For all files that were uploaded, move to the proper folder and attach to the tip
195 4
    protected function processFiles($tipID)
196
    {
197 4
        if(session('newTipFile') != null)
198
        {
199 2
            $files = session('newTipFile');
200 2
            $this->path = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID;
201 2
            foreach($files as $file)
202
            {
203
                //  Move the file to the proper folder
204 2
                $data = Files::find($file);
205 2
                Storage::move($data->file_link.$data->file_name, $this->path.DIRECTORY_SEPARATOR.$data->file_name);
206 2
                $data->update([
207 2
                    'file_link' => $this->path.DIRECTORY_SEPARATOR
208
                ]);
209
210
                //  Attach file to Tech Tip
211 2
                TechTipFiles::create([
212 2
                    'tip_id' => $tipID,
213 2
                    'file_id' => $data->file_id
214
                ]);
215
            }
216
217 2
            session()->forget('newTipFile');
218
        }
219
220 4
        return true;
221
    }
222
223
    //  Send the notifications for the new tip
224 4
    protected function sendNotification($sendEmail, $tipID)
225
    {
226
        //  Send out the notifications
227 4
        if($sendEmail)
228
        {
229
            $details = TechTips::find($tipID);
230
            $users = User::whereHas('UserSettings', function($query) {
231
                $query->where('em_tech_tip', 1);
232
            })->get();
233
234
            Notification::send($users, new NewTechTip($details));
235
        }
236 4
    }
237
}
238