Namespaced models and refactored filters

This commit is contained in:
Graham Campbell
2015-01-02 00:18:19 +00:00
parent 15a6694865
commit 0ccb5e289c
66 changed files with 310 additions and 195 deletions

90
src/Models/Component.php Normal file
View File

@@ -0,0 +1,90 @@
<?php
namespace CachetHQ\Cachet\Models;
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 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',
'status' => 'integer',
'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\HasMany
*/
public function incidents()
{
return $this->hasMany('CachetHQ\Cachet\Models\Incident', 'component_id', 'id');
}
/**
* Finds all components by status.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $status
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeStatus(Builder $query, $status)
{
return $query->where('status', $status);
}
/**
* Finds all components which don't have the given status.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $status
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotStatus(Builder $query, $status)
{
return $query->where('status', '<>', $status);
}
/**
* Looks up the human readable version of the status.
*
* @return string
*/
public function getHumanStatusAttribute()
{
return trans('cachet.component.status.'.$this->status);
}
/**
* Get the transformer instance.
*
* @return \CachetHQ\Cachet\Transformers\ComponentTransformer
*/
public function getTransformer()
{
return new ComponentTransformer();
}
}

113
src/Models/Incident.php Normal file
View File

@@ -0,0 +1,113 @@
<?php
namespace CachetHQ\Cachet\Models;
use CachetHQ\Cachet\Transformers\IncidentTransformer;
use Dingo\Api\Transformer\TransformableInterface;
use GrahamCampbell\Markdown\Facades\Markdown;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Watson\Validating\ValidatingTrait;
class Incident extends Model implements TransformableInterface
{
use SoftDeletingTrait, ValidatingTrait;
/**
* The validation rules.
*
* @var string[]
*/
protected $rules = [
'user_id' => 'required|integer',
'component_id' => 'integer',
'name' => 'required',
'status' => 'required|integer',
'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'];
/**
* An incident belongs to a component.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function component()
{
return $this->belongsTo('CachetHQ\Cachet\Models\Component', 'component_id', 'id');
}
/**
* Returns a human readable version of the status.
*
* @return string
*/
public function getHumanStatusAttribute()
{
$statuses = trans('cachet.incident.status');
return $statuses[$this->status];
}
/**
* Finds the icon to use for each status.
*
* @return string
*/
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';
}
}
/**
* Returns a Markdown formatted version of the status.
*
* @return string
*/
public function getFormattedMessageAttribute()
{
return Markdown::render($this->message);
}
/**
* Get the transformer instance.
*
* @return \CachetHQ\Cachet\Transformers\IncidentTransformer
*/
public function getTransformer()
{
return new IncidentTransformer();
}
/**
* Check if Incident has message.
*
* @return bool
*/
public function hasMessage()
{
return (trim($this->message) !== '');
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace CachetHQ\Cachet\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Str;
use Watson\Validating\ValidatingTrait;
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'];
/**
* Overrides the models boot method.
*
* @return void
*/
public static function boot()
{
parent::boot();
self::saving(function ($template) {
$template->slug = Str::slug($template->name);
});
}
}

61
src/Models/Metric.php Normal file
View File

@@ -0,0 +1,61 @@
<?php
namespace CachetHQ\Cachet\Models;
use CachetHQ\Cachet\Transformers\MetricTransformer;
use Dingo\Api\Transformer\TransformableInterface;
use Illuminate\Database\Eloquent\Model;
use Watson\Validating\ValidatingTrait;
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\Relations\HasMany
*/
public function points()
{
return $this->hasMany('CachetHQ\Cachet\Models\MetricPoint', 'metric_id', 'id');
}
/**
* Determines whether a chart should be shown.
*
* @return bool
*/
public function getShouldDisplayAttribute()
{
return $this->display_chart === 1;
}
/**
* Get the transformer instance.
*
* @return \CachetHQ\Cachet\Transformers\MetricTransformer
*/
public function getTransformer()
{
return new MetricTransformer();
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace CachetHQ\Cachet\Models;
use Illuminate\Database\Eloquent\Model;
class MetricPoint extends Model
{
/**
* A metric point belongs to a metric unit.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function metric()
{
return $this->belongsTo('CachetHQ\Cachet\Models\Metric', 'id', 'metric_id');
}
}

44
src/Models/Service.php Normal file
View File

@@ -0,0 +1,44 @@
<?php
namespace CachetHQ\Cachet\Models;
use Illuminate\Database\Eloquent\Model;
use Watson\Validating\ValidatingTrait;
class Service extends Model
{
use ValidatingTrait;
/**
* The validation rules.
*
* @var string[]
*/
protected $rules = [
'type' => 'alpha_dash|required',
'active' => 'required|in:0,1',
'properties' => '',
];
/**
* Returns a decoded properties object for the service.
*
* @param string $properties
*
* @return object
*/
public function getPropertiesAttribute($properties)
{
return json_decode($properties);
}
/**
* Sets the properties attribute which auto encodes to a JSON string.
*
* @param mixed $properties
*/
public function setPropertiesAttribute($properties)
{
$this->attributes['properties'] = json_encode($properties);
}
}

43
src/Models/Setting.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace CachetHQ\Cachet\Models;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = ['name', 'value'];
/**
* Returns a setting from the database.
*
* @param string $settingName
* @param bool $checkEnv
*
* @return string|null
*/
public static function get($settingName, $checkEnv = true)
{
$setting = null;
try {
$setting = self::whereName($settingName)->first()->value;
} catch (ErrorException $e) {
if ($checkEnv) {
$env = getenv(strtoupper($settingName));
if (!$env) {
return $env;
}
}
return $setting;
}
return $setting;
}
}

