Skip to content

Instantly share code, notes, and snippets.

@renalpha
Last active August 27, 2018 14:57
Show Gist options
  • Save renalpha/705214b45925054cf0b4ddee700793eb to your computer and use it in GitHub Desktop.
Save renalpha/705214b45925054cf0b4ddee700793eb to your computer and use it in GitHub Desktop.
Interface binding
<?php
namespace Domain\Abstractions;
/**
* Class AbstractDomainService
*
* @package Domain\Abstractions
*/
abstract class AbstractDomainService
{
/**
* @var
*/
public $repository;
/**
* AbstractDomainService constructor.
* @param $repositoryContract
*/
public function __construct($repositoryContract)
{
$this->repository = $repositoryContract;
}
/**
* @param $arrayRecords
* @return string
*/
public function batchCreate($arrayRecords)
{
foreach ($arrayRecords as $data) {
$entity = $this->loadNew($data);
$this->repository->onlySave($entity);
}
return 'Records Sucessfully saved!';
}
public function findAll()
{
return $this->repository->findAll();
}
public function findAllFiltered($filter)
{
return $this->repository->findAllFiltered($filter);
}
public function find($entityId)
{
return $this->repository->find($entityId);
}
public function findBy($arrKeyValue)
{
return $this->repository->findBy($arrKeyValue);
}
public function findAllBy($arrKeyValue)
{
return $this->repository->findAllBy($arrKeyValue);
}
public function findAllByFilter($filter)
{
return $this->repository->findAll($filter);
}
public function loadNew($post)
{
return $this->repository->loadNew($post);
}
public function getEntity()
{
return $this->repository->getEntity();
}
public function create($post)
{
if (!is_object($post)) {
$post = $this->loadNew($post);
}
return $this->repository->save($post);
}
public function save($entity)
{
return $this->repository->save($entity);
}
public function update($entityId, $post)
{
$entity = $this->find($entityId);
return $this->repository->update($entity, $post);
}
public function updateEntity($entity, $post)
{
return $this->repository->update($entity, $post);
}
public function delete($entityId)
{
return $this->repository->deleteById($entityId);
}
public function alreadyExists($parameter, $exceptionMessage)
{
$user = $this->repository->findBy($parameter);
if (!is_null($user)) {
throw new \Exception($exceptionMessage, 401);
}
}
public function count($companyId)
{
return $this->repository->count($companyId);
}
}
<?php
namespace Infrastructure\Repositories;
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 Model $model
*/
public function __construct(Model $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 get($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 getAll()
{
return $this->model->get();
}
public function paginate($amount)
{
return $this->model->paginate($amount);
}
public function search($query)
{
try {
return $this->model->search($query);
} catch (\Exception $e) {
dd('ERROR No searchable threat has been added to this modal!');
}
}
public function first()
{
return $this->model->first();
}
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;
}
public function whereNull($column)
{
$this->model = $this->model->whereNull($column);
return $this;
}
public function whereNotNull($column)
{
$this->model = $this->model->whereNotNull($column);
return $this;
}
public function orderBy($column, $direction = 'asc')
{
$this->model = $this->model->orderBy($column, $direction);
return $this;
}
public function where($column, $condition, $value)
{
$this->model = $this->model->where($column, $condition, $value);
return $this;
}
public function orWhere($column, $condition, $value)
{
$this->model = $this->model->orWhere($column, $condition, $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;
}
public function whereIn($column, $value)
{
$this->model = $this->model->whereIn($column, $value);
return $this;
}
public function groupBy($column)
{
$this->model = $this->model->groupBy($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;
}
public function getAttribute($key)
{
return $this->model->getAttribute(snake_case($key));
}
public function setAttribute($key, $value)
{
return $this->model->setAttribute(snake_case($key), $value);
}
public function active($state = 1)
{
return $this->model->where('active', '=', $state);
}
}
<?php
namespace App\Providers;
use App\Models\Ticket;
use Domain\Contracts\Repository\TicketRepositoryContract;
use Illuminate\Support\ServiceProvider;
use Infrastructure\Repositories\TicketRepository;
/**
* Class RepositoryServiceProvider
* @package App\Providers
*/
class RepositoryServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(TicketRepositoryContract::class, function(){
return new TicketRepository(new Ticket());
});
//... rest of repositories.
}
}
<?php
namespace App\Http\Controllers;
/**
* Class TicketController
* @package App\Http\Controllers
*/
class TicketController extends Controller
{
/**
* @var TicketService
*/
private $ticketService;
/**
* TicketController constructor.
* @param TicketService $ticketService
*/
public function __construct(TicketService $ticketService)
{
$this->ticketService = $ticketService;
}
<?php
namespace Infrastructure\Repositories;
use Domain\Contracts\Repository\TicketRepositoryContract;
use Illuminate\Database\Eloquent\Model;
/**
* Class TicketRepository
* @package Infrastructure\Repositories
*/
class TicketRepository extends AbstractRepository implements TicketRepositoryContract
{
/**
* TicketRepository constructor.
* @param Model $model
*/
public function __construct(Model $model)
{
parent::__construct($model);
}
}
<?php
namespace Domain\Service;
/**
* Class TicketService
* @package Domain\Services
*/
class TicketService extends AbstractDomainService implements TicketServiceContract
{
/**
* TicketService constructor.
* @param TicketRepositoryContract $repositoryContract
*/
public function __construct(TicketRepositoryContract $repositoryContract)
{
parent::__construct($repositoryContract);
}
<?php
namespace Domain\Contracts\Service;
/**
* Interface TicketServiceContract
* @package Domain\Contracts\Service
*/
interface TicketServiceContract{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment