Rename component.component to component_id with relationship name change Rename more instances of parent to component Working on Incident Repository Fix Incident seeder component_id is fillable within the model Fix bad relationship Finished Incident repository Added Metric repository Updated tests for new dingo release
121 lines
2.5 KiB
PHP
121 lines
2.5 KiB
PHP
<?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;
|
|
}
|
|
}
|