Passed
Push — master ( 60c4f2...db8d73 )
by Georgi
02:28
created

FileRemoteAccess::getHrefAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Epesi\FileStorage\Database\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\Auth;
7
8
class FileRemoteAccess extends Model {
9
    const DEFAULT_PERIOD = '1 week';
10
    
11
    protected $table = 'filestorage_remote_access';
12
    protected static $unguarded = true;    
13
14
    public static function check($fileId, $token)
15
    {
16
    	return (bool) self::where('file_id', $fileId)->where('token', $token)->where('expires_at', '>', date('Y-m-d H:i:s'))->count();
17
    }
18
    
19
    public static function grant($file, $expires = self::DEFAULT_PERIOD)
20
    {
21
		return self::create([
22
				'file_id' => is_numeric($file)? $file: $file->id,
23
				'token' => md5(uniqid(rand(), true)),
24
				'created_by' => Auth::id()?: 0,
25
				'expires_at' => date('Y-m-d H:i:s', strtotime($expires)),
26
		]);
27
    }
28
    
29
    public function getHrefAttribute()
30
    {		return url('file') . '?' . http_build_query(['id' => $this->file_id, 'token' => $this->token]);
31
    }
32
}