Created
November 23, 2018 09:52
-
-
Save renalpha/6ab2623017ef8d2154893f2b6b89b6d7 to your computer and use it in GitHub Desktop.
Domain Driven Design Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Infrastructure\Repositories; | |
use Domain\Common\Entity; | |
use Domain\Contracts\Repository\AbstractRepositoryInterface; | |
use Illuminate\Database\Eloquent\Model; | |
/** | |
* Class AbstractRepository | |
* @package Infrastructure\Repositories | |
*/ | |
abstract class AbstractRepository implements AbstractRepositoryInterface | |
{ | |
/** | |
* @var Model | |
*/ | |
protected $model; | |
/** | |
* AbstractRepository constructor. | |
* @param Entity $model | |
*/ | |
public function __construct(Entity $model) | |
{ | |
$this->model = $model; | |
} | |
/** | |
* @param array $attributes | |
* @return mixed | |
*/ | |
public function create(array $attributes) | |
{ | |
return $this->model->create($attributes); | |
} | |
/** | |
* @param array $attributes | |
* @param $id | |
* @return mixed | |
*/ | |
public function update(array $attributes, $id) | |
{ | |
$model = $this->model->findOrFail($id); | |
$model->fill($attributes); | |
$model->save(); | |
return $model; | |
} | |
/** | |
* @param $id | |
* @return mixed | |
*/ | |
public function findOrFail($id) | |
{ | |
$model = $this->model->findOrFail($id); | |
return $model; | |
} | |
/** | |
* @param $id | |
* @return mixed | |
*/ | |
public function delete($id) | |
{ | |
$model = $this->model->findOrFail($id); | |
$model->delete(); | |
return $model; | |
} | |
/** | |
* @param $id | |
* @return mixed | |
*/ | |
public function find($id) | |
{ | |
return $this->model->find($id); | |
} | |
/* | |
* Get all (selected columns) | |
*/ | |
public function all($columns = ['*']) | |
{ | |
return $this->model->all($columns); | |
} | |
public function select($columns = ['*']) | |
{ | |
return $this->model->select($columns); | |
} | |
/* | |
* Get Lists | |
*/ | |
public function lists($column = ['*'], $key = null) | |
{ | |
return $this->model->pluck($column, $key); | |
} | |
/* | |
* Get Lists | |
*/ | |
public function pluck($column = ['*'], $key = null) | |
{ | |
return $this->model->pluck($column, $key); | |
} | |
/* | |
* Get Lists | |
*/ | |
public function get($filters = null) | |
{ | |
return $this->model->get(); | |
} | |
/** | |
* @param $amount | |
* @return mixed | |
*/ | |
public function paginate($amount) | |
{ | |
return $this->model->paginate($amount); | |
} | |
/** | |
* @param $query | |
* @return mixed | |
*/ | |
public function search($query) | |
{ | |
try { | |
return $this->model->search($query); | |
} catch (\Exception $e) { | |
dd('ERROR No searchable threat has been added to this modal!'); | |
} | |
} | |
/** | |
* @return mixed | |
*/ | |
public function first() | |
{ | |
return $this->model->first(); | |
} | |
/** | |
* @return mixed | |
*/ | |
public function firstOrFail() | |
{ | |
return $this->model->firstOrFail(); | |
} | |
/** | |
* Check if entity has relation | |
* | |
* @param string $relation | |
* | |
* @return $this | |
*/ | |
public function has($relation) | |
{ | |
$this->model = $this->model->has($relation); | |
return $this; | |
} | |
/** | |
* Load relations | |
* | |
* @param array|string $relations | |
* | |
* @return $this | |
*/ | |
public function with($relations) | |
{ | |
$this->model = $this->model->with($relations); | |
return $this; | |
} | |
/** | |
* Load relation with closure | |
* | |
* @param string $relation | |
* @param closure $closure | |
* | |
* @return $this | |
*/ | |
public function whereHas($relation, $closure) | |
{ | |
$this->model = $this->model->whereHas($relation, $closure); | |
return $this; | |
} | |
/** | |
* @param $column | |
* @return $this | |
*/ | |
public function whereNull($column) | |
{ | |
$this->model = $this->model->whereNull($column); | |
return $this; | |
} | |
/** | |
* @param $column | |
* @return $this | |
*/ | |
public function whereNotNull($column) | |
{ | |
$this->model = $this->model->whereNotNull($column); | |
return $this; | |
} | |
/** | |
* @param $column | |
* @param string $direction | |
* @return $this | |
*/ | |
public function orderBy($column, $direction = 'asc') | |
{ | |
$this->model = $this->model->orderBy($column, $direction); | |
return $this; | |
} | |
/** | |
* @param $column | |
* @param $condition | |
* @param $value | |
* @return $this | |
*/ | |
public function where($column, $condition, $value) | |
{ | |
$this->model = $this->model->where($column, $condition, $value); | |
return $this; | |
} | |
/** | |
* @param $column | |
* @param $condition | |
* @param $value | |
* @return $this | |
*/ | |
public function orWhere($column, $condition, $value) | |
{ | |
$this->model = $this->model->orWhere($column, $condition, $value); | |
return $this; | |
} | |
/** | |
* @param $column | |
* @param $value | |
* @return $this | |
*/ | |
public function whereBetween($column, $value) | |
{ | |
$this->model = $this->model->whereBetween($column, $value); | |
return $this; | |
} | |
public function whereTranslation($column, $value) | |
{ | |
$this->model = $this->model->whereTranslation($column, $value); | |
return $this; | |
} | |
/** | |
* @param $column | |
* @param $value | |
* @return $this | |
*/ | |
public function whereIn($column, $value) | |
{ | |
$this->model = $this->model->whereIn($column, $value); | |
return $this; | |
} | |
/** | |
* @param $column | |
* @return $this | |
*/ | |
public function groupBy($column) | |
{ | |
$this->model = $this->model->groupBy($column); | |
return $this; | |
} | |
/** | |
* @param null $column | |
* @return $this | |
*/ | |
public function distinct($column = null) | |
{ | |
$this->model = $this->model->distinct($column); | |
return $this; | |
} | |
/** | |
* Set visible fields | |
* | |
* @param array $fields | |
* | |
* @return $this | |
*/ | |
public function visible(array $fields) | |
{ | |
$this->model->setVisible($fields); | |
return $this; | |
} | |
/** | |
* @param $key | |
* @return mixed | |
*/ | |
public function getAttribute($key) | |
{ | |
return $this->model->getAttribute(snake_case($key)); | |
} | |
/** | |
* @param $key | |
* @param $value | |
* @return mixed | |
*/ | |
public function setAttribute($key, $value) | |
{ | |
return $this->model->setAttribute(snake_case($key), $value); | |
} | |
/** | |
* @param int $state | |
* @return mixed | |
*/ | |
public function active($state = 1) | |
{ | |
return $this->model->where('active', '=', $state); | |
} | |
/** | |
* @param int $id | |
* @return int | |
*/ | |
public function findBy(int $id) | |
{ | |
return $this->model->findBy($id); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Domain\Services; | |
use Infrastructure\Repositories\AbstractRepository; | |
/** | |
* Class AbstractService | |
* @package Domain\Services | |
*/ | |
abstract class AbstractService | |
{ | |
/** | |
* @var AbstractRepository | |
*/ | |
public $repository; | |
/** | |
* @param array $params | |
* @return mixed | |
*/ | |
public function create(array $params) | |
{ | |
$entity = $this->repository->create($params); | |
$entity->save(); | |
return $entity; | |
} | |
/** | |
* @param int $id | |
* @param array $params | |
* @return mixed | |
*/ | |
public function update(int $id, array $params) | |
{ | |
$entity = $this->repository->update($params, $id); | |
$entity->save(); | |
return $entity; | |
} | |
/** | |
* @param int $id | |
*/ | |
public function remove(int $id) { | |
$this->delete($id); | |
} | |
/** | |
* @param int $id | |
*/ | |
public function delete(int $id) | |
{ | |
$this->repository->delete($id); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Domain\Common; | |
/** | |
* Class AggregateRoot | |
* | |
* @package Domain\Common | |
*/ | |
class AggregateRoot extends Entity | |
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Domain\Common; | |
use Illuminate\Database\Eloquent\Model; | |
/** | |
* Class Entity | |
* @package Domain\Entities | |
*/ | |
abstract class Entity extends Model | |
{ | |
/** | |
* @var int | |
*/ | |
protected $id; | |
/** | |
* @var \DateTimeInterface | |
*/ | |
protected $createdAt; | |
/** | |
* @var \DateTimeInterface | |
*/ | |
protected $updatedAt; | |
/** | |
* @var | |
*/ | |
protected $deletedAt; | |
/** | |
* @param int $id | |
*/ | |
public function setId(int $id) | |
{ | |
$this->id = $id; | |
} | |
/** | |
* @return int | |
*/ | |
public function getId(): int | |
{ | |
return $this->id; | |
} | |
/** | |
* @return \DateTimeInterface | |
*/ | |
public function getCreatedAt(): \DateTimeInterface | |
{ | |
return $this->createdAt; | |
} | |
/** | |
* @return \DateTimeInterface | |
*/ | |
public function getUpdatedAt(): \DateTimeInterface | |
{ | |
return $this->updatedAt; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getCreatedAtHumansAttribute() | |
{ | |
return $this->created_at->diffForHumans(); | |
} | |
/** | |
* Generates an unique iterated name. | |
* | |
* @param $column | |
* @param $name | |
* @return string | |
*/ | |
protected function generateIteratedName($column, $name): string | |
{ | |
$entity = new $this; | |
try { | |
// Requires soft delete | |
$existing = $entity->withTrashed(); | |
} catch (\Exception $e) { | |
$existing = $entity; | |
} | |
$existing->where($column, 'LIKE', "{$name}%") | |
->orderBy($column, 'desc') | |
->get(); | |
if ($existing->count() > 0) { | |
$sequence = (int)str_replace($name, '', $existing->first()->{$column}); | |
return $name . ($sequence + 1); | |
} else { | |
return $name; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment