Passed
Push — dev5 ( c2dc0b...cbc2fc )
by Ron
08:19
created

SetTechTips::processTipEquipment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
namespace App\Domains\TechTips;
4
5
use Illuminate\Http\File;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Storage;
9
use Illuminate\Support\Facades\Notification;
10
11
use App\Http\Requests\TechTipNewTipRequest;
12
use App\Http\Requests\TechTipProcessImageRequest;
13
14
use App\Domains\FilesDomain;
15
16
use App\User;
17
use App\Files;
18
use App\TechTips;
19
use App\TechTipFiles;
20
use App\TechTipTypes;
21
use App\TechTipSystems;
22
23
use App\Notifications\NewTechTip;
24
25
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
    //  Create the Tech Tip in the datbase
61 4
    protected function createTip($tipData)
62
    {
63
        //  Remove any forward slash (/) from the Subject Field
64 4
        $tipData->merge(['subject' => str_replace('/', '-', $tipData->subject)]);
65
66
        //  Enter the tip details and return the tip ID
67 4
        $tip = TechTips::create([
68 4
            'tip_type_id' => $tipData->tip_type_id,
69 4
            'subject'     => $tipData->subject,
70 4
            'description' => $tipData->description,
71 4
            'user_id'     => Auth::user()->user_id
72
        ]);
73 4
        $tipID = $tip->tip_id;
74
75 4
        $this->processTipEquipment($tipData->equipment, $tipID);
76 4
        $this->processFiles($tipID);
77 4
        $this->sendNotification($tipData->noEmail, $tipID);
78
79 4
        return $tipID;
80
    }
81
82
    //  Store any equipment types that were attached to the tip
83 4
    protected function processTipEquipment($equipment, $tipID)
84
    {
85 4
        foreach($equipment as $sys)
86
        {
87 4
            TechTipSystems::create([
88 4
                'tip_id' => $tipID,
89 4
                'sys_id' => $sys['sys_id']
90
            ]);
91
        }
92
93 4
        return true;
94
    }
95
96
    //  For all files that were uploaded, move to the proper folder and attach to the tip
97 4
    protected function processFiles($tipID)
98
    {
99 4
        if(session('newTipFile') != null)
100
        {
101 2
            $files = session('newTipFile');
102 2
            $this->path = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID;
103 2
            foreach($files as $file)
104
            {
105
                //  Move the file to the proper folder
106 2
                $data = Files::find($file);
107 2
                Storage::move($data->file_link.$data->file_name, $this->path.DIRECTORY_SEPARATOR.$data->file_name);
108 2
                $data->update([
109 2
                    'file_link' => $this->path.DIRECTORY_SEPARATOR
110
                ]);
111
112
                //  Attach file to Tech Tip
113 2
                TechTipFiles::create([
114 2
                    'tip_id' => $tipID,
115 2
                    'file_id' => $data->file_id
116
                ]);
117
            }
118
        }
119
120 4
        return true;
121
    }
122
123
    //  Send the notifications for the new tip
124 4
    protected function sendNotification($sendEmail, $tipID)
125
    {
126
        //  Send out the notifications
127 4
        if($sendEmail)
128
        {
129
            $details = TechTips::find($tipID);
130
            $users = User::whereHas('UserSettings', function($query) {
131
                $query->where('em_tech_tip', 1);
132
            })->get();
133
134
            Notification::send($users, new NewTechTip($details));
135
        }
136 4
    }
137
}
138