2014-11-25 16:09:03 +00:00
|
|
|
<?php
|
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
2015-07-06 17:37:01 +01:00
|
|
|
* (c) Alt Three Services Limited
|
2015-04-19 08:52:39 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-01-02 00:18:19 +00:00
|
|
|
namespace CachetHQ\Cachet\Models;
|
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
use AltThree\Validator\ValidatingTrait;
|
2015-07-14 14:09:32 +01:00
|
|
|
use CachetHQ\Cachet\Presenters\SubscriberPresenter;
|
2015-12-09 17:32:18 -06:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2015-12-09 18:38:06 -05:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2016-12-30 16:22:05 +00:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2015-07-14 14:09:32 +01:00
|
|
|
use McCool\LaravelAutoPresenter\HasPresenter;
|
2014-11-25 16:09:03 +00:00
|
|
|
|
2016-12-30 16:22:05 +00:00
|
|
|
/**
|
|
|
|
* This is the subscriber model.
|
|
|
|
*
|
|
|
|
* @author Joseph Cohen <joe@alt-three.com>
|
|
|
|
* @author James Brooks <james@alt-three.com>
|
|
|
|
* @author Graham Campbell <graham@alt-three.com>
|
|
|
|
*/
|
2015-07-14 14:09:32 +01:00
|
|
|
class Subscriber extends Model implements HasPresenter
|
2014-12-20 21:20:17 +00:00
|
|
|
{
|
2016-12-30 16:22:05 +00:00
|
|
|
use Notifiable, ValidatingTrait;
|
2014-11-25 16:09:03 +00:00
|
|
|
|
2015-01-01 18:57:33 +00:00
|
|
|
/**
|
2015-08-22 15:57:53 +01:00
|
|
|
* The attributes that should be casted to native types.
|
2015-01-01 18:57:33 +00:00
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2015-08-22 15:57:53 +01:00
|
|
|
protected $casts = [
|
2017-01-04 20:26:57 +00:00
|
|
|
'email' => 'string',
|
|
|
|
'phone_number' => 'string',
|
|
|
|
'slack_webhook_url' => 'string',
|
|
|
|
'verify_code' => 'string',
|
|
|
|
'verified_at' => 'date',
|
|
|
|
'global' => 'bool',
|
2014-11-27 16:05:00 +00:00
|
|
|
];
|
|
|
|
|
2015-05-24 16:33:03 -05:00
|
|
|
/**
|
2015-08-22 15:57:53 +01:00
|
|
|
* The fillable properties.
|
2015-05-24 16:33:03 -05:00
|
|
|
*
|
2015-08-22 15:57:53 +01:00
|
|
|
* @var string[]
|
2015-05-24 16:33:03 -05:00
|
|
|
*/
|
2016-12-23 13:34:38 +00:00
|
|
|
protected $fillable = [
|
|
|
|
'email',
|
2017-01-04 20:26:57 +00:00
|
|
|
'phone_number',
|
|
|
|
'slack_webhook_url',
|
2016-12-23 13:34:38 +00:00
|
|
|
'verified_at',
|
|
|
|
'global',
|
|
|
|
];
|
2015-05-24 16:33:03 -05:00
|
|
|
|
2015-08-03 08:54:40 +01:00
|
|
|
/**
|
2015-08-22 15:57:53 +01:00
|
|
|
* The validation rules.
|
2015-08-03 08:54:40 +01:00
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2015-08-22 15:57:53 +01:00
|
|
|
public $rules = [
|
2017-01-04 21:02:52 +00:00
|
|
|
'email' => 'nullable|email',
|
|
|
|
'phone_number' => 'nullable|string',
|
|
|
|
'slack_webhook_url' => 'nullable|url',
|
2015-08-03 08:54:40 +01:00
|
|
|
];
|
|
|
|
|
2016-02-09 19:09:30 +00:00
|
|
|
/**
|
|
|
|
* The relations to eager load on every query.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $with = ['subscriptions'];
|
|
|
|
|
2015-05-24 16:33:03 -05:00
|
|
|
/**
|
|
|
|
* Overrides the models boot method.
|
2016-12-15 20:11:06 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2015-05-24 16:33:03 -05:00
|
|
|
*/
|
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
self::creating(function ($user) {
|
|
|
|
if (!$user->verify_code) {
|
|
|
|
$user->verify_code = self::generateVerifyCode();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-13 19:25:43 +01:00
|
|
|
/**
|
|
|
|
* Get all of the meta relation.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
|
|
*/
|
|
|
|
public function meta()
|
|
|
|
{
|
|
|
|
return $this->morphMany(Meta::class, 'meta');
|
|
|
|
}
|
|
|
|
|
2016-01-10 15:54:54 +00:00
|
|
|
/**
|
2016-08-10 10:46:45 +01:00
|
|
|
* Get the subscriptions relation.
|
2016-01-10 15:54:54 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
2016-02-10 14:33:16 +00:00
|
|
|
public function subscriptions()
|
2016-01-10 15:54:54 +00:00
|
|
|
{
|
|
|
|
return $this->hasMany(Subscription::class);
|
|
|
|
}
|
|
|
|
|
2015-12-09 15:29:41 -06:00
|
|
|
/**
|
2015-12-17 14:41:22 +00:00
|
|
|
* Scope verified subscribers.
|
2015-12-09 15:29:41 -06:00
|
|
|
*
|
2015-12-09 17:32:18 -06:00
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
*
|
2015-12-09 15:29:41 -06:00
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
2015-12-17 14:41:22 +00:00
|
|
|
public function scopeIsVerified(Builder $query)
|
2015-12-09 15:29:41 -06:00
|
|
|
{
|
|
|
|
return $query->whereNotNull('verified_at');
|
|
|
|
}
|
|
|
|
|
2016-04-30 02:25:05 -05:00
|
|
|
/**
|
|
|
|
* Scope global subscribers.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeIsGlobal(Builder $query)
|
|
|
|
{
|
2016-10-19 12:29:16 +01:00
|
|
|
return $query->where('global', '=', true);
|
2016-04-30 02:25:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds all verified subscriptions for a component.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @param int $component_id
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeForComponent(Builder $query, $component_id)
|
|
|
|
{
|
|
|
|
return $query->select('subscribers.*')
|
|
|
|
->join('subscriptions', 'subscribers.id', '=', 'subscriptions.subscriber_id')
|
2016-10-19 12:29:16 +01:00
|
|
|
->where('subscriptions.component_id', '=', $component_id);
|
2016-04-30 02:25:05 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 16:33:03 -05:00
|
|
|
/**
|
|
|
|
* Determines if the subscriber is verified.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-12-09 17:32:18 -06:00
|
|
|
public function getIsVerifiedAttribute()
|
2015-05-24 16:33:03 -05:00
|
|
|
{
|
2015-11-21 22:04:14 +00:00
|
|
|
return $this->verified_at !== null;
|
2015-05-24 16:33:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an new verify code.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function generateVerifyCode()
|
|
|
|
{
|
|
|
|
return str_random(42);
|
|
|
|
}
|
2015-07-14 14:09:32 +01:00
|
|
|
|
2016-12-30 16:22:05 +00:00
|
|
|
/**
|
|
|
|
* Route notifications for the Nexmo channel.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function routeNotificationForNexmo()
|
|
|
|
{
|
|
|
|
return $this->phone_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Route notifications for the Slack channel.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function routeNotificationForSlack()
|
|
|
|
{
|
|
|
|
return $this->slack_webhook_url;
|
|
|
|
}
|
|
|
|
|
2015-07-14 14:09:32 +01:00
|
|
|
/**
|
|
|
|
* Get the presenter class.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPresenterClass()
|
|
|
|
{
|
|
|
|
return SubscriberPresenter::class;
|
|
|
|
}
|
2014-11-27 22:08:28 +00:00
|
|
|
}
|