1 | <?php |
||
2 | |||
3 | namespace Linkeys\UrlSigner\Models; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | use Illuminate\Database\Eloquent\Model; |
||
7 | use Linkeys\UrlSigner\Contracts\Models\Group as GroupModelContract; |
||
8 | use Linkeys\UrlSigner\Support\ExpiryNormaliser\NormaliserManagerContract; |
||
9 | |||
10 | class Group extends Model implements GroupModelContract |
||
11 | { |
||
12 | |||
13 | public $fillable = [ |
||
14 | 'expiry', |
||
15 | 'click_limit' |
||
16 | ]; |
||
17 | |||
18 | protected $dates = [ |
||
19 | 'expiry' |
||
20 | ]; |
||
21 | |||
22 | 33 | public function __construct(array $attributes = []) |
|
23 | { |
||
24 | 33 | parent::__construct($attributes); |
|
25 | 33 | $this->table = config('links.tables.groups'); |
|
26 | 33 | } |
|
27 | |||
28 | /** |
||
29 | * Prepare a date for array / JSON serialization. |
||
30 | * |
||
31 | * @param \DateTimeInterface $date |
||
32 | * @return string |
||
33 | */ |
||
34 | 3 | protected function serializeDate(\DateTimeInterface $date) |
|
35 | { |
||
36 | 3 | return $date->format('Y-m-d H:i:s'); |
|
37 | } |
||
38 | |||
39 | 7 | public function links() |
|
40 | { |
||
41 | 7 | return $this->hasMany(Link::class); |
|
42 | } |
||
43 | |||
44 | |||
45 | 24 | public function setExpiryAttribute($expiry) |
|
46 | { |
||
47 | 24 | $normaliser = resolve(NormaliserManagerContract::class); |
|
48 | 24 | $normalisedExpiry = $normaliser->normalise($expiry); |
|
49 | 24 | $this->attributes['expiry'] = $normalisedExpiry; |
|
50 | 24 | } |
|
51 | |||
52 | 10 | public function clickLimitReached() |
|
53 | { |
||
54 | 10 | if($this->click_limit === null) { |
|
0 ignored issues
–
show
|
|||
55 | 6 | return false; |
|
56 | } |
||
57 | 4 | return $this->click_limit <= $this->links->sum('clicks'); |
|
58 | } |
||
59 | |||
60 | 6 | public function expired() |
|
61 | { |
||
62 | 6 | if($this->expiry === null) { |
|
0 ignored issues
–
show
|
|||
63 | 1 | return false; |
|
64 | } |
||
65 | 5 | return !(new Carbon($this->expiry))->isFuture(); |
|
66 | } |
||
67 | |||
68 | } |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.