Cleaned up the models

This commit is contained in:
Graham Campbell
2015-01-01 18:57:33 +00:00
parent ee2d3bce3b
commit 5937b7b040
9 changed files with 95 additions and 36 deletions

View File

@@ -2,14 +2,21 @@
use CachetHQ\Cachet\Transformers\ComponentTransformer;
use Dingo\Api\Transformer\TransformableInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Support\Facades\Lang;
use Watson\Validating\ValidatingTrait;
class Component extends Model implements TransformableInterface
{
use SoftDeletingTrait, ValidatingTrait;
/**
* The validation rules.
*
* @var string[]
*/
protected $rules = [
'user_id' => 'integer|required',
'name' => 'required',
@@ -17,12 +24,17 @@ class Component extends Model implements TransformableInterface
'link' => 'url',
];
/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = ['name', 'description', 'status', 'user_id', 'tags', 'link', 'order'];
/**
* Lookup all of the incidents reported on the component.
*
* @return \Illuminate\Database\Eloquent\Relations
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function incidents()
{
@@ -37,7 +49,7 @@ class Component extends Model implements TransformableInterface
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeStatus($query, $status)
public function scopeStatus(Builder $query, $status)
{
return $query->where('status', $status);
}
@@ -50,7 +62,7 @@ class Component extends Model implements TransformableInterface
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotStatus($query, $status)
public function scopeNotStatus(Builder $query, $status)
{
return $query->where('status', '<>', $status);
}

View File

@@ -11,6 +11,11 @@ class Incident extends Model implements TransformableInterface
{
use SoftDeletingTrait, ValidatingTrait;
/**
* The validation rules.
*
* @var string[]
*/
protected $rules = [
'user_id' => 'required|integer',
'component_id' => 'integer',
@@ -19,8 +24,18 @@ class Incident extends Model implements TransformableInterface
'message' => 'required',
];
/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = ['user_id', 'component_id', 'name', 'status', 'message'];
/**
* The accessors to append to the model's serialized form.
*
* @var string[]
*/
protected $appends = ['humanStatus'];
/**
@@ -53,10 +68,14 @@ class Incident extends Model implements TransformableInterface
public function getIconAttribute()
{
switch ($this->status) {
case 1: return 'ion ion-flag';
case 2: return 'ion ion-alert';
case 3: return 'ion ion-eye';
case 4: return 'ion ion-checkmark';
case 1:
return 'ion ion-flag';
case 2:
return 'ion ion-alert';
case 3:
return 'ion ion-eye';
case 4:
return 'ion ion-checkmark';
}
}

View File

@@ -7,11 +7,21 @@ class IncidentTemplate extends Model
{
use ValidatingTrait;
/**
* The validation rules.
*
* @var string[]
*/
protected $rules = [
'name' => 'alpha|required',
'template' => 'required',
];
/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = ['name', 'template'];
/**

View File

@@ -9,18 +9,28 @@ class Metric extends Model implements TransformableInterface
{
use ValidatingTrait;
/**
* The validation rules.
*
* @var string[]
*/
protected $rules = [
'name' => 'required',
'suffix' => 'required',
'display_chart' => 'boolean',
];
/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = ['name', 'suffix', 'description', 'display_chart'];
/**
* Metrics contain many metric points.
*
* @return \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function points()
{

View File

@@ -7,6 +7,11 @@ class Service extends Model
{
use ValidatingTrait;
/**
* The validation rules.
*
* @var string[]
*/
protected $rules = [
'type' => 'alpha_dash|required',
'active' => 'required|in:0,1',

View File

@@ -4,6 +4,11 @@ use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = ['name', 'value'];
/**
@@ -12,41 +17,25 @@ class Setting extends Model
* @param string $settingName
* @param bool $checkEnv
*
* @return string
* @return string|null
*/
public static function get($settingName, $checkEnv = true)
{
// Default setting value.
$setting = null;
// First try finding the setting in the database.
try {
$setting = self::whereName($settingName)->first()->value;
} catch (ErrorException $e) {
// If we don't have a setting, check the env (fallback for original version)
if ($checkEnv) {
if (!($setting = getenv(strtoupper($settingName)))) {
return $setting;
$env = getenv(strtoupper($settingName));
if (!$env) {
return $env;
}
} else {
return $setting;
}
return $setting;
}
return $setting;
}
/**
* Throws an Exception.
*
* @param string $setting
*
* @throws \Exception
*
* @return void
*/
public static function unknownSettingException($setting)
{
throw new Exception(sprintf('Unknown setting %s', $setting));
}
}

View File

@@ -8,9 +8,19 @@ class Subscriber extends Model
{
use SoftDeletingTrait, ValidatingTrait;
/**
* The validation rules.
*
* @var string[]
*/
protected $rules = [
'email' => 'required|email',
];
/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = ['email'];
}

View File

@@ -19,14 +19,16 @@ class User extends Model implements UserInterface, RemindableInterface
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
* The hidden properties.
*
* These are excluded when we are serializing the model.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
/**
* Items which cannot be mass assigned.
* The properties that cannot be mass assigned.
*
* @var array
*/
@@ -37,11 +39,13 @@ class User extends Model implements UserInterface, RemindableInterface
*
* @param string $password
*
* @return void
* @return $this
*/
public function setPasswordAttribute($password)
{
$this->attributes['password'] = Hash::make($password);
return $this;
}
/**

View File

@@ -2,11 +2,11 @@
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class WebHook extends Model
{
// Request Methods.
const HEAD = 0;
const GET = 1;
const POST = 2;
@@ -31,7 +31,7 @@ class WebHook extends Model
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeActive($query)
public function scopeActive(Builder $query)
{
return $query->where('active', 1);
}
@@ -94,7 +94,7 @@ class WebHook extends Model
*
* @throws \Exception
*
* @return string HEAD, GET, POST, DELETE, PATCH, PUT etc
* @return string
*/
public function getRequestMethodAttribute()
{