Completed
Branch proxy (a8ed88)
by leo
08:29
created

Ticket::service()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: chenyihong
5
 * Date: 16/8/1
6
 * Time: 14:53
7
 */
8
9
namespace Leo108\CAS\Models;
10
11
use Carbon\Carbon;
12
use Illuminate\Database\Eloquent\Model;
13
use Leo108\CAS\Contracts\Models\UserModel;
14
15
/**
16
 * Class Ticket
17
 * @package Leo108\CAS\Models
18
 *
19
 * @property integer   $id
20
 * @property string    $ticket
21
 * @property string    $service_url
22
 * @property integer   $service_id
23
 * @property integer   $user_id
24
 * @property array     $proxies
25
 * @property integer   $created_at
26
 * @property integer   $expire_at
27
 * @property UserModel $user
28
 */
29
class Ticket extends Model
30
{
31
    protected $table = 'cas_tickets';
32
    public $timestamps = false;
33
    protected $fillable = ['ticket', 'service_url', 'proxies', 'expire_at', 'created_at'];
34
35
    public function getProxiesAttribute()
36
    {
37
        if (!$this->isProxy()) {
38
            return null;
39
        }
40
41
        return json_decode($this->attributes['proxies'], true);
42
    }
43
44
    public function setProxiesAttribute($value)
45
    {
46
        if ($this->id && !$this->isProxy()) {
47
            return;
48
        }
49
        $this->attributes['proxies'] = json_encode($value);
50
    }
51
52
    public function isExpired()
53
    {
54
        return (new Carbon($this->expire_at))->getTimestamp() < time();
55
    }
56
57
    public function service()
58
    {
59
        return $this->belongsTo(Service::class);
60
    }
61
62
    public function user()
63
    {
64
        return $this->belongsTo(config('cas.user_table.model'), 'user_id', config('cas.user_table.id'));
65
    }
66
67
    public function isProxy()
68
    {
69
        return !is_null($this->attributes['proxies']);
70
    }
71
}
72