Skip to content

Instantly share code, notes, and snippets.

@erandirjunior
Last active February 16, 2019 14:06
Show Gist options
  • Save erandirjunior/f4335bf50e6f9b7aff43adf5cd9bb817 to your computer and use it in GitHub Desktop.
Save erandirjunior/f4335bf50e6f9b7aff43adf5cd9bb817 to your computer and use it in GitHub Desktop.
Classe de repository
<?php
namespace Plenus\Repositories\Configuracao\Geral;
use Plenus\Entities\TbProgramaModalidade;
use Plenus\Repositories\Repository;
class ModalidadeRepository extends Repository
{
public function __construct()
{
$this->name = TbProgramaModalidade::class;
parent::__construct();
}
public function findAll()
{
return $this->em
->createQuery("SELECT
PM.id,
PM.nome
FROM
Plenus\Entities\TbProgramaModalidade PM
WHERE
PM.excluido IS NULL")
->getResult();
}
}
<?php
namespace Plenus\Services\Configuracao\Geral;
use Plenus\Entities\TbProgramaModalidade;
use Plenus\Helpers\{ArrayHelper,ObjectHelper,Validator};
use Plenus\Helpers\Messages\ResponseMessage;
use Plenus\Rules\Configuracao\Geral\ModalidadeRule;
use Plenus\Services\Service;
use Plenus\Repositories\Configuracao\Geral\ModalidadeRepository;
class ModalidadeService extends Service
{
private $repository;
private $rule;
public function __construct()
{
$this->repository = new ModalidadeRepository();
$this->rule = new ModalidadeRule();
}
public function index()
{
return $this->setResponse($this->repository->findAll())->response();
}
public function create($request)
{
$requestData = Validator::clearAllData($request->all());
if ($this->isValid($requestData) || $this->hasData($requestData)) {
return $this->response();
}
$this->setResponse(ResponseMessage::ERRO_SALVAR_REGISTRO, 500, false);
$object = ObjectHelper::setDataObject(new TbProgramaModalidade(), $requestData);
if ($this->repository->create($object)) {
$this->setResponse([], 201);
}
return $this->response();
}
public function isValid($data)
{
$validate = $this->rule->validateByEntity($data);
$this->setResponse($validate->getErrors(), 400, false);
return $validate->getFails();
}
public function hasData($data)
{
$this->setResponse(ResponseMessage::ERRO_DUPLICACAO, 400, false);
return $this->repository->count($data) != 0 ? true : false;
}
public function update($request)
{
$requestData = Validator::clearAllData($request->all());
$this->setResponse(ResponseMessage::ERRO_VALIDACAO, 400, false);
if (!Validator::isValid($requestData['id'], FILTER_VALIDATE_INT) && $this->isValid($requestData)) {
return $this->response();
}
if ($this->repository->exists2(ArrayHelper::only($requestData, ['nome', 'sigla']), $requestData['id'])) {
return $this->setResponse(ResponseMessage::ERRO_DUPLICACAO, 400, false)->response();
}
$object = $this->repository->find($requestData['id'])->toObject();
$object = ObjectHelper::setDataObject($object, $requestData, false);
if ($this->repository->edit($object)) {
$this->setResponse([], 204)->response();
}
return $this->setResponse(ResponseMessage::ERRO_ATUALIZAR_REGISTRO, 500, false);
}
public function delete($request)
{
$id = Validator::isValid($request->parameter('id'), FILTER_VALIDATE_INT);
if (!$id) {
return $this->setResponse(ResponseMessage::ERRO_VALIDACAO, 400, false)->response();
}
$secretaria = $this->repository->find($id)->toObject();
if (!$secretaria) {
return $this->setResponse(ResponseMessage::ERRO_REMOVER_REGISTRO, 500, false)->response();
}
$secretaria->setExcluido(new \DateTime());
if ($this->repository->edit($secretaria)) {
$this->setResponse(ResponseMessage::SUCESSO_REMOVER_REGISTRO)->response();
}
return $this->setResponse(ResponseMessage::ERRO_REMOVER_REGISTRO, 500, false);
}
public function findById($request)
{
$id = $request->parameter('id');
$data = $this->repository->find($id)->toArray(['id', 'nome']);
return $this->setResponse($data)->response();
}
}
namespace Plenus\Repositories;
use Config\Database\EntityDB;
use Doctrine\ORM\Query\ResultSetMapping;
use Plenus\Helpers\ObjectHelper;
abstract class Repository
{
protected $em;
private $result;
protected $name;
public function __construct()
{
$entityManager = new EntityDB();
$this->em = $entityManager->getConn();
return $this;
}
public function getEm()
{
return $this->em;
}
public function create($object)
{
$this->em->persist($object);
return is_null($this->em->flush()) ? true : false;
}
public function edit($object)
{
$this->em->merge($object);
return is_null($this->em->flush()) ? true : false;
}
public function __call($name, $arguments)
{
$criteria = $arguments[0];
$orderBy = empty($arguments[1]) ? null : $arguments[1];
$limit = empty($arguments[2]) ? null : $arguments[1];
$offset = empty($arguments[3]) ? null : $arguments[1];
$this->result = $this->em->getRepository($this->name)->$name($criteria, $orderBy, $limit, $offset);
return $this;
}
protected function simpleOrderBy($table, $columnsDefault, $pagination)
{
$order = $pagination['descending'] == "false" ? 'ASC' : 'DESC';
$sort = "ORDER BY {$table}.{$columnsDefault} {$order}";
if ($pagination['sortBy'] != "null") {
$sort = "ORDER BY {$table}.{$pagination['sortBy']} {$order}";
}
return $sort;
}
public function find($id)
{
$this->result = $this->em->getRepository($this->name)->find($id);
return $this;
}
public function count(array $criteria)
{
return $this->em->getRepository($this->name)->count($criteria);
}
public function exists2(array $criteria, $id)
{
$criteria['excluido'] = !empty($criteria['excluido']) ? $criteria['excluido'] : null;
$data = $this->findBy($criteria)->toArray(['id']);
if (!$data) {
return false;
}
foreach ($data as $d) {
if ($d['id'] != $id) {
return true;
}
}
return false;
}
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$this->result = $this->em->getRepository($this->name)->findBy($criteria, $orderBy, $limit, $offset);
return $this;
}
public function toObject()
{
return $this->result;
}
public function toArray(array $properties = [], $toDate = true, $format = 'd/m/Y')
{
return ObjectHelper::toArray($this->result, $properties, $toDate, $format);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment