From f662352f669d2a0f89ed9427a3129addc96c39ee Mon Sep 17 00:00:00 2001 From: Thomas Coleman Date: Thu, 27 Nov 2014 16:04:28 +0000 Subject: [PATCH 1/4] [Contributing] Added class coding standards --- CONTRIBUTING.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4199be96..e0587f80 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,6 +8,22 @@ It's dead simple, use the issue tracker. Be descriptive, remember this is a comm Please follow existing coding standards: +```php + Date: Thu, 27 Nov 2014 16:05:00 +0000 Subject: [PATCH 2/4] [Standards] Updated Codebase to reflect new coding standards. See CONTRIBUTING.md for standards. --- app/config/heroku/database.php | 34 ++-- app/controllers/AuthController.php | 40 +++-- app/controllers/DashboardController.php | 18 +- app/controllers/HomeController.php | 20 ++- app/controllers/RSSController.php | 48 ++--- app/controllers/SetupController.php | 82 ++++----- app/filters.php | 108 ++++++------ app/filters/CORSFilter.php | 14 +- app/filters/NoSetupFilter.php | 24 +-- app/lang/en/component.php | 16 +- app/lang/en/incident.php | 16 +- app/lang/en/overview.php | 8 +- app/models/Component.php | 66 +++---- app/models/Incident.php | 100 ++++++----- app/models/IncidentTemplate.php | 32 ++-- app/models/Metric.php | 52 +++--- app/models/MetricPoint.php | 6 +- app/models/Service.php | 32 ++-- app/models/Setting.php | 76 ++++---- app/models/Subscriber.php | 22 +-- app/models/User.php | 40 +++-- app/models/WebHook.php | 206 +++++++++++----------- app/models/WebHookResponse.php | 12 +- app/routes.php | 36 ++-- app/routes/api.php | 34 ++-- app/routes/app.php | 34 ++-- app/tests/ExampleTest.php | 5 +- app/tests/TestCase.php | 5 +- app/transformers/ComponentTransformer.php | 30 ++-- app/view-composers.php | 36 ++-- 30 files changed, 646 insertions(+), 606 deletions(-) diff --git a/app/config/heroku/database.php b/app/config/heroku/database.php index 7bfb5409..41c73981 100644 --- a/app/config/heroku/database.php +++ b/app/config/heroku/database.php @@ -1,20 +1,20 @@ 'cleardb', - 'connections' => array( - 'cleardb' => array( - 'driver' => 'mysql', - 'host' => $dbURL['host'], - 'database' => $dbName, - 'username' => $dbURL['user'], - 'password' => $dbURL['pass'], - 'charset' => 'utf8', - 'collation' => 'utf8_unicode_ci', - 'prefix' => '', - ), - ) - ); +return array( + 'default' => 'cleardb', + 'connections' => array( + 'cleardb' => array( + 'driver' => 'mysql', + 'host' => $dbURL['host'], + 'database' => $dbName, + 'username' => $dbURL['user'], + 'password' => $dbURL['pass'], + 'charset' => 'utf8', + 'collation' => 'utf8_unicode_ci', + 'prefix' => '', + ), + ) +); diff --git a/app/controllers/AuthController.php b/app/controllers/AuthController.php index dae85ffa..67198530 100644 --- a/app/controllers/AuthController.php +++ b/app/controllers/AuthController.php @@ -1,24 +1,26 @@ withInput(Input::except('password'))->with('error', 'Invalid email or password'); - } - } +/** + * Logs users into their account + */ +class AuthController extends Controller { + public function showLogin() { + return View::make('auth.login'); + } - public function logoutAction() { - Auth::logout(); + public function postLogin() { + if (Auth::attempt(Input::only(['email', 'password']))) { + return Redirect::intended('dashboard'); + } else { + return Redirect::back()->withInput(Input::except('password'))->with('error', 'Invalid email or password'); + } + } - return Redirect::to('/'); - } - } + public function logoutAction() { + Auth::logout(); + + return Redirect::to('/'); + } +} \ No newline at end of file diff --git a/app/controllers/DashboardController.php b/app/controllers/DashboardController.php index d5ff6d30..3f561ce4 100644 --- a/app/controllers/DashboardController.php +++ b/app/controllers/DashboardController.php @@ -1,10 +1,12 @@ Component::all()]); - } - } +namespace CachetHQ\Cachet\Controllers; + +class HomeController extends Controller { + /** + * Returns the rendered Blade templates. + * @return View + */ + public function showIndex() { + return View::make('index', ['components' => Component::all()]); + } +} \ No newline at end of file diff --git a/app/controllers/RSSController.php b/app/controllers/RSSController.php index 7098cf3f..a9372ace 100644 --- a/app/controllers/RSSController.php +++ b/app/controllers/RSSController.php @@ -1,27 +1,29 @@ channel([ - 'title' => Setting::get('app_name'), - 'description' => 'Status Feed', - 'link' => Setting::get('app_domain'), - ]); +namespace CachetHQ\Cachet\Controllers; - Incident::get()->map(function($incident) use ($feed) { - $feed->item([ - 'title' => $incident->name, - 'message' => $incident->message, - 'component' => $incident->parent->name, - 'status' => $incident->humanStatus, - 'created_at' => $incident->created_at, - 'updated_at' => $incident->updated_at - ]); - }); +class RSSController extends Controller { + public function feedAction() { + $feed = RSS::feed('2.0', 'UTF-8'); + $feed->channel([ + 'title' => Setting::get('app_name'), + 'description' => 'Status Feed', + 'link' => Setting::get('app_domain'), + ]); - return Response::make($feed, 200, [ - 'Content-Type' => 'text/xml' - ]); - } - } + Incident::get()->map(function($incident) use ($feed) { + $feed->item([ + 'title' => $incident->name, + 'message' => $incident->message, + 'component' => $incident->parent->name, + 'status' => $incident->humanStatus, + 'created_at' => $incident->created_at, + 'updated_at' => $incident->updated_at + ]); + }); + + return Response::make($feed, 200, [ + 'Content-Type' => 'text/xml' + ]); + } +} \ No newline at end of file diff --git a/app/controllers/SetupController.php b/app/controllers/SetupController.php index bf0f16d5..ce01f132 100644 --- a/app/controllers/SetupController.php +++ b/app/controllers/SetupController.php @@ -1,48 +1,50 @@ with([ - 'pageTitle' => 'Setup' - ]); - } +namespace CachetHQ\Cachet\Controllers; - public function setupCachet() { - $postData = Input::get(); - $v = Validator::make($postData, [ - 'settings.app_name' => 'required', - 'settings.app_domain' => 'url|required', - 'settings.show_support' => 'boolean', - 'user.name'=> 'alpha_dash|required', - 'user.email' => 'email|required', - 'user.password' => 'required' - ]); +class SetupController extends Controller { + public function showSetup() { + return View::make('setup')->with([ + 'pageTitle' => 'Setup' + ]); + } - if ($v->passes()) { - // Pull the user details out. - $userDetails = array_get($postData, 'user'); - unset($postData['user']); + public function setupCachet() { + $postData = Input::get(); + $v = Validator::make($postData, [ + 'settings.app_name' => 'required', + 'settings.app_domain' => 'url|required', + 'settings.show_support' => 'boolean', + 'user.name' => 'alpha_dash|required', + 'user.email' => 'email|required', + 'user.password' => 'required' + ]); - $user = new User; - $user->username = $userDetails['name']; - $user->email = $userDetails['email']; - $user->password = Hash::make($userDetails['password']); - $user->save(); + if ($v->passes()) { + // Pull the user details out. + $userDetails = array_get($postData, 'user'); + unset($postData['user']); - Auth::login($user); + $user = new User; + $user->username = $userDetails['name']; + $user->email = $userDetails['email']; + $user->password = Hash::make($userDetails['password']); + $user->save(); - // Create the settings, boi. - foreach (array_get($postData, 'settings') as $settingName => $settingValue) { - $setting = new Setting; - $setting->name = $settingName; - $setting->value = $settingValue; - $setting->save(); - } + Auth::login($user); - return Redirect::to('/'); - } else { - // No good, let's try that again. - return Redirect::back()->withInput()->with('errors', $v->messages()); - } - } - } + // Create the settings, boi. + foreach (array_get($postData, 'settings') as $settingName => $settingValue) { + $setting = new Setting; + $setting->name = $settingName; + $setting->value = $settingValue; + $setting->save(); + } + + return Redirect::to('/'); + } else { + // No good, let's try that again. + return Redirect::back()->withInput()->with('errors', $v->messages()); + } + } +} \ No newline at end of file diff --git a/app/filters.php b/app/filters.php index b51823e1..8f228a98 100644 --- a/app/filters.php +++ b/app/filters.php @@ -1,64 +1,64 @@ headers->set('Access-Control-Allow-Origin', '*'); - return $response; - } - } +namespace CachetHQ\Cachet\Filters; + +class CORSFilter { + public function filter($route, $request, $response) { + $response->headers->set('Access-Control-Allow-Origin', '*'); + return $response; + } +} diff --git a/app/filters/NoSetupFilter.php b/app/filters/NoSetupFilter.php index ef712c5a..40ded65a 100644 --- a/app/filters/NoSetupFilter.php +++ b/app/filters/NoSetupFilter.php @@ -1,13 +1,15 @@ first(); - if ($setting->value) { - return Response::make('Unauthorized', 401); - } - } catch (Exception $e) { - } - } - } +namespace CachetHQ\Cachet\Filters; + +class NoSetupFilter { + public function filter($route, $request, $settingName) { + try { + $setting = Setting::where('name', $settingName)->first(); + if ($setting->value) { + return Response::make('Unauthorized', 401); + } + } catch (Exception $e) { + } + } +} \ No newline at end of file diff --git a/app/lang/en/component.php b/app/lang/en/component.php index 634fe05f..8ca3b927 100644 --- a/app/lang/en/component.php +++ b/app/lang/en/component.php @@ -1,10 +1,10 @@ array( - 1 => 'Operational', - 2 => 'Performance Issues', - 3 => 'Partial Outage', - 4 => 'Major Outage' - ) - ); +return array( + 'status' => array( + 1 => 'Operational', + 2 => 'Performance Issues', + 3 => 'Partial Outage', + 4 => 'Major Outage' + ) +); diff --git a/app/lang/en/incident.php b/app/lang/en/incident.php index dd83d715..0f3ec6c6 100644 --- a/app/lang/en/incident.php +++ b/app/lang/en/incident.php @@ -1,10 +1,10 @@ array( - 1 => 'Investigating', - 2 => 'Identified', - 3 => 'Watching', - 4 => 'Fixed' - ) - ); +return array( + 'status' => array( + 1 => 'Investigating', + 2 => 'Identified', + 3 => 'Watching', + 4 => 'Fixed' + ) +); \ No newline at end of file diff --git a/app/lang/en/overview.php b/app/lang/en/overview.php index 7475916f..b5f8cb9a 100644 --- a/app/lang/en/overview.php +++ b/app/lang/en/overview.php @@ -1,6 +1,6 @@ 'All systems are functional.', - 'bad' => 'Some systems are experiencing issues.', - ); +return array( + 'good' => 'All systems are functional.', + 'bad' => 'Some systems are experiencing issues.', +); \ No newline at end of file diff --git a/app/models/Component.php b/app/models/Component.php index a70306d1..6b04461d 100644 --- a/app/models/Component.php +++ b/app/models/Component.php @@ -1,40 +1,42 @@ 'required|integer', - 'name' => 'required', - 'status' => 'required|integer' - ]; +class Component extends Eloquent implements \Dingo\Api\Transformer\TransformableInterface { + use ValidatingTrait; - protected $fillable = ['name', 'description', 'status']; + protected $rules = [ + 'user_id' => 'required|integer', + 'name' => 'required', + 'status' => 'required|integer' + ]; - /** - * Lookup all of the incidents reported on the component. - * @return Illuminate\Database\Eloquent\Relations - */ - public function incidents() { - return $this->hasMany('Incident', 'component', 'id'); - } + protected $fillable = ['name', 'description', 'status']; - /** - * Looks up the human readable version of the status. - * @return string - */ - public function getHumanStatusAttribute() { - return Lang::get('component.status.' . $this->status); - } + /** + * Lookup all of the incidents reported on the component. + * @return Illuminate\Database\Eloquent\Relations + */ + public function incidents() { + return $this->hasMany('Incident', 'component', 'id'); + } - /** - * Get the transformer instance. - * - * @return ComponentTransformer - */ - public function getTransformer() { - return new ComponentTransformer(); - } - } + /** + * Looks up the human readable version of the status. + * @return string + */ + public function getHumanStatusAttribute() { + return Lang::get('component.status.' . $this->status); + } + + /** + * Get the transformer instance. + * + * @return ComponentTransformer + */ + public function getTransformer() { + return new ComponentTransformer(); + } +} \ No newline at end of file diff --git a/app/models/Incident.php b/app/models/Incident.php index ea35461f..a392d295 100644 --- a/app/models/Incident.php +++ b/app/models/Incident.php @@ -1,59 +1,61 @@ 'required|integer', - 'component' => 'required|integer', - 'name' => 'required', - 'status' => 'required|integer', - 'message' => 'required', - ]; +class Incident extends Eloquent implements \Dingo\Api\Transformer\TransformableInterface { + use ValidatingTrait; + use Illuminate\Database\Eloquent\SoftDeletingTrait; - protected $fillable = ['component', 'name', 'status', 'message']; + protected $rules = [ + 'user_id' => 'required|integer', + 'component' => 'required|integer', + 'name' => 'required', + 'status' => 'required|integer', + 'message' => 'required', + ]; - protected $appends = ['humanStatus']; + protected $fillable = ['component', 'name', 'status', 'message']; - /** - * An incident belongs to a component. - * @return Illuminate\Database\Eloquent\Relations\BelongsTo - */ - public function parent() { - return $this->belongsTo('Component', 'component', 'id'); - } + protected $appends = ['humanStatus']; - /** - * Returns a human readable version of the status. - * @return string - */ - public function getHumanStatusAttribute() { - $statuses = Lang::get('incident.status'); - return $statuses[$this->status]; - } + /** + * An incident belongs to a component. + * @return Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function parent() { + return $this->belongsTo('Component', 'component', 'id'); + } - /** - * Finds the icon to use for each status. - * @return string - */ - public function getIconAttribute() { - switch ($this->status) { - case 1: return 'glyphicon-flag'; - case 2: return 'glyphicon-warning-sign'; - case 3: return 'glyphicon-eye-open'; - case 4: return 'glyphicon-ok'; - } - } + /** + * Returns a human readable version of the status. + * @return string + */ + public function getHumanStatusAttribute() { + $statuses = Lang::get('incident.status'); + return $statuses[$this->status]; + } - /** - * Get the transformer instance. - * - * @return IncidentTransformer - */ - public function getTransformer() { - return new IncidentTransformer(); - } - } + /** + * Finds the icon to use for each status. + * @return string + */ + public function getIconAttribute() { + switch ($this->status) { + case 1: return 'glyphicon-flag'; + case 2: return 'glyphicon-warning-sign'; + case 3: return 'glyphicon-eye-open'; + case 4: return 'glyphicon-ok'; + } + } + + /** + * Get the transformer instance. + * + * @return IncidentTransformer + */ + public function getTransformer() { + return new IncidentTransformer(); + } +} \ No newline at end of file diff --git a/app/models/IncidentTemplate.php b/app/models/IncidentTemplate.php index 07309bfc..7ce2722f 100644 --- a/app/models/IncidentTemplate.php +++ b/app/models/IncidentTemplate.php @@ -1,21 +1,23 @@ 'alpha|required', - 'slug' => 'alpha_dash|required', - 'template' => 'required' - ]; +class IncidentTemplate extends Eloquent { + use ValidatingTrait; - public static function boot() { - parent::boot(); + protected $rules = [ + 'name' => 'alpha|required', + 'slug' => 'alpha_dash|required', + 'template' => 'required' + ]; - self::on('saving', function($template) { - $template->slug = Str::slug($template->name); - }); - } - } + public static function boot() { + parent::boot(); + + self::on('saving', function($template) { + $template->slug = Str::slug($template->name); + }); + } +} \ No newline at end of file diff --git a/app/models/Metric.php b/app/models/Metric.php index 38c71227..b6781b21 100644 --- a/app/models/Metric.php +++ b/app/models/Metric.php @@ -1,32 +1,34 @@ 'required', - 'suffix' => 'required', - 'display_chart' => 'boolean', - ]; +class Metric extends Eloquent implements \Dingo\Api\Transformer\TransformableInterface { + use ValidatingTrait; - protected $fillable = ['name', 'suffix', 'description', 'display_chart']; + protected $rules = [ + 'name' => 'required', + 'suffix' => 'required', + 'display_chart' => 'boolean', + ]; - /** - * Determines whether a chart should be shown. - * @return bool - */ - public function getShouldDisplayAttribute() { - return $this->display_chart === 1; - } + protected $fillable = ['name', 'suffix', 'description', 'display_chart']; - /** - * Get the transformer instance. - * - * @return ComponentTransformer - */ - public function getTransformer() { - return new MetricTransformer(); - } - } + /** + * Determines whether a chart should be shown. + * @return bool + */ + public function getShouldDisplayAttribute() { + return $this->display_chart === 1; + } + + /** + * Get the transformer instance. + * + * @return ComponentTransformer + */ + public function getTransformer() { + return new MetricTransformer(); + } +} \ No newline at end of file diff --git a/app/models/MetricPoint.php b/app/models/MetricPoint.php index 30b50f8d..d2f315e2 100644 --- a/app/models/MetricPoint.php +++ b/app/models/MetricPoint.php @@ -1,5 +1,7 @@ 'alpha_dash|required', - 'active' => 'required|in:0,1', - 'properties' => '' - ]; +class Service extends Eloquent { + use ValidatingTrait; - public function getPropertiesAttribute($properties) { - return json_decode($properties); - } + protected $rules = [ + 'type' => 'alpha_dash|required', + 'active' => 'required|in:0,1', + 'properties' => '' + ]; - public function setPropertiesAttribute($properties) { - $this->attributes['properties'] = json_encode($properties); - } - } + public function getPropertiesAttribute($properties) { + return json_decode($properties); + } + + public function setPropertiesAttribute($properties) { + $this->attributes['properties'] = json_encode($properties); + } +} \ No newline at end of file diff --git a/app/models/Setting.php b/app/models/Setting.php index beaaa843..4204bd7f 100644 --- a/app/models/Setting.php +++ b/app/models/Setting.php @@ -1,42 +1,44 @@ 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; - } - } else { - return $setting; - } - } +class Setting extends Eloquent { + /** + * Returns a setting from the database. + * @param string $settingName + * @param bool $checkEnv + * @return string + */ + public static function get($settingName, $checkEnv = true) { + // Default setting value. + $setting = null; - return $setting; - } + // 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; + } + } else { + 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) - ); - } - } + 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) + ); + } +} \ No newline at end of file diff --git a/app/models/Subscriber.php b/app/models/Subscriber.php index d64a0745..2e0c562d 100644 --- a/app/models/Subscriber.php +++ b/app/models/Subscriber.php @@ -1,15 +1,17 @@ 'required|email' - ]; +class Subscriber extends Eloquent { + use ValidatingTrait; + use SoftDeletingTrait; - protected $fillable = ['email']; - } + protected $rules = [ + 'email' => 'required|email' + ]; + + protected $fillable = ['email']; +} \ No newline at end of file diff --git a/app/models/User.php b/app/models/User.php index b07bfbba..14573e0d 100644 --- a/app/models/User.php +++ b/app/models/User.php @@ -1,26 +1,28 @@ hasMany('WebHookContent', 'hook_id', 'id'); - } +class WebHook extends Eloquent { + // Request Methods. + const HEAD = 0; + const GET = 1; + const POST = 2; + const PATCH = 3; + const PUT = 4; + const DELETE = 5; - /** - * Returns all active hooks. - * - * @param Illuminate\Database\Eloquent\Builder $query - * @return Illuminate\Database\Eloquent\Builder - */ - public function scopeActive($query) { - return $query->where('active', 1); - } + /** + * Returns all responses for a WebHook. + * + * @return Illuminate\Database\Eloquent\Builder + */ + public function response() { + return $this->hasMany('WebHookContent', 'hook_id', 'id'); + } - /** - * Setups a Ping event that is fired upon a web hook. - * - * @return array result of the ping - */ - public function ping() { - return $this->fire('ping', 'Coming live to you from Cachet.'); - } + /** + * Returns all active hooks. + * + * @param Illuminate\Database\Eloquent\Builder $query + * @return Illuminate\Database\Eloquent\Builder + */ + public function scopeActive($query) { + return $query->where('active', 1); + } - /** - * 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 object - */ - public function fire($eventType, $data = null) { - $startTime = microtime(true); + /** + * Setups a Ping event that is fired upon a web hook. + * + * @return array result of the ping + */ + public function ping() { + return $this->fire('ping', 'Coming live to you from Cachet.'); + } - $client = new \GuzzleHttp\Client(); - $request = $client->createRequest($this->requestMethod, $this->endpoint, [ - 'headers' => [ - 'X-Cachet-Event' => $eventType, - ], - 'body' => $data - ]); + /** + * 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 object + */ + public function fire($eventType, $data = null) { + $startTime = microtime(true); - try { - $response = $client->send($request); - } catch (\GuzzleHttp\Exception\ClientException $e) { - // Do nothing with the exception, we want it. - $response = $e->getResponse(); - } + $client = new \GuzzleHttp\Client(); + $request = $client->createRequest($this->requestMethod, $this->endpoint, [ + 'headers' => [ + 'X-Cachet-Event' => $eventType, + ], + 'body' => $data + ]); - $timeTaken = microtime(TRUE) - $startTime; + try { + $response = $client->send($request); + } catch (\GuzzleHttp\Exception\ClientException $e) { + // Do nothing with the exception, we want it. + $response = $e->getResponse(); + } - // 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(); + $timeTaken = microtime(TRUE) - $startTime; - return $hookResponse; - } + // 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(); - /** - * Returns a human readable request type name. - * - * @throws Exception - * @return string HEAD, GET, POST, DELETE, PATCH, PUT etc - */ - public function getRequestMethodAttribute() { - $requestMethod = NULL; + return $hookResponse; + } - switch ($this->request_type) { - case self::HEAD: - $requestMethod = 'HEAD'; - break; - case self::GET: - $requestMethod = 'GET'; - break; - case self::POST: - $requestMethod = 'POST'; - break; - case self::PATCH: - $requestMethod = 'PATCH'; - break; - case self::PUT: - $requestMethod = 'PUT'; - break; - case self::DELETE: - $requestMethod = 'DELETE'; - break; + /** + * Returns a human readable request type name. + * + * @throws Exception + * @return string HEAD, GET, POST, DELETE, PATCH, PUT etc + */ + public function getRequestMethodAttribute() { + $requestMethod = NULL; - default: - throw new Exception('Unknown request type value: ' . $this->request_type); - break; - } + switch ($this->request_type) { + case self::HEAD: + $requestMethod = 'HEAD'; + break; + case self::GET: + $requestMethod = 'GET'; + break; + case self::POST: + $requestMethod = 'POST'; + break; + case self::PATCH: + $requestMethod = 'PATCH'; + break; + case self::PUT: + $requestMethod = 'PUT'; + break; + case self::DELETE: + $requestMethod = 'DELETE'; + break; - return $requestMethod; - } - } + default: + throw new Exception('Unknown request type value: ' . $this->request_type); + break; + } + + return $requestMethod; + } +} \ No newline at end of file diff --git a/app/models/WebHookResponse.php b/app/models/WebHookResponse.php index 9888b296..cbf3469f 100644 --- a/app/models/WebHookResponse.php +++ b/app/models/WebHookResponse.php @@ -1,7 +1,9 @@ belongsTo('WebHook', 'id', 'hook_id'); - } - } +namespace CachetHQ\Cachet\Models; + +class WebHookResponse extends Eloquent { + public function hook() { + return $this->belongsTo('WebHook', 'id', 'hook_id'); + } +} \ No newline at end of file diff --git a/app/routes.php b/app/routes.php index 274c13f3..2dfbd51c 100644 --- a/app/routes.php +++ b/app/routes.php @@ -1,24 +1,24 @@ setFlags(RecursiveDirectoryIterator::SKIP_DOTS); +function routesInDirectory($app = '') { + $routeDir = app_path('routes/' . $app . ($app !== '' ? '/' : null)); + $iterator = new RecursiveDirectoryIterator($routeDir); + $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS); - foreach ($iterator as $route) { - $isDotFile = strpos($route->getFilename(), '.') === 0; + foreach ($iterator as $route) { + $isDotFile = strpos($route->getFilename(), '.') === 0; - if (!$isDotFile && !$route->isDir()) { - require $routeDir . $route->getFilename(); - } - } - } + if (!$isDotFile && !$route->isDir()) { + require $routeDir . $route->getFilename(); + } + } +} diff --git a/app/routes/api.php b/app/routes/api.php index e7db1ce6..23186dc5 100644 --- a/app/routes/api.php +++ b/app/routes/api.php @@ -1,23 +1,23 @@ 'v1', 'prefix' => 'api', 'namespace' => 'CachetHQ\Cachet\Controllers\Api'], function() { +Route::api(['version' => 'v1', 'prefix' => 'api', 'namespace' => 'CachetHQ\Cachet\Controllers\Api'], function() { - Route::get('components', 'ComponentController@getComponents'); - Route::get('components/{id}', 'ComponentController@getComponent'); - Route::get('components/{id}/incidents', 'ComponentController@getComponentIncidents'); - Route::get('incidents', 'IncidentController@getIncidents'); - Route::get('incidents/{id}', 'IncidentController@getIncident'); - Route::get('metrics', 'MetricController@getMetrics'); - Route::get('metrics/{id}', 'MetricController@getMetric'); + Route::get('components', 'ComponentController@getComponents'); + Route::get('components/{id}', 'ComponentController@getComponent'); + Route::get('components/{id}/incidents', 'ComponentController@getComponentIncidents'); + Route::get('incidents', 'IncidentController@getIncidents'); + Route::get('incidents/{id}', 'IncidentController@getIncident'); + Route::get('metrics', 'MetricController@getMetrics'); + Route::get('metrics/{id}', 'MetricController@getMetric'); - Route::group(['protected' => true], function() { - Route::post('components', 'ComponentController@postComponents'); - Route::post('incidents', 'IncidentController@postIncidents'); - Route::post('metrics', 'MetricController@postMetrics'); + Route::group(['protected' => true], function() { + Route::post('components', 'ComponentController@postComponents'); + Route::post('incidents', 'IncidentController@postIncidents'); + Route::post('metrics', 'MetricController@postMetrics'); - Route::put('components/{id}', 'ComponentController@putComponent'); - Route::put('incidents/{id}', 'IncidentController@putIncident'); - Route::put('metrics/{id}', 'MetricController@putMetric'); - }); + Route::put('components/{id}', 'ComponentController@putComponent'); + Route::put('incidents/{id}', 'IncidentController@putIncident'); + Route::put('metrics/{id}', 'MetricController@putMetric'); + }); - }); +}); \ No newline at end of file diff --git a/app/routes/app.php b/app/routes/app.php index f0b99861..695538d4 100644 --- a/app/routes/app.php +++ b/app/routes/app.php @@ -1,24 +1,24 @@ 'no_setup:app_name'], function() { - Route::get('/setup', 'SetupController@showSetup'); - Route::group(['before' => 'csrf'], function() { - Route::post('/setup', 'SetupController@setupCachet'); - }); - }); +Route::group(['before' => 'no_setup:app_name'], function() { + Route::get('/setup', 'SetupController@showSetup'); + Route::group(['before' => 'csrf'], function() { + Route::post('/setup', 'SetupController@setupCachet'); + }); +}); - Route::get('/auth/login', 'AuthController@showLogin')->before('guest'); - Route::post('/auth/login', 'AuthController@postLogin')->before('guest|csrf'); +Route::get('/auth/login', 'AuthController@showLogin')->before('guest'); +Route::post('/auth/login', 'AuthController@postLogin')->before('guest|csrf'); - Route::group(['before' => 'auth'], function() { - // Dashboard/Management Panel etc. - Route::get('/dashboard', 'DashboardController@showDashboard'); +Route::group(['before' => 'auth'], function() { + // Dashboard/Management Panel etc. + Route::get('/dashboard', 'DashboardController@showDashboard'); - // Authorization stuff. - Route::get('/auth/logout', 'AuthController@logoutAction'); - }); + // Authorization stuff. + Route::get('/auth/logout', 'AuthController@logoutAction'); +}); - Route::get('/rss', 'RSSController@feedAction'); +Route::get('/rss', 'RSSController@feedAction'); \ No newline at end of file diff --git a/app/tests/ExampleTest.php b/app/tests/ExampleTest.php index 62387de1..3fd44a4e 100644 --- a/app/tests/ExampleTest.php +++ b/app/tests/ExampleTest.php @@ -1,5 +1,7 @@ assertTrue($this->client->getResponse()->isOk()); } - -} +} \ No newline at end of file diff --git a/app/tests/TestCase.php b/app/tests/TestCase.php index d367fe53..ebc42884 100644 --- a/app/tests/TestCase.php +++ b/app/tests/TestCase.php @@ -1,5 +1,7 @@ (int) $component->id, - 'name' => $component->name, - 'description' => $component->description, - 'status_id' => (int) $component->status, - 'status' => $component->humanStatus, - 'incident_count' => $component->incidents()->count(), - 'created_at' => $component->created_at->timestamp, - 'updated_at' => $component->updated_at->timestamp, - ]; - } - } +namespace CachetHQ\Cachet\Transformers; + +class ComponentTransformer extends \League\Fractal\TransformerAbstract { + public function transform(Component $component) { + return [ + 'id' => (int) $component->id, + 'name' => $component->name, + 'description' => $component->description, + 'status_id' => (int) $component->status, + 'status' => $component->humanStatus, + 'incident_count' => $component->incidents()->count(), + 'created_at' => $component->created_at->timestamp, + 'updated_at' => $component->updated_at->timestamp, + ]; + } +} \ No newline at end of file diff --git a/app/view-composers.php b/app/view-composers.php index 26d9488c..8128f007 100644 --- a/app/view-composers.php +++ b/app/view-composers.php @@ -1,24 +1,24 @@ groupBy('status') - ->orderBy('status', 'desc'); + $incidents = Incident::whereRaw('DATE(created_at) = "' . $date . '"') + ->groupBy('status') + ->orderBy('status', 'desc'); - $incidentCount = $incidents->count(); + $incidentCount = $incidents->count(); - if ($incidentCount <= 1 || ($incidentCount > 1 && (int) $incidents->first()->status === 4)) { - $status = 'success'; - $message = Lang::get('overview.good'); - } else { - $status = 'danger'; - $message = Lang::get('overview.bad'); - } + if ($incidentCount <= 1 || ($incidentCount > 1 && (int) $incidents->first()->status === 4)) { + $status = 'success'; + $message = Lang::get('overview.good'); + } else { + $status = 'danger'; + $message = Lang::get('overview.bad'); + } - $view->with([ - 'systemStatus' => $status, - 'systemMessage' => $message - ]); - }); + $view->with([ + 'systemStatus' => $status, + 'systemMessage' => $message + ]); +}); From ee65a462ef5f093f2cac2323be69e2b8c720b5d9 Mon Sep 17 00:00:00 2001 From: Thomas Coleman Date: Thu, 27 Nov 2014 16:33:18 +0000 Subject: [PATCH 3/4] [Namespacing] Removed namespaced added in, to be added in properly later --- app/controllers/AuthController.php | 2 -- app/controllers/DashboardController.php | 2 -- app/controllers/HomeController.php | 2 -- app/controllers/RSSController.php | 2 -- app/controllers/SetupController.php | 2 -- app/filters/CORSFilter.php | 2 -- app/filters/NoSetupFilter.php | 2 -- app/models/Component.php | 2 -- app/models/Incident.php | 2 -- app/models/IncidentTemplate.php | 2 -- app/models/Metric.php | 2 -- app/models/MetricPoint.php | 2 -- app/models/Service.php | 2 -- app/models/Setting.php | 2 -- app/models/Subscriber.php | 2 -- app/models/User.php | 2 -- app/models/WebHook.php | 2 -- app/models/WebHookResponse.php | 2 -- app/tests/ExampleTest.php | 2 -- app/tests/TestCase.php | 2 -- app/transformers/ComponentTransformer.php | 2 -- 21 files changed, 42 deletions(-) diff --git a/app/controllers/AuthController.php b/app/controllers/AuthController.php index 67198530..d7acf1fc 100644 --- a/app/controllers/AuthController.php +++ b/app/controllers/AuthController.php @@ -1,7 +1,5 @@ with([ diff --git a/app/filters/CORSFilter.php b/app/filters/CORSFilter.php index fb60e513..50f4bd07 100644 --- a/app/filters/CORSFilter.php +++ b/app/filters/CORSFilter.php @@ -1,7 +1,5 @@ headers->set('Access-Control-Allow-Origin', '*'); diff --git a/app/filters/NoSetupFilter.php b/app/filters/NoSetupFilter.php index 40ded65a..9763a877 100644 --- a/app/filters/NoSetupFilter.php +++ b/app/filters/NoSetupFilter.php @@ -1,7 +1,5 @@ belongsTo('WebHook', 'id', 'hook_id'); diff --git a/app/tests/ExampleTest.php b/app/tests/ExampleTest.php index 3fd44a4e..ad2aed05 100644 --- a/app/tests/ExampleTest.php +++ b/app/tests/ExampleTest.php @@ -1,7 +1,5 @@ Date: Thu, 27 Nov 2014 16:36:24 +0000 Subject: [PATCH 4/4] [Coding Standards] Applied new coding standards to CachetHQ\Cachet folder --- .../Controllers/Api/IncidentController.php | 154 +++++++++--------- .../Controllers/Api/MetricController.php | 134 +++++++-------- app/transformers/IncidentTransformer.php | 32 ++-- app/transformers/MetricTransformer.php | 26 +-- 4 files changed, 173 insertions(+), 173 deletions(-) diff --git a/app/CachetHq/Cachet/Controllers/Api/IncidentController.php b/app/CachetHq/Cachet/Controllers/Api/IncidentController.php index d6bebc26..b18bad0e 100644 --- a/app/CachetHq/Cachet/Controllers/Api/IncidentController.php +++ b/app/CachetHq/Cachet/Controllers/Api/IncidentController.php @@ -1,91 +1,91 @@ auth = $auth; - } + public function __construct(Shield $auth) { + $this->auth = $auth; + } - /** - * Get all incidents - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public function getIncidents() { - return Incident::all(); - } + /** + * Get all incidents + * + * @return \Illuminate\Database\Eloquent\Collection + */ + public function getIncidents() { + return Incident::all(); + } - /** - * Get a single incident - * - * @param int $id - * - * @return Incident - */ - public function getIncident($id) { - if ($incident = Incident::find($id)) { - return $incident; - } else { - App::abort(404, 'Incident not found'); - } - } + /** + * Get a single incident + * + * @param int $id + * + * @return Incident + */ + public function getIncident($id) { + if ($incident = Incident::find($id)) { + return $incident; + } else { + App::abort(404, 'Incident not found'); + } + } - /** - * Create a new incident - * - * @return Incident - */ - public function postIncidents() { - $incident = new Incident(Input::all()); - $incident->user_id = $this->auth->user()->id; - return $this->_saveIncident($incident); - } + /** + * Create a new incident + * + * @return Incident + */ + public function postIncidents() { + $incident = new Incident(Input::all()); + $incident->user_id = $this->auth->user()->id; + return $this->_saveIncident($incident); + } - /** - * Update an existing incident - * - * @param int $id - * - * @return Incident - */ - public function putIncident($id) { - $incident = $this->getIncident($id); + /** + * Update an existing incident + * + * @param int $id + * + * @return Incident + */ + public function putIncident($id) { + $incident = $this->getIncident($id); - $incident->fill(Input::all()); + $incident->fill(Input::all()); - return $this->_saveIncident($incident); - } + return $this->_saveIncident($incident); + } - /** - * Function for saving the incident, and returning appropriate error codes - * - * @param Incident $incident - * - * @return Incident - */ - private function _saveIncident($incident) { - if ($incident->isValid()) { - try { - $component = $incident->parent; - if (!$component) { - App::abort(400, 'Invalid component specified'); - } + /** + * Function for saving the incident, and returning appropriate error codes + * + * @param Incident $incident + * + * @return Incident + */ + private function _saveIncident($incident) { + if ($incident->isValid()) { + try { + $component = $incident->parent; + if (!$component) { + App::abort(400, 'Invalid component specified'); + } - $incident->saveOrFail(); - return $incident; - } catch (Exception $e) { - App::abort(500, $e->getMessage()); - } - } else { - App::abort(404, $incident->getErrors()->first()); - } - } - } + $incident->saveOrFail(); + return $incident; + } catch (Exception $e) { + App::abort(500, $e->getMessage()); + } + } else { + App::abort(404, $incident->getErrors()->first()); + } + } +} \ No newline at end of file diff --git a/app/CachetHq/Cachet/Controllers/Api/MetricController.php b/app/CachetHq/Cachet/Controllers/Api/MetricController.php index 6a7a4b69..29f2a593 100644 --- a/app/CachetHq/Cachet/Controllers/Api/MetricController.php +++ b/app/CachetHq/Cachet/Controllers/Api/MetricController.php @@ -1,76 +1,76 @@ _saveMetric($metric); - } + /** + * Create a new metric + * + * @return Metric + */ + public function postMetrics() { + $metric = new Metric(Input::all()); + return $this->_saveMetric($metric); + } - /** - * Update an existing metric - * - * @param int $id - * - * @return Metric - */ - public function putMetric($id) { - $metric = $this->getMetric($id); - $metric->fill(Input::all()); - return $this->_saveMetric($metric); - } + /** + * Update an existing metric + * + * @param int $id + * + * @return Metric + */ + public function putMetric($id) { + $metric = $this->getMetric($id); + $metric->fill(Input::all()); + return $this->_saveMetric($metric); + } - /** - * Function for saving a metric, and returning appropriate error codes - * - * @param Metric $metric - * - * @return Metric - */ - private function _saveMetric($metric) { - if ($metric->isValid()) { - try { - $metric->saveOrFail(); - return $metric; - } catch (Exception $e) { - App::abort(500, $e->getMessage()); - } - } else { - App::abort(404, $metric->getErrors()->first()); - } - } - } + /** + * Function for saving a metric, and returning appropriate error codes + * + * @param Metric $metric + * + * @return Metric + */ + private function _saveMetric($metric) { + if ($metric->isValid()) { + try { + $metric->saveOrFail(); + return $metric; + } catch (Exception $e) { + App::abort(500, $e->getMessage()); + } + } else { + App::abort(404, $metric->getErrors()->first()); + } + } +} \ No newline at end of file diff --git a/app/transformers/IncidentTransformer.php b/app/transformers/IncidentTransformer.php index ab1f6b5f..5fbc46bf 100644 --- a/app/transformers/IncidentTransformer.php +++ b/app/transformers/IncidentTransformer.php @@ -1,19 +1,19 @@ parent; - $transformer = $component->getTransformer(); +class IncidentTransformer extends \League\Fractal\TransformerAbstract { + public function transform(Incident $incident) { + $component = $incident->parent; + $transformer = $component->getTransformer(); - return [ - 'id' => (int) $incident->id, - 'name' => $incident->name, - 'message' => $incident->message, - 'status_id' => (int) $incident->status, - 'status' => $incident->getHumanStatusAttribute(), - 'component' => $transformer->transform($component), - 'created_at' => $incident->created_at->timestamp, - 'updated_at' => $incident->updated_at->timestamp, - ]; - } - } + return [ + 'id' => (int) $incident->id, + 'name' => $incident->name, + 'message' => $incident->message, + 'status_id' => (int) $incident->status, + 'status' => $incident->getHumanStatusAttribute(), + 'component' => $transformer->transform($component), + 'created_at' => $incident->created_at->timestamp, + 'updated_at' => $incident->updated_at->timestamp, + ]; + } +} \ No newline at end of file diff --git a/app/transformers/MetricTransformer.php b/app/transformers/MetricTransformer.php index d1f2411a..3e8845bb 100644 --- a/app/transformers/MetricTransformer.php +++ b/app/transformers/MetricTransformer.php @@ -1,15 +1,15 @@ (int) $component->id, - 'name' => $component->name, - 'description' => $component->description, - 'suffix' => $component->suffix, - 'display' => $component->shouldDisplay, - 'created_at' => $component->created_at->timestamp, - 'updated_at' => $component->updated_at->timestamp, - ]; - } - } +class MetricTransformer extends \League\Fractal\TransformerAbstract { + public function transform(Metric $metric) { + return [ + 'id' => (int) $component->id, + 'name' => $component->name, + 'description' => $component->description, + 'suffix' => $component->suffix, + 'display' => $component->shouldDisplay, + 'created_at' => $component->created_at->timestamp, + 'updated_at' => $component->updated_at->timestamp, + ]; + } +} \ No newline at end of file