Cachet/app/Models/Subscriber.php

202 lines
4.5 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (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;
2015-12-09 18:38:06 -05:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use McCool\LaravelAutoPresenter\HasPresenter;
/**
* 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>
*/
class Subscriber extends Model implements HasPresenter
2014-12-20 21:20:17 +00:00
{
use Notifiable, ValidatingTrait;
2015-01-01 18:57:33 +00:00
/**
* The attributes that should be casted to native types.
2015-01-01 18:57:33 +00:00
*
* @var string[]
*/
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',
];
2015-05-24 16:33:03 -05:00
/**
* The fillable properties.
2015-05-24 16:33:03 -05:00
*
* @var string[]
2015-05-24 16:33:03 -05:00
*/
protected $fillable = [
'email',
2017-01-04 20:26:57 +00:00
'phone_number',
'slack_webhook_url',
'verified_at',
'global',
];
2015-05-24 16:33:03 -05:00
2015-08-03 08:54:40 +01:00
/**
* The validation rules.
2015-08-03 08:54:40 +01:00
*
* @var string[]
*/
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
];
/**
* 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-08-10 10:46:45 +01:00
* Get the subscriptions relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2016-02-10 14:33:16 +00:00
public function subscriptions()
{
return $this->hasMany(Subscription::class);
}
/**
2015-12-17 14:41:22 +00:00
* Scope verified subscribers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
2015-12-17 14:41:22 +00:00
public function scopeIsVerified(Builder $query)
{
return $query->whereNotNull('verified_at');
}
/**
* 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);
}
/**
* 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);
}
2015-05-24 16:33:03 -05:00
/**
* Determines if the subscriber is verified.
*
* @return bool
*/
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);
}
/**
* 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;
}
/**
* Get the presenter class.
*
* @return string
*/
public function getPresenterClass()
{
return SubscriberPresenter::class;
}
}