diff --git a/app/CachetHq/Cachet/Controllers/Api/ComponentController.php b/app/CachetHq/Cachet/Controllers/Api/ComponentController.php index b86065e9..75d5b3a6 100644 --- a/app/CachetHq/Cachet/Controllers/Api/ComponentController.php +++ b/app/CachetHq/Cachet/Controllers/Api/ComponentController.php @@ -3,18 +3,17 @@ namespace CachetHQ\Cachet\Controllers\Api; use Input; -use Dingo\Api\Routing\Controller as DingoController; -use Dingo\Api\Auth\Shield; +use Dingo\Api\Routing\ControllerTrait; +use Illuminate\Routing\Controller; use CachetHQ\Cachet\Repositories\Component\ComponentRepository; -class ComponentController extends DingoController { +class ComponentController extends Controller { - protected $auth; + use ControllerTrait; protected $component; - public function __construct(Shield $auth, ComponentRepository $component) { - $this->auth = $auth; + public function __construct(ComponentRepository $component) { $this->component = $component; } @@ -38,6 +37,11 @@ class ComponentController extends DingoController { return $this->component->findOrFail($id); } + /** + * Return a component with incidents + * @param [type] $id [description] + * @return [type] [description] + */ public function getComponentIncidents($id) { return $this->component->with($id, ['incidents']); } diff --git a/app/CachetHq/Cachet/Controllers/Api/IncidentController.php b/app/CachetHq/Cachet/Controllers/Api/IncidentController.php index 2c25e4ed..18b2bf7a 100644 --- a/app/CachetHq/Cachet/Controllers/Api/IncidentController.php +++ b/app/CachetHq/Cachet/Controllers/Api/IncidentController.php @@ -2,16 +2,19 @@ namespace CachetHQ\Cachet\Controllers\Api; -use Input, Incident; -use Dingo\Api\Routing\Controller as DingoController; -use Dingo\Api\Auth\Shield; +use Input; +use Dingo\Api\Routing\ControllerTrait; +use Illuminate\Routing\Controller; +use CachetHQ\Cachet\Repositories\Incident\IncidentRepository; -class IncidentController extends DingoController { +class IncidentController extends Controller { - protected $auth; + use ControllerTrait; - public function __construct(Shield $auth) { - $this->auth = $auth; + protected $incident; + + public function __construct(IncidentRepository $incident) { + $this->incident = $incident; } /** @@ -20,7 +23,7 @@ class IncidentController extends DingoController { * @return \Illuminate\Database\Eloquent\Collection */ public function getIncidents() { - return Incident::all(); + return $this->incident->all(); } /** @@ -31,11 +34,7 @@ class IncidentController extends DingoController { * @return Incident */ public function getIncident($id) { - if ($incident = Incident::find($id)) { - return $incident; - } else { - App::abort(404, 'Incident not found'); - } + return $this->incident->findOrFail($id); } /** @@ -44,9 +43,7 @@ class IncidentController extends DingoController { * @return Incident */ public function postIncidents() { - $incident = new Incident(Input::all()); - $incident->user_id = $this->auth->user()->id; - return $this->_saveIncident($incident); + return $this->incident->create($this->auth->user()->id, Input::all()); } /** @@ -57,35 +54,6 @@ class IncidentController extends DingoController { * @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 - * - * @return Incident - */ - private function _saveIncident($incident) { - if ($incident->isValid()) { - try { - $component = $incident->component; - 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()); - } + return $this->incident->update($id, Input::all()); } } diff --git a/app/CachetHq/Cachet/Controllers/Api/MetricController.php b/app/CachetHq/Cachet/Controllers/Api/MetricController.php index e27c4078..68dcf671 100644 --- a/app/CachetHq/Cachet/Controllers/Api/MetricController.php +++ b/app/CachetHq/Cachet/Controllers/Api/MetricController.php @@ -2,18 +2,27 @@ namespace CachetHQ\Cachet\Controllers\Api; -use Input, Metric; -use Dingo\Api\Routing\Controller as DingoController; +use Input; +use Dingo\Api\Routing\ControllerTrait; +use Illuminate\Routing\Controller; +use CachetHQ\Cachet\Repositories\Metric\MetricRepository; -class MetricController extends DingoController { +class MetricController extends Controller { + + use ControllerTrait; + protected $metric; + + public function __construct(MetricRepository $metric) { + $this->metric = $metric; + } /** * Get all metrics * * @return \Illuminate\Database\Eloquent\Collection */ public function getMetrics() { - return Metric::all(); + return $this->metric->all(); } /** @@ -24,11 +33,7 @@ class MetricController extends DingoController { * @return Metric */ public function getMetric($id) { - if ($metric = Metric::find($id)) { - return $metric; - } else { - App::abort(404, 'Metric not found'); - } + return $this->metric->findOrFail($id); } /** @@ -37,8 +42,7 @@ class MetricController extends DingoController { * @return Metric */ public function postMetrics() { - $metric = new Metric(Input::all()); - return $this->_saveMetric($metric); + return $this->metric->create(Input::all()); } /** @@ -49,28 +53,6 @@ class MetricController extends DingoController { * @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()); - } + return $this->metric->update($id, Input::all()); } } diff --git a/app/CachetHq/Cachet/Repositories/EloquentRepository.php b/app/CachetHq/Cachet/Repositories/EloquentRepository.php index 1c262ea0..0a17995c 100644 --- a/app/CachetHq/Cachet/Repositories/EloquentRepository.php +++ b/app/CachetHq/Cachet/Repositories/EloquentRepository.php @@ -91,16 +91,6 @@ abstract class EloquentRepository { $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); - } - /** * Validate a given model with Watson validation * @param object $model @@ -110,5 +100,21 @@ abstract class EloquentRepository { if ($model->isInvalid()) { throw new Exception('Invalid model validation'); } + + return $this; + } + + /** + * Validate whether a model has a correct relationship + * @param object $model + * @param string $relationship Name of the relationship to validate against + * @return Exception + */ + public function hasRelationship($model, $relationship) { + if (! $model->$relationship) { + throw new Exception('Invalid relationship exception'); + } + + return $this; } } diff --git a/app/CachetHq/Cachet/Repositories/Incident/EloquentIncidentRepository.php b/app/CachetHq/Cachet/Repositories/Incident/EloquentIncidentRepository.php new file mode 100644 index 00000000..946457e8 --- /dev/null +++ b/app/CachetHq/Cachet/Repositories/Incident/EloquentIncidentRepository.php @@ -0,0 +1,35 @@ +model = $model; + } + + public function create($user_id, array $array) { + $incident = new $this->model($array); + $incident->user_id = $user_id; + + $this->validate($incident)->hasRelationship($incident, 'component'); + + $incident->saveOrFail(); + return $incident; + } + + public function update($id, array $array) { + $incident = $this->model->findOrFail($id); + $incident->fill($array); + + $this->validate($incident)->hasRelationship($incident, 'component'); + + $incident->update($array); + return $incident; + } +} diff --git a/app/CachetHq/Cachet/Repositories/Incident/IncidentRepository.php b/app/CachetHq/Cachet/Repositories/Incident/IncidentRepository.php new file mode 100644 index 00000000..3564146a --- /dev/null +++ b/app/CachetHq/Cachet/Repositories/Incident/IncidentRepository.php @@ -0,0 +1,14 @@ +model = $model; + } + + public function create(array $array) { + $metric = new $this->model($array); + + $this->validate($metric); + + $metric->saveOrFail(); + return $metric; + } + + public function update($id, array $array) { + $metric = $this->model->findOrFail($id); + $metric->fill($array); + + $this->validate($metric); + + $metric->update($array); + return $metric; + } +} diff --git a/app/CachetHq/Cachet/Repositories/Metric/MetricRepository.php b/app/CachetHq/Cachet/Repositories/Metric/MetricRepository.php new file mode 100644 index 00000000..9ae376e9 --- /dev/null +++ b/app/CachetHq/Cachet/Repositories/Metric/MetricRepository.php @@ -0,0 +1,14 @@ +app->bind( + 'CachetHQ\Cachet\Repositories\Incident\IncidentRepository', + 'CachetHQ\Cachet\Repositories\Incident\EloquentIncidentRepository' + ); + $this->app->bind( + 'CachetHQ\Cachet\Repositories\Metric\MetricRepository', + 'CachetHQ\Cachet\Repositories\Metric\EloquentMetricRepository' + ); } } diff --git a/app/config/app.php b/app/config/app.php index a03462a6..3aa8e2a8 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -122,7 +122,7 @@ return array( 'Illuminate\View\ViewServiceProvider', 'Illuminate\Workbench\WorkbenchServiceProvider', - 'Dingo\Api\ApiServiceProvider', + 'Dingo\Api\Provider\ApiServiceProvider', 'Thujohn\Rss\RssServiceProvider', 'CachetHQ\Cachet\Support\ServiceProviders\RepositoryServiceProvider', diff --git a/app/tests/Controllers/Api/ComponentControllerTest.php b/app/tests/Controllers/Api/ComponentControllerTest.php index 55a4c0d2..e5ed113f 100644 --- a/app/tests/Controllers/Api/ComponentControllerTest.php +++ b/app/tests/Controllers/Api/ComponentControllerTest.php @@ -14,7 +14,7 @@ class ComponentControllerTest extends TestCase { public function test_get_components_method() { $this->repo->shouldReceive('all')->once()->andReturn('foo'); - $controller = new CachetHQ\Cachet\Controllers\Api\ComponentController($this->dingo, $this->repo); + $controller = new CachetHQ\Cachet\Controllers\Api\ComponentController($this->repo); $response = $controller->getComponents(); $this->assertEquals('foo', $response); @@ -23,9 +23,10 @@ class ComponentControllerTest extends TestCase { public function test_get_component_method() { $this->repo->shouldReceive('findOrFail')->with(1)->once()->andReturn('foo'); - $controller = new CachetHQ\Cachet\Controllers\Api\ComponentController($this->dingo, $this->repo); + $controller = new CachetHQ\Cachet\Controllers\Api\ComponentController($this->repo); $response = $controller->getComponent(1); $this->assertEquals('foo', $response); } + } diff --git a/composer.json b/composer.json index 2c4e28b2..cbb81bc7 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "require": { "laravel/framework": "4.2.*", "guzzlehttp/guzzle": "~5.0", - "dingo/api": "0.6.*", + "dingo/api": "0.7.*", "watson/validating": "0.10.*", "thujohn/rss": "dev-master" }, diff --git a/composer.lock b/composer.lock index 153ccecd..16b47f26 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": "bceebea08594f491daed4b104ec0cbc0", + "hash": "6a8ddbabbd2a61cd7be7327e895cd682", "packages": [ { "name": "classpreloader/classpreloader", @@ -89,43 +89,45 @@ }, { "name": "dingo/api", - "version": "v0.6.6", + "version": "v0.7.1", "source": { "type": "git", "url": "https://github.com/dingo/api.git", - "reference": "87ce1b823941deddf266d2fcbf537145c0530716" + "reference": "def08e52a6582d8f60c12ebc509ed7d6e8a7f81b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/dingo/api/zipball/87ce1b823941deddf266d2fcbf537145c0530716", - "reference": "87ce1b823941deddf266d2fcbf537145c0530716", + "url": "https://api.github.com/repos/dingo/api/zipball/def08e52a6582d8f60c12ebc509ed7d6e8a7f81b", + "reference": "def08e52a6582d8f60c12ebc509ed7d6e8a7f81b", "shasum": "" }, "require": { - "illuminate/support": "~4.1|~5.0", - "league/fractal": "0.8.*", + "illuminate/support": "~4.1", + "league/fractal": "0.10.*", "php": ">=5.4.0" }, "require-dev": { - "illuminate/auth": "~4.1", - "illuminate/console": "~4.1", - "illuminate/database": "~4.1", - "illuminate/events": "~4.1", - "illuminate/pagination": "~4.1", - "illuminate/routing": "~4.1", - "lucadegasperi/oauth2-server-laravel": "1.0.*", + "illuminate/auth": "4.2.*", + "illuminate/console": "4.2.*", + "illuminate/database": "4.2.*", + "illuminate/events": "4.2.*", + "illuminate/filesystem": "4.2.*", + "illuminate/pagination": "4.2.*", + "illuminate/routing": "4.2.*", + "lucadegasperi/oauth2-server-laravel": "3.0.*", "mockery/mockery": "~0.9", "phpunit/phpunit": "~4.0", - "squizlabs/php_codesniffer": "1.*" + "squizlabs/php_codesniffer": "~1.5", + "tymon/jwt-auth": "0.3.*" }, "suggest": { - "lucadegasperi/oauth2-server-laravel": "Use the League OAuth 2.0 server to protect your API." + "lucadegasperi/oauth2-server-laravel": "Use the League OAuth 2.0 server to protect your API.", + "tymon/jwt-auth": "Protect your API with JSON Web Tokens" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "0.6-dev", - "dev-develop": "0.7-dev" + "dev-master": "0.7-dev" } }, "autoload": { @@ -150,7 +152,7 @@ "laravel", "restful" ], - "time": "2014-09-29 00:06:33" + "time": "2014-12-01 01:04:27" }, { "name": "filp/whoops", @@ -575,16 +577,16 @@ }, { "name": "league/fractal", - "version": "0.8.3", + "version": "0.10.0", "source": { "type": "git", "url": "https://github.com/thephpleague/fractal.git", - "reference": "9985eee7efc42ef472da07856cdd3fd29c57642c" + "reference": "557e1803dabe8ce252a4549a29da6d439f62d3c2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/fractal/zipball/9985eee7efc42ef472da07856cdd3fd29c57642c", - "reference": "9985eee7efc42ef472da07856cdd3fd29c57642c", + "url": "https://api.github.com/repos/thephpleague/fractal/zipball/557e1803dabe8ce252a4549a29da6d439f62d3c2", + "reference": "557e1803dabe8ce252a4549a29da6d439f62d3c2", "shasum": "" }, "require": { @@ -602,7 +604,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "0.8-dev" + "dev-master": "0.10-dev" } }, "autoload": { @@ -617,8 +619,8 @@ "authors": [ { "name": "Phil Sturgeon", - "email": "email@philsturgeon.co.uk", - "homepage": "http://philsturgeon.co.uk/", + "email": "me@philsturgeon.uk", + "homepage": "http://philsturgeon.uk/", "role": "Developer" } ], @@ -630,7 +632,7 @@ "league", "rest" ], - "time": "2014-06-14 11:11:41" + "time": "2014-10-17 17:12:38" }, { "name": "monolog/monolog",