Merge pull request #43 from Ehesp/repo_pattern

Repository pattern implementation
This commit is contained in:
Philip Manavopoulos
2014-11-26 23:07:38 +00:00
11 changed files with 388 additions and 242 deletions

View File

@@ -0,0 +1,51 @@
<?php namespace CachetHq\Cachet\Controllers\Api;
use Input;
use Dingo\Api\Routing\Controller as DingoController;
use Dingo\Api\Auth\Shield;
use CachetHq\Cachet\Repositories\Component\ComponentRepository;
class ComponentController extends DingoController {
protected $auth;
protected $component;
public function __construct(Shield $auth, ComponentRepository $component) {
$this->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());
}
}

View File

@@ -0,0 +1,90 @@
<?php namespace CachetHq\Cachet\Controllers\Api;
use Input, Incident;
use Dingo\Api\Routing\Controller as DingoController;
use Dingo\Api\Auth\Shield;
use CachetHq\Cachet\Repositories\Component\ComponentRepository;
class IncidentController extends DingoController {
protected $auth;
public function __construct(Shield $auth) {
$this->auth = $auth;
}
/**
* 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');
}
}
/**
* 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);
$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->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());
}
}
}

View File

@@ -0,0 +1,76 @@
<?php namespace CachetHq\Cachet\Controllers\Api;
use Input, Metric;
use Dingo\Api\Routing\Controller as DingoController;
use Dingo\Api\Auth\Shield;
use CachetHq\Cachet\Repositories\Component\ComponentRepository;
class MetricController extends DingoController {
/**
* Get all metrics
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getMetrics() {
return Metric::all();
}
/**
* Get a single metric
*
* @param int $id
*
* @return Metric
*/
public function getMetric($id) {
if ($metric = Metric::find($id)) {
return $metric;
} else {
App::abort(404, 'Metric not found');
}
}
/**
* 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);
}
/**
* 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());
}
}
}

View File

@@ -0,0 +1,12 @@
<?php namespace CachetHq\Cachet\Repositories\Component;
interface ComponentRepository {
public function all();
public function create($id, array $array);
public function findOrFail($id);
public function with($id, array $with);
}

View File

@@ -0,0 +1,26 @@
<?php namespace CachetHq\Cachet\Repositories\Component;
use CachetHq\Cachet\Repositories\EloquentRepository;
use Component;
use Exception;
class EloquentComponentRepository extends EloquentRepository implements ComponentRepository {
protected $model;
public function __construct(Component $model) {
$this->model = $model;
}
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;
}
}

View File

@@ -0,0 +1,101 @@
<?php namespace CachetHq\Cachet\Repositories;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Exception;
abstract class EloquentRepository {
/**
* Returns all models
* @return object
*/
public function all() {
return $this->model->all();
}
/**
* 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);
}
/**
* 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 int $id
* @return object
*/
public function find(int $id) {
return $this->model->find($id);
}
/**
* Finds a model by ID
* @param integer $id
* @return object|ModelNotFoundException
*/
public function findOrFail($id) {
return $this->model->findOrFail($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;
}
throw new ModelNotFoundException;
}
/**
* 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);
}
}

View File

@@ -0,0 +1,12 @@
<?php namespace CachetHq\Cachet\Support\ServiceProviders;
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind('CachetHq\Cachet\Repositories\Component\ComponentRepository', 'CachetHq\Cachet\Repositories\Component\EloquentComponentRepository');
}
}

View File

@@ -124,6 +124,8 @@ return array(
'Dingo\Api\ApiServiceProvider',
'CachetHq\Cachet\Support\ServiceProviders\RepositoryServiceProvider',
),
/*

View File

@@ -1,227 +0,0 @@
<?php
class ApiController extends \Dingo\Api\Routing\Controller {
protected $auth;
public function __construct(\Dingo\Api\Auth\Shield $auth) {
$this->auth = $auth;
}
/**
* Get all components
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getComponents() {
return Component::all();
}
/**
* Get a single component
*
* @param int $id
*
* @return Component
*/
public function getComponent($id) {
if ($component = Component::find($id)) {
return $component;
} else {
App::abort(404, 'Component not found');
}
}
public function getComponentIncidents($id) {
$component = $this->getComponent($id);
return $component->incidents;
}
/**
* Create a new component
*
* @return Component
*/
public function postComponents() {
$component = new Component(Input::all());
$component->user_id = $this->auth->user()->id;
return $this->_saveComponent($component);
}
/**
* Update an existing component
*
* @param int $id
*
* @return Component
*/
public function putComponent($id) {
$component = $this->getComponent($id);
$component->fill(Input::all());
return $this->_saveComponent($component);
}
/**
* 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');
}
}
/**
* 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);
$incident->fill(Input::all());
return $this->_saveIncident($incident);
}
/**
* Get all metrics
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getMetrics() {
return Metric::all();
}
/**
* Get a single metric
*
* @param int $id
*
* @return Metric
*/
public function getMetric($id) {
if ($metric = Metric::find($id)) {
return $metric;
} else {
App::abort(404, 'Metric not found');
}
}
/**
* Function for saving the component, and returning appropriate error codes
*
* @param Component $component
*
* @return Component
*/
private function _saveComponent($component) {
if ($component->isValid()) {
try {
$component->saveOrFail();
return $component;
} catch (Exception $e) {
App::abort(500, $e->getMessage());
}
} else {
App::abort(404, $component->getErrors()->first());
}
}
/**
* 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());
}
}
/**
* 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);
}
/**
* 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());
}
}
}

View File

@@ -1,23 +1,23 @@
<?php
Route::api(['version' => 'v1', 'prefix' => 'api'], function() {
Route::api(['version' => 'v1', 'prefix' => 'api', 'namespace' => 'CachetHq\Cachet\Controllers\Api'], function() {
Route::get('components', 'ApiController@getComponents');
Route::get('components/{id}', 'ApiController@getComponent');
Route::get('components/{id}/incidents', 'ApiController@getComponentIncidents');
Route::get('incidents', 'ApiController@getIncidents');
Route::get('incidents/{id}', 'ApiController@getIncident');
Route::get('metrics', 'ApiController@getMetrics');
Route::get('metrics/{id}', 'ApiController@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', 'ApiController@postComponents');
Route::post('incidents', 'ApiController@postIncidents');
Route::post('metrics', 'ApiController@postMetrics');
Route::post('components', 'ComponentController@postComponents');
Route::post('incidents', 'IncidentController@postIncidents');
Route::post('metrics', 'MetricController@postMetrics');
Route::put('components/{id}', 'ApiController@putComponent');
Route::put('incidents/{id}', 'ApiController@putIncident');
Route::put('metrics/{id}', 'ApiController@putMetric');
Route::put('components/{id}', 'ComponentController@putComponent');
Route::put('incidents/{id}', 'IncidentController@putIncident');
Route::put('metrics/{id}', 'MetricController@putMetric');
});
});

View File

@@ -23,7 +23,10 @@
"app/database/seeds",
"app/tests/TestCase.php",
"app/filters"
]
],
"psr-4": {
"CachetHq\\": "app/CachetHq"
}
},
"extra": {
"heroku": {