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

CreateFileLink::createLink()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 14
ccs 11
cts 11
cp 1
crap 3
rs 9.9332
1
<?php
2
3
namespace App\Domains\FileLinks;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Support\Facades\Auth;
8
9
use App\FileLinks;
10
11
use App\Domains\FileLinks\SaveFileLinkFile;
12
13
use App\Http\Requests\NewFileLinkRequest;
14
15
class CreateFileLink extends SaveFileLinkFile
16
{
17
    //  Create a brand new file link and determine if any files should be linked to it
18 8
    public function create(NewFileLinkRequest $request)
19
    {
20 8
        $linkID = $this->createLink($request);
21
22 8
        if($request->session()->has('newLinkFile'))
23
        {
24 2
            $this->relocateFiles($linkID);
25
        }
26
27 8
        return ['link' => $linkID, 'name' => Str::slug($request->name)];
28
    }
29
30
    //  Build a new link data in the database
31 8
    protected function createLink($data)
32
    {
33 8
        $link = FileLinks::create([
34 8
            'user_id'      => Auth::user()->user_id,
35 8
            'cust_id'      => $data->customerID,
36 8
            'link_hash'    => $this->generateHash(),
37 8
            'link_name'    => $data->name,
38 8
            'expire'       => $data->expire,
39 8
            'allow_upload' => isset($data->allowUp) && $data->allowUp ? true : false,
40 8
            'note'         => $data->instructions
41
        ]);
42
43 8
        Log::info('User '.Auth::user()->full_name.' created new file link.  Data - ', array($link));
44 8
        return $link->link_id;
45
    }
46
47
    //  Generate a random hash to use as the link.  Verify it is not already in use
48 8
    protected function generateHash()
49
    {
50
        do
51
        {
52 8
            $hash = strtolower(Str::random(15));
53 8
            $dup  = FileLinks::where('link_hash', $hash)->get()->count();
54 8
            Log::debug('New hash created - '.$hash.'.  Checking for duplicate.  Result - '.$dup);
55 8
        } while($dup != 0);
56
57 8
        return $hash;
58
    }
59
}
60