From 08360124ffd57826d1cc783ff7d884a3905aab10 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Wed, 26 Nov 2014 23:20:31 +0000 Subject: [PATCH 1/3] Make CachetHq acually be CachetHQ. Fix formatting. --- .../Controllers/Api/ComponentController.php | 102 +++++----- .../Controllers/Api/IncidentController.php | 112 +++++------ .../Controllers/Api/MetricController.php | 12 +- .../Component/ComponentRepository.php | 16 +- .../Component/EloquentComponentRepository.php | 38 ++-- .../Repositories/EloquentRepository.php | 174 +++++++++--------- .../RepositoryServiceProvider.php | 15 +- app/config/app.php | 2 +- app/routes/api.php | 2 +- 9 files changed, 242 insertions(+), 231 deletions(-) diff --git a/app/CachetHq/Cachet/Controllers/Api/ComponentController.php b/app/CachetHq/Cachet/Controllers/Api/ComponentController.php index 3f3c8cccb079..b4efca0839cb 100644 --- a/app/CachetHq/Cachet/Controllers/Api/ComponentController.php +++ b/app/CachetHq/Cachet/Controllers/Api/ComponentController.php @@ -1,51 +1,53 @@ -auth = $auth; - $this->component = $component; - } - - /** - * Get all components - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public function getComponents() { - return $this->component->all(); - } - - /** - * Get a single component - * - * @param int $id - * - * @return \Component - */ - public function getComponent($id) { - return $this->component->findOrFail($id); - } - - public function getComponentIncidents($id) { - return $this->component->with($id, ['incidents']); - } - - /** - * Create a new component - * - * @return \Component - */ - public function postComponents() { - return $this->component->create($this->auth->user()->id, Input::all()); +auth = $auth; + $this->component = $component; + } + + /** + * Get all components + * + * @return \Illuminate\Database\Eloquent\Collection + */ + public function getComponents() { + return $this->component->all(); + } + + /** + * Get a single component + * + * @param int $id + * + * @return \Component + */ + public function getComponent($id) { + return $this->component->findOrFail($id); + } + + public function getComponentIncidents($id) { + return $this->component->with($id, ['incidents']); + } + + /** + * Create a new component + * + * @return \Component + */ + public function postComponents() { + return $this->component->create($this->auth->user()->id, Input::all()); + } } -} diff --git a/app/CachetHq/Cachet/Controllers/Api/IncidentController.php b/app/CachetHq/Cachet/Controllers/Api/IncidentController.php index ccf353fa4344..d6bebc269ec3 100644 --- a/app/CachetHq/Cachet/Controllers/Api/IncidentController.php +++ b/app/CachetHq/Cachet/Controllers/Api/IncidentController.php @@ -1,68 +1,70 @@ -auth = $auth; - } - - /** - * Get all incidents - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public function getIncidents() { - return Incident::all(); - } + protected $auth; - /** - * 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'); + public function __construct(Shield $auth) { + $this->auth = $auth; } - } - /** - * 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); - } + /** + * Get all incidents + * + * @return \Illuminate\Database\Eloquent\Collection + */ + public function getIncidents() { + return Incident::all(); + } - /** - * Update an existing incident - * - * @param int $id - * - * @return Incident - */ - public function putIncident($id) { - $incident = $this->getIncident($id); + /** + * 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'); + } + } - $incident->fill(Input::all()); + /** + * 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); + } - return $this->_saveIncident($incident); - } + /** + * Update an existing incident + * + * @param int $id + * + * @return Incident + */ + public function putIncident($id) { + $incident = $this->getIncident($id); + + $incident->fill(Input::all()); - /** + return $this->_saveIncident($incident); + } + + /** * Function for saving the incident, and returning appropriate error codes * * @param Incident $incident @@ -86,4 +88,4 @@ private function _saveIncident($incident) { 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 9d62a0a315b0..6a7a4b69dfc9 100644 --- a/app/CachetHq/Cachet/Controllers/Api/MetricController.php +++ b/app/CachetHq/Cachet/Controllers/Api/MetricController.php @@ -1,9 +1,11 @@ -getErrors()->first()); } } -} \ No newline at end of file + } diff --git a/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php b/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php index cf02022d1ff4..5402a69c7047 100644 --- a/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php +++ b/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php @@ -1,12 +1,14 @@ -model = $model; - } - - public function create($user_id, array $array) { - $component = new $this->model($array); - $component->user_id = $user_id; + protected $model; - if ($component->isInvalid()) { - throw new Exception('Invalid model validation', $component->getErrors()); + public function __construct(Component $model) { + $this->model = $model; } - $component->saveOrFail(); - return $component; + public function create($user_id, array $array) { + $component = new $this->model($array); + $component->user_id = $user_id; + + if ($component->isInvalid()) { + throw new Exception('Invalid model validation', $component->getErrors()); + } + + $component->saveOrFail(); + return $component; + } } -} diff --git a/app/CachetHq/Cachet/Repositories/EloquentRepository.php b/app/CachetHq/Cachet/Repositories/EloquentRepository.php index 9d0187373715..03645ab5180c 100644 --- a/app/CachetHq/Cachet/Repositories/EloquentRepository.php +++ b/app/CachetHq/Cachet/Repositories/EloquentRepository.php @@ -1,101 +1,103 @@ -model->all(); - } + abstract class EloquentRepository { - /** - * Returns an object with related relationships - * @param id $id - * @param array $with Array of model relationships - * @return object|ModelNotFoundException - */ - public function with($id, array $with = []) { - return $this->model->with($with)->findOrFail($id); - } + /** + * Returns all models + * @return object + */ + public function all() { + return $this->model->all(); + } - /** - * Sets the model to query against a user id - * @param integer $id - * @param string $column - * @return $this - */ - public function withAuth($id, $column = 'user_id') { - $this->model = $this->model->where($column, $id); - return $this; - } + /** + * Returns an object with related relationships + * @param id $id + * @param array $with Array of model relationships + * @return object|ModelNotFoundException + */ + public function with($id, array $with = []) { + return $this->model->with($with)->findOrFail($id); + } - /** - * Finds a model by ID - * @param int $id - * @return object - */ - public function find(int $id) { - return $this->model->find($id); - } + /** + * Sets the model to query against a user id + * @param integer $id + * @param string $column + * @return $this + */ + public function withAuth($id, $column = 'user_id') { + $this->model = $this->model->where($column, $id); + return $this; + } - /** - * Finds a model by ID - * @param integer $id - * @return object|ModelNotFoundException - */ - public function findOrFail($id) { - return $this->model->findOrFail($id); - } + /** + * Finds a model by ID + * @param int $id + * @return object + */ + public function find(int $id) { + return $this->model->find($id); + } - /** - * Finds a model by type - * @param string $key - * @param string $value - * @param array $columns - * @return object|ModelNotFoundException - */ - public function findByOrFail($key, $value, $columns = ['*']) { - if (! is_null($item = $this->model->where($key, $value)->first($columns))) { - return $item; + /** + * Finds a model by ID + * @param integer $id + * @return object|ModelNotFoundException + */ + public function findOrFail($id) { + return $this->model->findOrFail($id); } - throw new ModelNotFoundException; - } + /** + * Finds a model by type + * @param string $key + * @param string $value + * @param array $columns + * @return object|ModelNotFoundException + */ + public function findByOrFail($key, $value, $columns = ['*']) { + if (! is_null($item = $this->model->where($key, $value)->first($columns))) { + return $item; + } - /** - * Counts the number of rows returned - * @param string $key - * @param string $value - * @return integer - */ - public function count($key = null, $value = null) { - if (is_null($key) || is_null($value)) { - return $this->model->where($key, $value)->count(); + throw new ModelNotFoundException; } - return $this->model->count(); - } - - /** - * Deletes a model by ID - * @param inetegr $id - */ - public function destroy($id) { - $this->model->delete($id); - } + /** + * Counts the number of rows returned + * @param string $key + * @param string $value + * @return integer + */ + public function count($key = null, $value = null) { + if (is_null($key) || is_null($value)) { + return $this->model->where($key, $value)->count(); + } + + return $this->model->count(); + } + + /** + * Deletes a model by ID + * @param inetegr $id + */ + public function destroy($id) { + $this->model->delete($id); + } - /** - * Updates a given model by ID with an array of updates - * @param inetegr $id - * @param array $array Key Value pairs to update - */ - public function update($id, array $array) { - $model = $this->model->whereId($id)->first(['id']); - $model->update($array); + /** + * Updates a given model by ID with an array of updates + * @param inetegr $id + * @param array $array Key Value pairs to update + */ + public function update($id, array $array) { + $model = $this->model->whereId($id)->first(['id']); + $model->update($array); + } } -} \ No newline at end of file diff --git a/app/CachetHq/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php b/app/CachetHq/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php index 3f772fdf120b..09e9949b4fe4 100644 --- a/app/CachetHq/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php +++ b/app/CachetHq/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php @@ -1,12 +1,11 @@ -app->bind('CachetHq\Cachet\Repositories\Component\ComponentRepository', 'CachetHq\Cachet\Repositories\Component\EloquentComponentRepository'); + class RepositoryServiceProvider extends ServiceProvider { + public function register() { + $this->app->bind('CachetHQ\Cachet\Repositories\Component\ComponentRepository', 'CachetHQ\Cachet\Repositories\Component\EloquentComponentRepository'); + } } - -} \ No newline at end of file diff --git a/app/config/app.php b/app/config/app.php index 4e16e1e025f4..180026552918 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -124,7 +124,7 @@ 'Dingo\Api\ApiServiceProvider', - 'CachetHq\Cachet\Support\ServiceProviders\RepositoryServiceProvider', + 'CachetHQ\Cachet\Support\ServiceProviders\RepositoryServiceProvider', ), diff --git a/app/routes/api.php b/app/routes/api.php index 0249789171e1..e7db1ce69b22 100644 --- a/app/routes/api.php +++ b/app/routes/api.php @@ -1,6 +1,6 @@ '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'); From 01512e0bd4c8a64e8b490e4037577a2d16e40f73 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Wed, 26 Nov 2014 23:23:14 +0000 Subject: [PATCH 2/3] Fix namespace loading in composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index eaa655ac9eae..82678ddbefdf 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "app/filters" ], "psr-4": { - "CachetHq\\": "app/CachetHq" + "CachetHQ\\": "app/CachetHq" } }, "extra": { From 079a2cbd20d197f85b2876b6d9363a8f828b7455 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Wed, 26 Nov 2014 23:23:25 +0000 Subject: [PATCH 3/3] Update locked requirements --- composer.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.lock b/composer.lock index c4878e71c952..f653fc5ee455 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "e1b88e1dfc75394c68da25e3c80b39e5", + "hash": "c920eb10b4a8e258de23386a3b8b6172", "packages": [ { "name": "classpreloader/classpreloader",