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

FileRemoteAccess   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 23
rs 10
c 2
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 3 1
A getHrefAttribute() 0 2 1
A grant() 0 7 3
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
}