Move CachetHq to folder

This commit is contained in:
James Brooks
2014-12-20 21:16:00 +00:00
parent 63ff951d88
commit a982edeb63
21 changed files with 0 additions and 0 deletions
@@ -0,0 +1,57 @@
<?php
namespace CachetHQ\Cachet\Controllers\Api;
use Input;
use Dingo\Api\Routing\ControllerTrait;
use Illuminate\Routing\Controller;
use CachetHQ\Cachet\Repositories\Component\ComponentRepository;
class ComponentController extends Controller {
use ControllerTrait;
protected $component;
public function __construct(ComponentRepository $component) {
$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);
}
/**
* Return a component with incidents
* @param int $id Component ID
* @return \Component
*/
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());
}
}
@@ -0,0 +1,59 @@
<?php
namespace CachetHQ\Cachet\Controllers\Api;
use Input;
use Dingo\Api\Routing\ControllerTrait;
use Illuminate\Routing\Controller;
use CachetHQ\Cachet\Repositories\Incident\IncidentRepository;
class IncidentController extends Controller {
use ControllerTrait;
protected $incident;
public function __construct(IncidentRepository $incident) {
$this->incident = $incident;
}
/**
* Get all incidents
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getIncidents() {
return $this->incident->all();
}
/**
* Get a single incident
*
* @param int $id
*
* @return Incident
*/
public function getIncident($id) {
return $this->incident->findOrFail($id);
}
/**
* Create a new incident
*
* @return Incident
*/
public function postIncidents() {
return $this->incident->create($this->auth->user()->id, Input::all());
}
/**
* Update an existing incident
*
* @param int $id
*
* @return Incident
*/
public function putIncident($id) {
return $this->incident->update($id, Input::all());
}
}
@@ -0,0 +1,58 @@
<?php
namespace CachetHQ\Cachet\Controllers\Api;
use Input;
use Dingo\Api\Routing\ControllerTrait;
use Illuminate\Routing\Controller;
use CachetHQ\Cachet\Repositories\Metric\MetricRepository;
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 $this->metric->all();
}
/**
* Get a single metric
*
* @param int $id
*
* @return Metric
*/
public function getMetric($id) {
return $this->metric->findOrFail($id);
}
/**
* Create a new metric
*
* @return Metric
*/
public function postMetrics() {
return $this->metric->create(Input::all());
}
/**
* Update an existing metric
*
* @param int $id
*
* @return Metric
*/
public function putMetric($id) {
return $this->metric->update($id, Input::all());
}
}
@@ -0,0 +1,47 @@
<?php
namespace CachetHQ\Cachet\Controllers\Api;
use Input;
use Dingo\Api\Routing\ControllerTrait;
use Illuminate\Routing\Controller;
use CachetHQ\Cachet\Repositories\MetricPoint\MetricPointRepository;
class MetricController extends Controller {
use ControllerTrait;
protected $metricpoint;
public function __construct(MetricPointRepository $metricpoint) {
$this->metricpoint = $metricpoint;
}
/**
* Get all metric points
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getMetricPoints() {
return $this->metricpoint->all();
}
/**
* Get a single metric point
*
* @param int $id
*
* @return MetricPoint
*/
public function getMetricPoint($id) {
return $this->metricpoint->findOrFail($id);
}
/**
* Create a new metric point
*
* @return MetricPoint
*/
public function postMetricPoints() {
return $this->metricpoint->create(Input::all());
}
}
@@ -0,0 +1,14 @@
<?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);
}
@@ -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;
$this->validate($component);
$component->saveOrFail();
return $component;
}
}
@@ -0,0 +1,120 @@
<?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);
}
/**
* 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');
}
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;
}
}
@@ -0,0 +1,35 @@
<?php
namespace CachetHQ\Cachet\Repositories\Incident;
use CachetHQ\Cachet\Repositories\EloquentRepository;
use Incident;
class EloquentIncidentRepository extends EloquentRepository implements IncidentRepository {
protected $model;
public function __construct(Incident $model) {
$this->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;
}
}
@@ -0,0 +1,14 @@
<?php
namespace CachetHQ\Cachet\Repositories\Incident;
interface IncidentRepository {
public function all();
public function create($id, array $array);
public function findOrFail($id);
public function update($id, array $with);
}
@@ -0,0 +1,34 @@
<?php
namespace CachetHQ\Cachet\Repositories\Metric;
use CachetHQ\Cachet\Repositories\EloquentRepository;
use Metric;
class EloquentMetricRepository extends EloquentRepository implements MetricRepository {
protected $model;
public function __construct(Metric $model) {
$this->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;
}
}
@@ -0,0 +1,14 @@
<?php
namespace CachetHQ\Cachet\Repositories\Metric;
interface MetricRepository {
public function all();
public function create(array $array);
public function findOrFail($id);
public function update($id, array $with);
}
@@ -0,0 +1,34 @@
<?php
namespace CachetHQ\Cachet\Repositories\MetricPoint;
use CachetHQ\Cachet\Repositories\EloquentRepository;
use MetricPoint;
class EloquentMetricRepository extends EloquentRepository implements MetricRepository {
protected $model;
public function __construct(MetricPoint $model) {
$this->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;
}
}
@@ -0,0 +1,14 @@
<?php
namespace CachetHQ\Cachet\Repositories\MetricPoint;
interface MetricPointRepository {
public function all();
public function create(array $array);
public function findOrFail($id);
public function update($id, array $with);
}
@@ -0,0 +1,19 @@
<?php
namespace CachetHQ\Cachet\Service\Email;
class EmailService implements ServiceInterface {
protected $properties;
public function register() {
}
public function unregister() {
}
public function fire($data) {
}
}
@@ -0,0 +1,9 @@
<?php
namespace CachetHQ\Cachet\Service;
interface ServiceInterface {
public function register();
public function unregister();
public function fire($data);
}
@@ -0,0 +1,22 @@
<?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'
);
$this->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'
);
}
}
@@ -0,0 +1,35 @@
<?php
namespace CachetHQ\Cachet\Support\ServiceProviders;
use Illuminate\Support\ServiceProvider;
use RecursiveDirectoryIterator;
class RoutingServiceProvider extends ServiceProvider {
public function register() {}
public function boot() {
$this->routesInDirectory();
}
/**
* Organise Routes
* @param string $app
*/
private function routesInDirectory($app = '') {
$routeDir = app_path('routes/' . $app . ($app !== '' ? '/' : null));
$iterator = new RecursiveDirectoryIterator($routeDir);
$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
foreach ($iterator as $route) {
$isDotFile = strpos($route->getFilename(), '.') === 0;
if (!$isDotFile && !$route->isDir()) {
require $routeDir . $route->getFilename();
}
}
}
}
@@ -0,0 +1,23 @@
<?php
namespace CachetHQ\Cachet\Transformers;
use Component;
use League\Fractal\TransformerAbstract;
class ComponentTransformer extends TransformerAbstract {
public function transform(Component $component) {
return [
'id' => (int) $component->id,
'name' => $component->name,
'description' => $component->description,
'status_id' => (int) $component->status,
'status' => $component->humanStatus,
'incident_count' => $component->incidents()->count(),
'created_at' => $component->created_at->timestamp,
'updated_at' => $component->updated_at->timestamp,
];
}
}
@@ -0,0 +1,25 @@
<?php
namespace CachetHQ\Cachet\Transformers;
use Incident;
use League\Fractal\TransformerAbstract;
class IncidentTransformer extends TransformerAbstract {
public function transform(Incident $incident) {
$component = $incident->component;
$transformer = $component->getTransformer();
return [
'id' => (int) $incident->id,
'name' => $incident->name,
'message' => $incident->message,
'status_id' => (int) $incident->status,
'status' => $incident->getHumanStatusAttribute(),
'component' => $transformer->transform($component),
'created_at' => $incident->created_at->timestamp,
'updated_at' => $incident->updated_at->timestamp,
];
}
}
@@ -0,0 +1,20 @@
<?php
namespace CachetHQ\Cachet\Transformers;
use MetricPoint;
use League\Fractal\TransformerAbstract;
class MetricPointTransformer extends TransformerAbstract {
public function transform(MetricPoint $metricPoint) {
return [
'id' => (int) $metricPoint->id,
'metric_id' => $metricPoint->metric_id,
'value' => $metricPoint->value,
'created_at' => $metricPoint->created_at->timestamp,
'updated_at' => $metricPoint->updated_at->timestamp,
];
}
}
@@ -0,0 +1,22 @@
<?php
namespace CachetHQ\Cachet\Transformers;
use Metric;
use League\Fractal\TransformerAbstract;
class MetricTransformer extends TransformerAbstract {
public function transform(Metric $metric) {
return [
'id' => (int) $metric->id,
'name' => $metric->name,
'description' => $metric->description,
'suffix' => $metric->suffix,
'display' => $metric->shouldDisplay,
'created_at' => $metric->created_at->timestamp,
'updated_at' => $metric->updated_at->timestamp,
];
}
}