Passed
Push — dev5 ( da16e0...6692a0 )
by Ron
08:20
created

CreateFileLink::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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