Cleanup models

This commit is contained in:
Graham Campbell 2015-01-01 12:23:17 +00:00
parent a14c28b287
commit a442708062
11 changed files with 79 additions and 84 deletions

View File

@ -1,11 +1,14 @@
<?php <?php
use CachetHQ\Cachet\Transformers\ComponentTransformer;
use Dingo\Api\Transformer\TransformableInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Watson\Validating\ValidatingTrait; use Watson\Validating\ValidatingTrait;
class Component extends Eloquent implements \Dingo\Api\Transformer\TransformableInterface class Component extends Model implements TransformableInterface
{ {
use ValidatingTrait; use SoftDeletingTrait, ValidatingTrait;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
protected $rules = [ protected $rules = [
'user_id' => 'integer|required', 'user_id' => 'integer|required',
@ -14,20 +17,12 @@ class Component extends Eloquent implements \Dingo\Api\Transformer\Transformable
'link' => 'url', 'link' => 'url',
]; ];
protected $fillable = [ protected $fillable = ['name', 'description', 'status', 'user_id', 'tags', 'link', 'order'];
'name',
'description',
'status',
'user_id',
'tags',
'link',
'order',
];
/** /**
* Lookup all of the incidents reported on the component. * Lookup all of the incidents reported on the component.
* *
* @return Illuminate\Database\Eloquent\Relations * @return \Illuminate\Database\Eloquent\Relations
*/ */
public function incidents() public function incidents()
{ {
@ -37,10 +32,10 @@ class Component extends Eloquent implements \Dingo\Api\Transformer\Transformable
/** /**
* Finds all components by status. * Finds all components by status.
* *
* @param Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $query
* @param int $status * @param int $status
* *
* @return Illuminate\Database\Eloquent\Builder * @return \Illuminate\Database\Eloquent\Builder
*/ */
public function scopeStatus($query, $status) public function scopeStatus($query, $status)
{ {
@ -50,10 +45,10 @@ class Component extends Eloquent implements \Dingo\Api\Transformer\Transformable
/** /**
* Finds all components which don't have the given status. * Finds all components which don't have the given status.
* *
* @param Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $query
* @param int $status * @param int $status
* *
* @return Illuminate\Database\Eloquent\Builder * @return \Illuminate\Database\Eloquent\Builder
*/ */
public function scopeNotStatus($query, $status) public function scopeNotStatus($query, $status)
{ {
@ -73,10 +68,10 @@ class Component extends Eloquent implements \Dingo\Api\Transformer\Transformable
/** /**
* Get the transformer instance. * Get the transformer instance.
* *
* @return ComponentTransformer * @return \CachetHQ\Cachet\Transformers\ComponentTransformer
*/ */
public function getTransformer() public function getTransformer()
{ {
return new CachetHQ\Cachet\Transformers\ComponentTransformer(); return new ComponentTransformer();
} }
} }

View File

@ -1,10 +1,12 @@
<?php <?php
use CachetHQ\Cachet\Transformers\IncidentTransformer;
use Dingo\Api\Transformer\TransformableInterface; use Dingo\Api\Transformer\TransformableInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingTrait; use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Watson\Validating\ValidatingTrait; use Watson\Validating\ValidatingTrait;
class Incident extends Eloquent implements TransformableInterface class Incident extends Model implements TransformableInterface
{ {
use SoftDeletingTrait, ValidatingTrait; use SoftDeletingTrait, ValidatingTrait;
@ -74,7 +76,7 @@ class Incident extends Eloquent implements TransformableInterface
*/ */
public function getTransformer() public function getTransformer()
{ {
return new CachetHQ\Cachet\Transformers\IncidentTransformer(); return new IncidentTransformer();
} }
/** /**

View File

@ -1,8 +1,9 @@
<?php <?php
use Illuminate\Database\Eloquent\Model;
use Watson\Validating\ValidatingTrait; use Watson\Validating\ValidatingTrait;
class IncidentTemplate extends Eloquent class IncidentTemplate extends Model
{ {
use ValidatingTrait; use ValidatingTrait;
@ -11,10 +12,7 @@ class IncidentTemplate extends Eloquent
'template' => 'required', 'template' => 'required',
]; ];
protected $fillable = [ protected $fillable = ['name', 'template'];
'name',
'template',
];
/** /**
* Overrides the models boot method. * Overrides the models boot method.

View File

@ -1,8 +1,11 @@
<?php <?php
use CachetHQ\Cachet\Transformers\MetricTransformer;
use Dingo\Api\Transformer\TransformableInterface;
use Illuminate\Database\Eloquent\Model;
use Watson\Validating\ValidatingTrait; use Watson\Validating\ValidatingTrait;
class Metric extends Eloquent implements \Dingo\Api\Transformer\TransformableInterface class Metric extends Model implements TransformableInterface
{ {
use ValidatingTrait; use ValidatingTrait;
@ -17,7 +20,7 @@ class Metric extends Eloquent implements \Dingo\Api\Transformer\TransformableInt
/** /**
* Metrics contain many metric points. * Metrics contain many metric points.
* *
* @return Illuminate\Database\Eloquent\Builder * @return \Illuminate\Database\Eloquent\Builder
*/ */
public function points() public function points()
{ {
@ -37,10 +40,10 @@ class Metric extends Eloquent implements \Dingo\Api\Transformer\TransformableInt
/** /**
* Get the transformer instance. * Get the transformer instance.
* *
* @return CachetHQ\Cachet\Transformers\MetricTransformer * @return \CachetHQ\Cachet\Transformers\MetricTransformer
*/ */
public function getTransformer() public function getTransformer()
{ {
return new CachetHQ\Cachet\Transformers\MetricTransformer(); return new MetricTransformer();
} }
} }

View File

@ -1,6 +1,8 @@
<?php <?php
class MetricPoint extends Eloquent use Illuminate\Database\Eloquent\Model;
class MetricPoint extends Model
{ {
/** /**
* A metric point belongs to a metric unit. * A metric point belongs to a metric unit.

View File

@ -1,8 +1,9 @@
<?php <?php
use Illuminate\Database\Eloquent\Model;
use Watson\Validating\ValidatingTrait; use Watson\Validating\ValidatingTrait;
class Service extends Eloquent class Service extends Model
{ {
use ValidatingTrait; use ValidatingTrait;

View File

@ -1,6 +1,8 @@
<?php <?php
class Setting extends Eloquent use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{ {
protected $fillable = ['name', 'value']; protected $fillable = ['name', 'value'];
@ -20,7 +22,7 @@ class Setting extends Eloquent
// First try finding the setting in the database. // First try finding the setting in the database.
try { try {
$setting = self::whereName($settingName)->first()->value; $setting = self::whereName($settingName)->first()->value;
} catch (\ErrorException $e) { } catch (ErrorException $e) {
// If we don't have a setting, check the env (fallback for original version) // If we don't have a setting, check the env (fallback for original version)
if ($checkEnv) { if ($checkEnv) {
if (!($setting = getenv(strtoupper($settingName)))) { if (!($setting = getenv(strtoupper($settingName)))) {
@ -39,14 +41,12 @@ class Setting extends Eloquent
* *
* @param string $setting * @param string $setting
* *
* @throws Exception * @throws \Exception
* *
* @return void * @return void
*/ */
public static function unknownSettingException($setting) public static function unknownSettingException($setting)
{ {
throw new \Exception( throw new Exception(sprintf('Unknown setting %s', $setting));
sprintf('Unknown setting %s', $setting)
);
} }
} }

View File

@ -1,12 +1,12 @@
<?php <?php
use Illuminate\Database\Eloquent\SoftDeletingTrait; use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Database\Eloquent\Model;
use Watson\Validating\ValidatingTrait; use Watson\Validating\ValidatingTrait;
class Subscriber extends Eloquent class Subscriber extends Model
{ {
use ValidatingTrait; use SoftDeletingTrait, ValidatingTrait;
use SoftDeletingTrait;
protected $rules = [ protected $rules = [
'email' => 'required|email', 'email' => 'required|email',

View File

@ -4,8 +4,10 @@ use Illuminate\Auth\Reminders\RemindableInterface;
use Illuminate\Auth\Reminders\RemindableTrait; use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\UserInterface; use Illuminate\Auth\UserInterface;
use Illuminate\Auth\UserTrait; use Illuminate\Auth\UserTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
class User extends Eloquent implements UserInterface, RemindableInterface class User extends Model implements UserInterface, RemindableInterface
{ {
use UserTrait, RemindableTrait; use UserTrait, RemindableTrait;
@ -33,7 +35,7 @@ class User extends Eloquent implements UserInterface, RemindableInterface
/** /**
* Hash any password being inserted by default. * Hash any password being inserted by default.
* *
* @param string @password * @param string $password
* *
* @return void * @return void
*/ */
@ -51,10 +53,6 @@ class User extends Eloquent implements UserInterface, RemindableInterface
*/ */
public function getGravatarAttribute($size = 200) public function getGravatarAttribute($size = 200)
{ {
return sprintf( return sprintf('https://www.gravatar.com/avatar/%s?size=%d', md5($this->email), $size);
'https://www.gravatar.com/avatar/%s?size=%d',
md5($this->email),
$size
);
} }
} }

View File

@ -1,6 +1,10 @@
<?php <?php
class WebHook extends Eloquent use Illuminate\Database\Eloquent\Model;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
class WebHook extends Model
{ {
// Request Methods. // Request Methods.
const HEAD = 0; const HEAD = 0;
@ -13,7 +17,7 @@ class WebHook extends Eloquent
/** /**
* Returns all responses for a WebHook. * Returns all responses for a WebHook.
* *
* @return Illuminate\Database\Eloquent\Builder * @return \Illuminate\Database\Eloquent\Builder
*/ */
public function response() public function response()
{ {
@ -23,9 +27,9 @@ class WebHook extends Eloquent
/** /**
* Returns all active hooks. * Returns all active hooks.
* *
* @param Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $query
* *
* @return Illuminate\Database\Eloquent\Builder * @return \Illuminate\Database\Eloquent\Builder
*/ */
public function scopeActive($query) public function scopeActive($query)
{ {
@ -35,7 +39,7 @@ class WebHook extends Eloquent
/** /**
* Setups a Ping event that is fired upon a web hook. * Setups a Ping event that is fired upon a web hook.
* *
* @return array result of the ping * @return object
*/ */
public function ping() public function ping()
{ {
@ -54,7 +58,7 @@ class WebHook extends Eloquent
{ {
$startTime = microtime(true); $startTime = microtime(true);
$client = new \GuzzleHttp\Client(); $client = new Client();
$request = $client->createRequest($this->requestMethod, $this->endpoint, [ $request = $client->createRequest($this->requestMethod, $this->endpoint, [
'headers' => [ 'headers' => [
'X-Cachet-Event' => $eventType, 'X-Cachet-Event' => $eventType,
@ -64,7 +68,7 @@ class WebHook extends Eloquent
try { try {
$response = $client->send($request); $response = $client->send($request);
} catch (\GuzzleHttp\Exception\ClientException $e) { } catch (ClientException $e) {
// Do nothing with the exception, we want it. // Do nothing with the exception, we want it.
$response = $e->getResponse(); $response = $e->getResponse();
} }
@ -88,7 +92,7 @@ class WebHook extends Eloquent
/** /**
* Returns a human readable request type name. * Returns a human readable request type name.
* *
* @throws Exception * @throws \Exception
* *
* @return string HEAD, GET, POST, DELETE, PATCH, PUT etc * @return string HEAD, GET, POST, DELETE, PATCH, PUT etc
*/ */
@ -98,29 +102,19 @@ class WebHook extends Eloquent
switch ($this->request_type) { switch ($this->request_type) {
case self::HEAD: case self::HEAD:
$requestMethod = 'HEAD'; return 'HEAD';
break;
case self::GET: case self::GET:
$requestMethod = 'GET'; return 'GET';
break;
case self::POST: case self::POST:
$requestMethod = 'POST'; return 'POST';
break;
case self::PATCH: case self::PATCH:
$requestMethod = 'PATCH'; return 'PATCH';
break;
case self::PUT: case self::PUT:
$requestMethod = 'PUT'; return 'PUT';
break;
case self::DELETE: case self::DELETE:
$requestMethod = 'DELETE'; return 'DELETE';
break;
default: default:
throw new Exception('Unknown request type value: '.$this->request_type); throw new Exception('Unknown request type value: '.$this->request_type);
break; }
}
return $requestMethod;
} }
} }

View File

@ -1,11 +1,13 @@
<?php <?php
class WebHookResponse extends Eloquent use Illuminate\Database\Eloquent\Model;
class WebHookResponse extends Model
{ {
/** /**
* Returns the hook that a response belongs to. * Returns the hook that a response belongs to.
* *
* @return Illuminate\Database\Eloquent\Relations\BelongsTo * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/ */
public function hook() public function hook()
{ {