Skip to content

Commit

Permalink
Merge pull request #46 from cachethq/namespace
Browse files Browse the repository at this point in the history
Namespace
  • Loading branch information
jbrooksuk committed Nov 26, 2014
2 parents 37ccd24 + 079a2cb commit 32f3f54
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 233 deletions.
102 changes: 52 additions & 50 deletions app/CachetHq/Cachet/Controllers/Api/ComponentController.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
<?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());
<?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());
}
}
}
112 changes: 57 additions & 55 deletions app/CachetHq/Cachet/Controllers/Api/IncidentController.php
Original file line number Diff line number Diff line change
@@ -1,68 +1,70 @@
<?php namespace CachetHq\Cachet\Controllers\Api;
<?php

use Input, Incident;
use Dingo\Api\Routing\Controller as DingoController;
use Dingo\Api\Auth\Shield;
namespace CachetHQ\Cachet\Controllers\Api;

class IncidentController extends DingoController {
use Input, Incident;
use Dingo\Api\Routing\Controller as DingoController;
use Dingo\Api\Auth\Shield;

protected $auth;
class IncidentController extends DingoController {

public function __construct(Shield $auth) {
$this->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
Expand All @@ -86,4 +88,4 @@ private function _saveIncident($incident) {
App::abort(404, $incident->getErrors()->first());
}
}
}
}
12 changes: 7 additions & 5 deletions app/CachetHq/Cachet/Controllers/Api/MetricController.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php namespace CachetHq\Cachet\Controllers\Api;
<?php

use Input, Metric;
use Dingo\Api\Routing\Controller as DingoController;
namespace CachetHQ\Cachet\Controllers\Api;

class MetricController extends DingoController {
use Input, Metric;
use Dingo\Api\Routing\Controller as DingoController;

class MetricController extends DingoController {

/**
* Get all metrics
Expand Down Expand Up @@ -71,4 +73,4 @@ private function _saveMetric($metric) {
App::abort(404, $metric->getErrors()->first());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php namespace CachetHq\Cachet\Repositories\Component;
<?php

interface ComponentRepository {
namespace CachetHQ\Cachet\Repositories\Component;

public function all();
interface ComponentRepository {

public function create($id, array $array);
public function all();

public function findOrFail($id);
public function create($id, array $array);

public function with($id, array $with);
}
public function findOrFail($id);

public function with($id, array $with);
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
<?php namespace CachetHq\Cachet\Repositories\Component;
<?php

use CachetHq\Cachet\Repositories\EloquentRepository;
use Component;
use Exception;
namespace CachetHQ\Cachet\Repositories\Component;

class EloquentComponentRepository extends EloquentRepository implements ComponentRepository {
use CachetHQ\Cachet\Repositories\EloquentRepository;
use Component;
use Exception;

protected $model;
class EloquentComponentRepository extends EloquentRepository implements ComponentRepository {

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;
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;
}
}
}
Loading

0 comments on commit 32f3f54

Please sign in to comment.