28
src/Models/Subscriber.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
namespace CachetHQ\Cachet\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Watson\Validating\ValidatingTrait;
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'];
}

64
src/Models/User.php Normal file
View File

@@ -0,0 +1,64 @@
<?php
namespace CachetHQ\Cachet\Models;
use Illuminate\Auth\Reminders\RemindableInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\UserTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
class User extends Model implements UserInterface, RemindableInterface
{
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The hidden properties.
*
* These are excluded when we are serializing the model.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
/**
* The properties that cannot be mass assigned.
*
* @var array
*/
protected $guarded = [];
/**
* Hash any password being inserted by default.
*
* @param string $password
*
* @return $this
*/
public function setPasswordAttribute($password)
{
$this->attributes['password'] = Hash::make($password);
return $this;
}
/**
* Returns a Gravatar URL for the users email address.
*
* @param int $size
*
* @return string
*/
public function getGravatarAttribute($size = 200)
{
return sprintf('https://www.gravatar.com/avatar/%s?size=%d', md5($this->email), $size);
}
}

122
src/Models/WebHook.php Normal file
View File

@@ -0,0 +1,122 @@
<?php
namespace CachetHQ\Cachet\Models;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class WebHook extends Model
{
const HEAD = 0;
const GET = 1;
const POST = 2;
const PATCH = 3;
const PUT = 4;
const DELETE = 5;
/**
* Returns all responses for a WebHook.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function response()
{
return $this->hasMany('WebHookContent', 'hook_id', 'id');
}
/**
* Returns all active hooks.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeActive(Builder $query)
{
return $query->where('active', 1);
}
/**
* Setups a ping event that is fired upon a web hook.
*
* @return \CachetHQ\Cachet\Models\WebHookResponse
*/
public function ping()
{
return $this->fire('ping', 'Coming live to you from Cachet.');
}
/**
* Fires the actual web hook event.
*
* @param string $eventType The event to send X-Cachet-Event
* @param mixed $data Data to send to the Web Hook
*
* @return \CachetHQ\Cachet\Models\WebHookResponse
*/
public function fire($eventType, $data = null)
{
$startTime = microtime(true);
$client = new Client();
$request = $client->createRequest($this->requestMethod, $this->endpoint, [
'headers' => [
'X-Cachet-Event' => $eventType,
],
'body' => $data
]);
try {
$response = $client->send($request);
} catch (ClientException $e) {
// Do nothing with the exception, we want it.
$response = $e->getResponse();
}
$timeTaken = microtime(true) - $startTime;
// Store the request
$hookResponse = new WebHookResponse();
$hookResponse->web_hook_id = $this->id;
$hookResponse->response_code = $response->getStatusCode();
$hookResponse->sent_headers = json_encode($request->getHeaders());
$hookResponse->sent_body = json_encode($data);
$hookResponse->recv_headers = json_encode($response->getHeaders());
$hookResponse->recv_body = json_encode($response->getBody());
$hookResponse->time_taken = $timeTaken;
$hookResponse->save();
return $hookResponse;
}
/**
* Returns a human readable request type name.
*
* @throws \Exception
*
* @return string
*/
public function getRequestMethodAttribute()
{
$requestMethod = null;
switch ($this->request_type) {
case self::HEAD:
return 'HEAD';
case self::GET:
return 'GET';
case self::POST:
return 'POST';
case self::PATCH:
return 'PATCH';
case self::PUT:
return 'PUT';
case self::DELETE:
return 'DELETE';
default:
throw new Exception('Unknown request type value: '.$this->request_type);
}
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace CachetHQ\Cachet\Models;
use Illuminate\Database\Eloquent\Model;
class WebHookResponse extends Model
{
/**
* Returns the hook that a response belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function hook()
{
return $this->belongsTo('WebHook', 'id', 'hook_id');
}
}