Cachet is now a Laravel 5 app
This commit is contained in:
56
app/Repositories/Component/ComponentRepository.php
Normal file
56
app/Repositories/Component/ComponentRepository.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\Component;
|
||||
|
||||
interface ComponentRepository
|
||||
{
|
||||
/**
|
||||
* Returns all models.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function all();
|
||||
|
||||
/**
|
||||
* Create a new model.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function create($userId, array $data);
|
||||
|
||||
/**
|
||||
* Update a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function update($id, array $data);
|
||||
|
||||
/**
|
||||
* Finds a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function findOrFail($id);
|
||||
|
||||
/**
|
||||
* Returns an object with related relationships.
|
||||
*
|
||||
* @param int $id
|
||||
* @param string[] $with
|
||||
*
|
||||
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function with($id, array $with = []);
|
||||
}
|
||||
67
app/Repositories/Component/EloquentComponentRepository.php
Normal file
67
app/Repositories/Component/EloquentComponentRepository.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\Component;
|
||||
|
||||
use CachetHQ\Cachet\Models\Component;
|
||||
use CachetHQ\Cachet\Repositories\EloquentRepository;
|
||||
|
||||
class EloquentComponentRepository extends EloquentRepository implements ComponentRepository
|
||||
{
|
||||
/**
|
||||
* The eloquent model instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Models\Component
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a new eloquent component repository instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Component $model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Component $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new model.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function create($userId, array $data)
|
||||
{
|
||||
$component = new $this->model($data);
|
||||
$component->user_id = $userId;
|
||||
|
||||
$this->validate($component);
|
||||
|
||||
$component->saveOrFail();
|
||||
|
||||
return $component;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function update($id, array $data)
|
||||
{
|
||||
$component = $this->model->findOrFail($id);
|
||||
$component->fill($data);
|
||||
$this->validate($component);
|
||||
|
||||
$component->update($data);
|
||||
|
||||
return $component;
|
||||
}
|
||||
}
|
||||
175
app/Repositories/EloquentRepository.php
Normal file
175
app/Repositories/EloquentRepository.php
Normal file
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
abstract class EloquentRepository
|
||||
{
|
||||
/**
|
||||
* Returns all models.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->model->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns paginated result.
|
||||
*
|
||||
* @param int $perPage
|
||||
*
|
||||
* @return \Illuminate\Pagination\Paginator
|
||||
*/
|
||||
public function paginate($perPage = 20)
|
||||
{
|
||||
return $this->model->paginate($perPage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an object with related relationships.
|
||||
*
|
||||
* @param int $id
|
||||
* @param string[] $with
|
||||
*
|
||||
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function with($id, array $with = [])
|
||||
{
|
||||
return $this->model->with($with)->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the model to query against a user id.
|
||||
*
|
||||
* @param int $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 \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function find($id)
|
||||
{
|
||||
return $this->model->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function findOrFail($id)
|
||||
{
|
||||
return $this->model->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a model by type.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @param array $columns
|
||||
*
|
||||
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function findByOrFail($key, $value, $columns = ['*'])
|
||||
{
|
||||
$model = $this->model->where($key, $value)->first($columns);
|
||||
|
||||
if ($model === null) {
|
||||
throw new ModelNotFoundException();
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the number of rows returned.
|
||||
*
|
||||
* @param string|null $key
|
||||
* @param string|null $value
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count($key = null, $value = null)
|
||||
{
|
||||
if ($key === null || $value === null) {
|
||||
return $this->model->count();
|
||||
}
|
||||
|
||||
return $this->model->where($key, $value)->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$this->model->destroy($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a given model with Watson validation.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
*
|
||||
* @throws \CachetHQ\Cachet\Repositories\InvalidModelValidationException
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function validate(Model $model)
|
||||
{
|
||||
if ($model->isInvalid()) {
|
||||
throw new InvalidModelValidationException('Validation failed on the model.');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate whether a model has a correct relationship.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $relationship
|
||||
*
|
||||
* @throws \CachetHQ\Cachet\Repositories\InvalidRelationshipException
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function hasRelationship(Model $model, $relationship)
|
||||
{
|
||||
if ($model->$relationship === null) {
|
||||
throw new InvalidRelationshipException('The relationship was not valid.');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
74
app/Repositories/Incident/EloquentIncidentRepository.php
Normal file
74
app/Repositories/Incident/EloquentIncidentRepository.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\Incident;
|
||||
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use CachetHQ\Cachet\Repositories\EloquentRepository;
|
||||
|
||||
class EloquentIncidentRepository extends EloquentRepository implements IncidentRepository
|
||||
{
|
||||
/**
|
||||
* The eloquent model instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Models\Incident
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a new eloquent incident repository instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Incident $model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Incident $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new model.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function create($userId, array $data)
|
||||
{
|
||||
$incident = new $this->model($data);
|
||||
$incident->user_id = $userId;
|
||||
$this->validate($incident);
|
||||
|
||||
if (isset($data['component_id'])) {
|
||||
$this->hasRelationship($incident, 'component');
|
||||
}
|
||||
|
||||
$incident->saveOrFail();
|
||||
|
||||
return $incident;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function update($id, array $data)
|
||||
{
|
||||
$incident = $this->model->findOrFail($id);
|
||||
$incident->fill($data);
|
||||
$this->validate($incident);
|
||||
|
||||
if (isset($data['component_id'])) {
|
||||
$this->hasRelationship($incident, 'component');
|
||||
}
|
||||
|
||||
$incident->update($data);
|
||||
|
||||
return $incident;
|
||||
}
|
||||
}
|
||||
53
app/Repositories/Incident/IncidentRepository.php
Normal file
53
app/Repositories/Incident/IncidentRepository.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\Incident;
|
||||
|
||||
interface IncidentRepository
|
||||
{
|
||||
/**
|
||||
* Returns all models.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function all();
|
||||
|
||||
/**
|
||||
* Returns paginated result.
|
||||
*
|
||||
* @param int $perPage
|
||||
*
|
||||
* @return \Illuminate\Pagination\Paginator
|
||||
*/
|
||||
public function paginate($perPage = 20);
|
||||
|
||||
/**
|
||||
* Create a new model.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function create($userId, array $data);
|
||||
|
||||
/**
|
||||
* Finds a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function findOrFail($id);
|
||||
|
||||
/**
|
||||
* Update a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function update($id, array $data);
|
||||
}
|
||||
10
app/Repositories/InvalidModelValidationException.php
Normal file
10
app/Repositories/InvalidModelValidationException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories;
|
||||
|
||||
use Exception;
|
||||
|
||||
class InvalidModelValidationException extends Exception
|
||||
{
|
||||
//
|
||||
}
|
||||
10
app/Repositories/InvalidRelationshipException.php
Normal file
10
app/Repositories/InvalidRelationshipException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories;
|
||||
|
||||
use Exception;
|
||||
|
||||
class InvalidRelationshipException extends Exception
|
||||
{
|
||||
//
|
||||
}
|
||||
80
app/Repositories/Metric/EloquentMetricRepository.php
Normal file
80
app/Repositories/Metric/EloquentMetricRepository.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\Metric;
|
||||
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use CachetHQ\Cachet\Repositories\EloquentRepository;
|
||||
|
||||
class EloquentMetricRepository extends EloquentRepository implements MetricRepository
|
||||
{
|
||||
/**
|
||||
* The eloquent model instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Models\Metric
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a new eloquent metric repository instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Metric $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new model.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function create(array $data)
|
||||
{
|
||||
$metric = new $this->model($data);
|
||||
|
||||
$this->validate($metric);
|
||||
|
||||
$metric->saveOrFail();
|
||||
|
||||
return $metric;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function update($id, array $data)
|
||||
{
|
||||
$metric = $this->model->findOrFail($id);
|
||||
$metric->fill($data);
|
||||
|
||||
$this->validate($metric);
|
||||
|
||||
$metric->update($data);
|
||||
|
||||
return $metric;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all metric point models.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function points($id)
|
||||
{
|
||||
$metric = $this->model->findOrFail($id);
|
||||
|
||||
return $metric->points;
|
||||
}
|
||||
}
|
||||
52
app/Repositories/Metric/MetricRepository.php
Normal file
52
app/Repositories/Metric/MetricRepository.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\Metric;
|
||||
|
||||
interface MetricRepository
|
||||
{
|
||||
/**
|
||||
* Returns all models.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function all();
|
||||
|
||||
/**
|
||||
* Returns all metric point models.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function points($id);
|
||||
|
||||
/**
|
||||
* Create a new model.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function create(array $data);
|
||||
|
||||
/**
|
||||
* Finds a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function findOrFail($id);
|
||||
|
||||
/**
|
||||
* Update a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function update($id, array $data);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\MetricPoint;
|
||||
|
||||
use CachetHQ\Cachet\Models\MetricPoint;
|
||||
use CachetHQ\Cachet\Repositories\EloquentRepository;
|
||||
|
||||
class EloquentMetricPointRepository extends EloquentRepository implements MetricPointRepository
|
||||
{
|
||||
/**
|
||||
* The eloquent model instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Models\MetricPoint
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a new eloquent metric point repository instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\MetricPoint $model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(MetricPoint $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new model.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function create($id, array $data)
|
||||
{
|
||||
$data['metric_id'] = $id;
|
||||
$metric = new $this->model($data);
|
||||
|
||||
$this->validate($metric);
|
||||
|
||||
$metric->saveOrFail();
|
||||
|
||||
return $metric;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function update($id, array $data)
|
||||
{
|
||||
$metric = $this->model->findOrFail($id);
|
||||
$metric->fill($data);
|
||||
|
||||
$this->validate($metric);
|
||||
|
||||
$metric->update($data);
|
||||
|
||||
return $metric;
|
||||
}
|
||||
}
|
||||
44
app/Repositories/MetricPoint/MetricPointRepository.php
Normal file
44
app/Repositories/MetricPoint/MetricPointRepository.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\MetricPoint;
|
||||
|
||||
interface MetricPointRepository
|
||||
{
|
||||
/**
|
||||
* Returns all models.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function all();
|
||||
|
||||
/**
|
||||
* Create a new model.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function create($id, array $data);
|
||||
|
||||
/**
|
||||
* Finds a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function findOrFail($id);
|
||||
|
||||
/**
|
||||
* Update a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function update($id, array $data);
|
||||
}
|
||||
Reference in New Issue
Block a user