Cachet/app/Models/Incident.php

218 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\Models\Traits\SearchableTrait;
use CachetHQ\Cachet\Models\Traits\SortableTrait;
2015-06-16 09:46:29 +01:00
use CachetHQ\Cachet\Presenters\IncidentPresenter;
use Illuminate\Database\Eloquent\Builder;
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;
/**
* This is the incident model.
*
* @author James Brooks <james@alt-three.com>
* @author Joseph Cohen <joseph@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
*/
2015-03-20 18:30:45 -06:00
class Incident extends Model implements HasPresenter
2014-12-20 21:20:17 +00:00
{
use SearchableTrait, SoftDeletes, SortableTrait, ValidatingTrait;
2016-10-06 17:21:18 +01:00
/**
* Status for incident being investigated.
*
* @var int
*/
const INVESTIGATING = 1;
/**
* Status for incident having been identified.
*
* @var int
*/
const IDENTIFIED = 2;
/**
* Status for incident being watched.
*
* @var int
*/
const WATCHED = 3;
/**
* Status for incident now being fixed.
*
* @var int
*/
const FIXED = 4;
/**
* The accessors to append to the model's array form.
*
* @var string[]
*/
protected $appends = [
'is_resolved',
];
/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
protected $casts = [
'visible' => 'int',
'stickied' => 'bool',
'occurred_at' => 'date',
'deleted_at' => 'date',
];
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',
2016-08-17 01:12:21 +02:00
'stickied',
'message',
'occurred_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 = [
2016-10-19 12:29:16 +01:00
'component_id' => 'nullable|int',
'name' => 'required|string',
2015-10-20 20:33:23 +01:00
'status' => 'required|int',
2015-10-20 20:34:59 +01:00
'visible' => 'required|bool',
2016-10-19 12:29:16 +01:00
'stickied' => 'required|bool',
'message' => 'required|string',
2015-06-18 16:13:02 +01:00
];
/**
* The searchable fields.
*
* @var string[]
*/
protected $searchable = [
'id',
'component_id',
'name',
'status',
'visible',
2016-08-17 01:12:21 +02:00
'stickied',
];
/**
* The sortable fields.
*
* @var string[]
*/
protected $sortable = [
'id',
'name',
'status',
'visible',
2016-08-17 01:12:21 +02:00
'stickied',
'message',
'occurred_at',
];
2016-10-06 17:21:18 +01:00
/**
* The relations to eager load on every query.
*
* @var string[]
*/
protected $with = ['updates'];
2016-08-10 10:46:45 +01:00
/**
* Get the component relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function component()
{
return $this->belongsTo(Component::class, 'component_id', 'id');
}
2016-10-06 17:21:18 +01:00
/**
* Get the updates relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function updates()
{
return $this->hasMany(IncidentUpdate::class)->orderBy('created_at', 'desc');
}
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(Builder $query)
2015-05-20 08:41:02 +01:00
{
2016-10-19 12:29:16 +01:00
return $query->where('visible', '=', 1);
2015-05-20 08:41:02 +01:00
}
2016-08-17 01:12:21 +02:00
/**
* Finds all stickied incidents.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeStickied(Builder $query)
{
2016-10-19 12:29:16 +01:00
return $query->where('stickied', '=', true);
2016-08-17 01:12:21 +02:00
}
2016-10-06 17:21:18 +01:00
/**
* Is the incident resolved?
*
* @return bool
*/
public function getIsResolvedAttribute()
{
if ($updates = $this->updates->first()) {
return $updates->status === self::FIXED;
}
return $this->status === self::FIXED;
}
/**
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;
}
}