Move code to src folder
This commit is contained in:
14
src/Repositories/Component/ComponentRepository.php
Normal file
14
src/Repositories/Component/ComponentRepository.php
Normal file
@@ -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);
|
||||
}
|
||||
28
src/Repositories/Component/EloquentComponentRepository.php
Normal file
28
src/Repositories/Component/EloquentComponentRepository.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\Component;
|
||||
|
||||
use CachetHQ\Cachet\Repositories\EloquentRepository;
|
||||
use Component;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
149
src/Repositories/EloquentRepository.php
Normal file
149
src/Repositories/EloquentRepository.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
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 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 object
|
||||
*/
|
||||
public function find($id)
|
||||
{
|
||||
return $this->model->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a model by ID.
|
||||
*
|
||||
* @param int $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 int
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
46
src/Repositories/Incident/EloquentIncidentRepository.php
Normal file
46
src/Repositories/Incident/EloquentIncidentRepository.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?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);
|
||||
|
||||
if (isset($array['component_id'])) {
|
||||
$this->hasRelationship($incident, 'component');
|
||||
}
|
||||
|
||||
$incident->saveOrFail();
|
||||
|
||||
return $incident;
|
||||
}
|
||||
|
||||
public function update($id, array $array)
|
||||
{
|
||||
$incident = $this->model->findOrFail($id);
|
||||
$incident->fill($array);
|
||||
$this->validate($incident);
|
||||
|
||||
if (isset($array['component_id'])) {
|
||||
$this->hasRelationship($incident, 'component');
|
||||
}
|
||||
|
||||
$incident->update($array);
|
||||
|
||||
return $incident;
|
||||
}
|
||||
}
|
||||
14
src/Repositories/Incident/IncidentRepository.php
Normal file
14
src/Repositories/Incident/IncidentRepository.php
Normal file
@@ -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);
|
||||
}
|
||||
39
src/Repositories/Metric/EloquentMetricRepository.php
Normal file
39
src/Repositories/Metric/EloquentMetricRepository.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
14
src/Repositories/Metric/MetricRepository.php
Normal file
14
src/Repositories/Metric/MetricRepository.php
Normal file
@@ -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,39 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\MetricPoint;
|
||||
|
||||
use CachetHQ\Cachet\Repositories\EloquentRepository;
|
||||
use MetricPoint;
|
||||
|
||||
class EloquentMetricPointRepository 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;
|
||||
}
|
||||
}
|
||||
14
src/Repositories/MetricPoint/MetricPointRepository.php
Normal file
14
src/Repositories/MetricPoint/MetricPointRepository.php
Normal file
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user