Cachet/app/Models/Incident.php

158 lines
3.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;
2015-06-16 09:46:29 +01:00
use CachetHQ\Cachet\Presenters\IncidentPresenter;
2015-03-12 07:26:15 +00:00
use Carbon\Carbon;
2015-01-01 12:23:17 +00:00
use Illuminate\Database\Eloquent\Model;
2015-03-20 18:30:45 -06:00
use Illuminate\Database\Eloquent\SoftDeletes;
use McCool\LaravelAutoPresenter\HasPresenter;
2015-03-20 18:30:45 -06:00
class Incident extends Model implements HasPresenter
2014-12-20 21:20:17 +00:00
{
2015-03-20 18:30:45 -06:00
use SoftDeletes, ValidatingTrait;
2015-01-01 18:57:33 +00:00
/**
* The accessors to append to the model's serialized form.
2015-01-01 18:57:33 +00:00
*
* @var string[]
*/
protected $appends = ['human_status'];
/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
protected $casts = [
'id' => 'int',
'visible' => 'integer',
];
/**
* The attributes that should be mutated to dates.
*
* @var string[]
*/
protected $dates = ['scheduled_at', 'deleted_at'];
2015-01-01 18:57:33 +00:00
/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = [
'component_id',
'name',
'status',
2015-05-20 08:41:02 +01:00
'visible',
'message',
'scheduled_at',
'created_at',
'updated_at',
];
2015-01-01 18:57:33 +00:00
/**
* The validation rules.
2015-06-18 16:13:02 +01:00
*
* @var string[]
*/
public $rules = [
'component_id' => 'integer',
'name' => 'required',
'status' => 'required|integer',
'visible' => 'required|boolean',
'message' => 'required',
2015-06-18 16:13:02 +01:00
];
2015-05-20 08:41:02 +01:00
/**
* Finds all visible incidents.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeVisible($query)
{
return $query->where('visible', 1);
}
/**
* Finds all scheduled incidents (maintenance).
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeScheduled($query)
{
return $query->where('status', 0)->where('scheduled_at', '>=', Carbon::now());
}
/**
* Finds all non-scheduled incidents.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotScheduled($query)
{
return $query->where(function ($query) {
return $query->whereNull('scheduled_at')->orWhere('scheduled_at', '<=', Carbon::now());
});
}
/**
* An incident belongs to a component.
2014-12-30 12:49:39 +00:00
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2014-12-20 21:20:17 +00:00
public function component()
{
2015-06-16 09:46:29 +01:00
return $this->belongsTo(Component::class, 'component_id', 'id');
}
/**
* Returns a human readable version of the status.
2014-12-30 12:49:39 +00:00
*
* @return string
*/
2014-12-20 21:20:17 +00:00
public function getHumanStatusAttribute()
{
$statuses = trans('cachet.incidents.status');
2014-12-20 21:20:17 +00:00
return $statuses[$this->status];
}
/**
* Returns whether the "incident" is scheduled or not.
*
* @return bool
*/
public function getIsScheduledAttribute()
{
return $this->getOriginal('scheduled_at') !== null;
}
/**
2015-03-20 18:30:45 -06:00
* Get the presenter class.
2014-12-30 12:49:39 +00:00
*
2015-03-20 18:30:45 -06:00
* @return string
*/
2015-03-20 18:30:45 -06:00
public function getPresenterClass()
2014-12-20 21:20:17 +00:00
{
2015-06-16 09:46:29 +01:00
return IncidentPresenter::class;
}
}