diff --git a/app/CachetHq/Cachet/Controllers/Api/ComponentController.php b/app/CachetHq/Cachet/Controllers/Api/ComponentController.php index b4efca08..62544be7 100644 --- a/app/CachetHq/Cachet/Controllers/Api/ComponentController.php +++ b/app/CachetHq/Cachet/Controllers/Api/ComponentController.php @@ -1,53 +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()); - } + 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()); + } +} \ 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 5402a69c..f700e007 100644 --- a/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php +++ b/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php @@ -1,14 +1,14 @@ model = $model; - } - - public function create($user_id, array $array) { - $component = new $this->model($array); - $component->user_id = $user_id; - - $this->validate($component); - - $component->saveOrFail(); - return $component; - } + 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; + + $this->validate($component); + + $component->saveOrFail(); + return $component; + } +} diff --git a/app/CachetHq/Cachet/Repositories/EloquentRepository.php b/app/CachetHq/Cachet/Repositories/EloquentRepository.php index 3afb4048..1c262ea0 100644 --- a/app/CachetHq/Cachet/Repositories/EloquentRepository.php +++ b/app/CachetHq/Cachet/Repositories/EloquentRepository.php @@ -1,114 +1,114 @@ model->all(); + /** + * 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; } - /** - * 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); + 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(); } - /** - * 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; - } + return $this->model->count(); + } - /** - * Finds a model by ID - * @param int $id - * @return object - */ - public function find(int $id) { - return $this->model->find($id); - } + /** + * Deletes a model by ID + * @param inetegr $id + */ + public function destroy($id) { + $this->model->delete($id); + } - /** - * Finds a model by ID - * @param integer $id - * @return object|ModelNotFoundException - */ - public function findOrFail($id) { - return $this->model->findOrFail($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); + } - /** - * 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); - } - - /** - * Validate a given model with Watson validation - * @param object $model - * @return Exception - */ - public function validate($model) { - if ($model->isInvalid()) { - throw new Exception('Invalid model validation'); - } + /** + * Validate a given model with Watson validation + * @param object $model + * @return Exception + */ + public function validate($model) { + if ($model->isInvalid()) { + throw new Exception('Invalid model validation'); } } +} diff --git a/composer.lock b/composer.lock index 36aaac35..c7579ec7 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": "a869e5c2586d00bb96110dc4fd24e123", + "hash": "ae2cfb2f8386f3b5f6fd63b350807b5a", "packages": [ { "name": "classpreloader/classpreloader",