Cachet/app/Models/Subscriber.php
Graham Campbell 1607275d2d Merge branch '2.0'
# Conflicts:
#	VERSION
#	app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php
#	app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php
#	app/Models/Subscriber.php
2015-12-18 12:55:47 +00:00

108 lines
2.2 KiB
PHP

<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait;
use CachetHQ\Cachet\Presenters\SubscriberPresenter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use McCool\LaravelAutoPresenter\HasPresenter;
class Subscriber extends Model implements HasPresenter
{
use ValidatingTrait;
/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
protected $casts = [
'id' => 'int',
'email' => 'string',
'verify_code' => 'string',
'verified_at' => 'date',
];
/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = ['email'];
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'email' => 'required|email',
];
/**
* Overrides the models boot method.
*/
public static function boot()
{
parent::boot();
self::creating(function ($user) {
if (!$user->verify_code) {
$user->verify_code = self::generateVerifyCode();
}
});
}
/**
* Scope verified subscribers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsVerified(Builder $query)
{
return $query->whereNotNull('verified_at');
}
/**
* Determines if the subscriber is verified.
*
* @return bool
*/
public function getIsVerifiedAttribute()
{
return $this->verified_at !== null;
}
/**
* Returns an new verify code.
*
* @return string
*/
public static function generateVerifyCode()
{
return str_random(42);
}
/**
* Get the presenter class.
*
* @return string
*/
public function getPresenterClass()
{
return SubscriberPresenter::class;
}
}