Code standards update to classes

This commit is contained in:
Elliot Hesp
2014-11-27 20:14:17 +00:00
parent bf1858cd4c
commit 954f9cf65e
5 changed files with 172 additions and 172 deletions

View File

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

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

View File

@@ -1,26 +1,26 @@
<?php
namespace CachetHQ\Cachet\Repositories\Component;
namespace CachetHQ\Cachet\Repositories\Component;
use CachetHQ\Cachet\Repositories\EloquentRepository;
use Component;
use Exception;
use CachetHQ\Cachet\Repositories\EloquentRepository;
use Component;
use Exception;
class EloquentComponentRepository extends EloquentRepository implements ComponentRepository {
class EloquentComponentRepository extends EloquentRepository implements ComponentRepository {
protected $model;
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;
$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;
}
}

View File

@@ -1,114 +1,114 @@
<?php
namespace CachetHQ\Cachet\Repositories;
namespace CachetHQ\Cachet\Repositories;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Exception;
abstract class EloquentRepository {
abstract class EloquentRepository {
/**
* Returns all models
* @return object
*/
public function all() {
return $this->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');
}
}
}

2
composer.lock generated
View File

@@ -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",