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

GetFileLinkDetails::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace App\Domains\FileLinks;
4
5
use Illuminate\Support\Facades\Log;
6
7
use App\FileLinks;
8
use App\FileLinkFiles;
9
10
use App\Http\Resources\FileLinks as FileLinksResource;
11
12
class GetFileLinkDetails
13
{
14
    protected $linkID, $linkDetails;
15
16
    //  Constructor will set the ID of the link if it is available
17 38
    public function __construct($linkID = null)
18
    {
19 38
        $this->linkID = $linkID;
20 38
        if($this->linkID)
21
        {
22 12
            $this->linkDetails = FileLinks::find($this->linkID);
23
        }
24 38
    }
25
26
    //  Return the details of the file link
27 4
    public function execute($collection = false)
28
    {
29 4
        if($collection)
30
        {
31 2
            return new FileLinksResource($this->linkDetails);
32
        }
33
34 2
        return $this->linkDetails;
35
    }
36
37
    //  Determines if the link is actually valid or not
38 4
    public function isLinkValid()
39
    {
40 4
        return $this->linkDetails ? true : false;
41
    }
42
43
    //  Determines if there is a customer attached to the link
44 6
    public function getLinkCustomer()
45
    {
46 6
        return $this->linkDetails->cust_id;
47
    }
48
49
    //  Retrieves the instructions assigned to the link
50 8
    public function getLinkInstructions()
51
    {
52 8
        return $this->linkDetails->note;
53
    }
54
55
    //  Retrieves the ID of the link based on the URL hash provided
56 26
    public function getLinkID($hash)
57
    {
58 26
        $data = FileLinks::where('link_hash', $hash)->first();
59
60 26
        if($data)
61
        {
62 24
            $this->linkID = $data->link_id;
63 24
            $this->linkDetails = $data;
64 24
            return $this->linkID;
65
        }
66
67 2
        return false;
68
    }
69
70
    //  Determine if the link has expired or not
71 10
    public function isLinkExpired()
72
    {
73 10
        return $this->linkDetails->expire <= date('Y-m-d');
74
    }
75
76
    //  Determine if the link has files availabe for guest to download
77 8
    public function hasGuestFiles()
78
    {
79 8
        $files = FileLinkFiles::where('link_id', $this->linkID)->where('upload', false)->count();
80 8
        return $files > 0 ? true : false;
81
    }
82
83
    //  Determine if the guest can upload a file or not
84 8
    public function canGuestUpload()
85
    {
86 8
        return $this->linkDetails->allow_upload === 'Yes' ? true : false;
87
    }
88
89
    //  Determine if the link has no files to download, and the guest cannot upload files
90 8
    public function isLinkDead()
91
    {
92 8
        if(!$this->hasGuestFiles() && !$this->canGuestUpload())
93
        {
94 2
            return true;
95
        }
96
97 6
        return false;
98
    }
99
}
100