linkeys-app /
signed-url
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Linkeys\UrlSigner\Models; |
||
| 4 | |||
| 5 | use Carbon\Carbon; |
||
| 6 | use Illuminate\Database\Eloquent\Model; |
||
| 7 | use Illuminate\Support\Facades\App; |
||
| 8 | use Linkeys\UrlSigner\Contracts\Models\Link as LinkModelContract; |
||
| 9 | use Linkeys\UrlSigner\Support\ExpiryNormaliser\NormaliserManager; |
||
| 10 | use Linkeys\UrlSigner\Support\ExpiryNormaliser\NormaliserManagerContract; |
||
| 11 | use Linkeys\UrlSigner\Support\ExpiryNormaliser\Normalisers\ExpiryNormaliser; |
||
| 12 | use Linkeys\UrlSigner\Support\ExpiryNormaliser\Normalisers\FromDateTime; |
||
| 13 | use Linkeys\UrlSigner\Support\ExpiryNormaliser\Normalisers\FromInteger; |
||
| 14 | use Linkeys\UrlSigner\Support\ExpiryNormaliser\Normalisers\FromString; |
||
| 15 | use Linkeys\UrlSigner\Support\UrlManipulator\UrlManipulator; |
||
| 16 | use Linkeys\UrlSigner\Support\Uuid\UuidCreator; |
||
| 17 | use Ramsey\Uuid\Uuid; |
||
| 18 | |||
| 19 | class Link extends Model implements LinkModelContract |
||
| 20 | { |
||
| 21 | |||
| 22 | public $fillable = [ |
||
| 23 | 'url', |
||
| 24 | 'data', |
||
| 25 | 'expiry', |
||
| 26 | 'click_limit', |
||
| 27 | ]; |
||
| 28 | |||
| 29 | protected $casts = [ |
||
| 30 | 'data' => 'array', |
||
| 31 | ]; |
||
| 32 | |||
| 33 | protected $dates = [ |
||
| 34 | 'expiry' |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var ExpiryNormaliser |
||
| 39 | */ |
||
| 40 | protected $normaliser; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Prepare a date for array / JSON serialization. |
||
| 44 | * |
||
| 45 | * @param \DateTimeInterface $date |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | protected function serializeDate(\DateTimeInterface $date) |
||
| 49 | { |
||
| 50 | return $date->format('Y-m-d H:i:s'); |
||
| 51 | } |
||
| 52 | |||
| 53 | 55 | public function __construct(array $attributes = []) |
|
| 54 | { |
||
| 55 | 55 | parent::__construct($attributes); |
|
| 56 | 55 | $this->table = config('links.tables.links'); |
|
| 57 | 55 | } |
|
| 58 | |||
| 59 | 11 | public function group() |
|
| 60 | { |
||
| 61 | 11 | return $this->belongsTo(Group::class); |
|
| 62 | } |
||
| 63 | |||
| 64 | 55 | public static function boot() |
|
| 65 | { |
||
| 66 | 55 | parent::boot(); |
|
| 67 | 55 | self::creating(function ($model) { |
|
| 68 | 51 | $model->uuid = $model->uuid ?: resolve(UuidCreator::class)->create(); |
|
| 69 | 55 | }); |
|
| 70 | 55 | } |
|
| 71 | |||
| 72 | 51 | public function setExpiryAttribute($expiry) |
|
| 73 | { |
||
| 74 | 51 | $normaliser = resolve(NormaliserManagerContract::class); |
|
| 75 | 51 | $this->attributes['expiry'] = $normaliser->normalise($expiry); |
|
| 76 | 51 | } |
|
| 77 | |||
| 78 | 3 | public function getFullUrl() |
|
| 79 | { |
||
| 80 | 3 | $urlParser = resolve(UrlManipulator::class); |
|
| 81 | 3 | $urlParser->setUrl($this->url); |
|
|
0 ignored issues
–
show
|
|||
| 82 | 3 | $urlParser->appendQuery([config('links.query_key') => $this->uuid]); |
|
| 83 | 3 | return $urlParser->getUrl(); |
|
| 84 | } |
||
| 85 | |||
| 86 | 15 | public function clickLimitReached() |
|
| 87 | { |
||
| 88 | 15 | if($this->click_limit === null) { |
|
|
0 ignored issues
–
show
|
|||
| 89 | 11 | return false; |
|
| 90 | } |
||
| 91 | 4 | return $this->click_limit <= $this->clicks; |
|
|
0 ignored issues
–
show
|
|||
| 92 | } |
||
| 93 | |||
| 94 | 12 | public function expired() |
|
| 95 | { |
||
| 96 | 12 | if($this->expiry === null) { |
|
|
0 ignored issues
–
show
|
|||
| 97 | 5 | return false; |
|
| 98 | } |
||
| 99 | 7 | return !(new Carbon($this->expiry))->isFuture(); |
|
| 100 | } |
||
| 101 | |||
| 102 | } |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.