* command and user actions added
* language model added
This commit is contained in:
parent
15fe800840
commit
773769d7d6
37
src/App/Action/Comment/DeleteAction.php
Normal file
37
src/App/Action/Comment/DeleteAction.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Comment;
|
||||
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class DeleteAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityServiceInterface
|
||||
*/
|
||||
private $entityService;
|
||||
|
||||
public function __construct(EntityServiceInterface $entityService)
|
||||
{
|
||||
$this->entityService = $entityService;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$id = $request->getAttribute('id');
|
||||
|
||||
$result = $this->entityService->delete($id);
|
||||
|
||||
$return = new JsonResponse($result);
|
||||
|
||||
if (false === $result) {
|
||||
return $return->withStatus(500, 'Failed to delete record.');
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
17
src/App/Action/Comment/DeleteFactory.php
Normal file
17
src/App/Action/Comment/DeleteFactory.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Comment;
|
||||
|
||||
use App\Action\Article\DeleteAction;
|
||||
use App\Service\Article\CommentService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class DeleteFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$entityService = $container->get(CommentService::class);
|
||||
return new DeleteAction($entityService);
|
||||
}
|
||||
}
|
||||
35
src/App/Action/Comment/GetAction.php
Normal file
35
src/App/Action/Comment/GetAction.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Comment;
|
||||
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class GetAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityServiceInterface
|
||||
*/
|
||||
private $entityService;
|
||||
|
||||
public function __construct(EntityServiceInterface $entityService)
|
||||
{
|
||||
$this->entityService = $entityService;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$id = $request->getAttribute('id');
|
||||
$entity = $this->entityService->get($id);
|
||||
|
||||
if (null === $entity) {
|
||||
$ret = new JsonResponse(false);
|
||||
return $ret->withStatus(404);
|
||||
}
|
||||
|
||||
return new JsonResponse($entity);
|
||||
}
|
||||
}
|
||||
17
src/App/Action/Comment/GetFactory.php
Normal file
17
src/App/Action/Comment/GetFactory.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Comment;
|
||||
|
||||
use App\Action\Article\GetAction;
|
||||
use App\Service\Article\CommentService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class GetFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$entityService = $container->get(CommentService::class);
|
||||
return new GetAction($entityService);
|
||||
}
|
||||
}
|
||||
27
src/App/Action/Comment/ListAction.php
Normal file
27
src/App/Action/Comment/ListAction.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Comment;
|
||||
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class ListAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityServiceInterface
|
||||
*/
|
||||
private $entityService;
|
||||
|
||||
public function __construct(EntityServiceInterface $entityService)
|
||||
{
|
||||
$this->entityService = $entityService;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return new JsonResponse($this->entityService->getList());
|
||||
}
|
||||
}
|
||||
17
src/App/Action/Comment/ListFactory.php
Normal file
17
src/App/Action/Comment/ListFactory.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Comment;
|
||||
|
||||
use App\Action\Article\ListAction;
|
||||
use App\Service\Article\CommentService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class ListFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$entityService = $container->get(CommentService::class);
|
||||
return new ListAction($entityService);
|
||||
}
|
||||
}
|
||||
37
src/App/Action/Comment/PostAction.php
Normal file
37
src/App/Action/Comment/PostAction.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Comment;
|
||||
|
||||
use App\Action\AbstractFormAction;
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class PostAction extends AbstractFormAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityServiceInterface
|
||||
*/
|
||||
private $entityService;
|
||||
|
||||
public function __construct(EntityServiceInterface $entityService)
|
||||
{
|
||||
$this->entityService = $entityService;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$data = $this->getRequestData($request);
|
||||
|
||||
$result = $this->entityService->create($data);
|
||||
|
||||
if (false === $result) {
|
||||
$result = new JsonResponse(false);
|
||||
return $result->withStatus(500, 'Failed to insert record.');
|
||||
}
|
||||
|
||||
return new JsonResponse($result);
|
||||
}
|
||||
}
|
||||
17
src/App/Action/Comment/PostFactory.php
Normal file
17
src/App/Action/Comment/PostFactory.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Comment;
|
||||
|
||||
use App\Action\Article\PostAction;
|
||||
use App\Service\Article\CommentService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class PostFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$entityService = $container->get(CommentService::class);
|
||||
return new PostAction($entityService);
|
||||
}
|
||||
}
|
||||
38
src/App/Action/Comment/PutAction.php
Normal file
38
src/App/Action/Comment/PutAction.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Comment;
|
||||
|
||||
use App\Action\AbstractFormAction;
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class PutAction extends AbstractFormAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityServiceInterface
|
||||
*/
|
||||
private $entityService;
|
||||
|
||||
public function __construct(EntityServiceInterface $entityService)
|
||||
{
|
||||
$this->entityService = $entityService;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$id = $request->getAttribute('id', false);
|
||||
$data = $this->getRequestData($request);
|
||||
|
||||
$result = $this->entityService->modify($id, $data);
|
||||
|
||||
if (false === $result) {
|
||||
$result = new JsonResponse(false);
|
||||
return $result->withStatus(500, 'Failed to update record.');
|
||||
}
|
||||
|
||||
return new JsonResponse($result);
|
||||
}
|
||||
}
|
||||
17
src/App/Action/Comment/PutFactory.php
Normal file
17
src/App/Action/Comment/PutFactory.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Comment;
|
||||
|
||||
use App\Action\Article\PutAction;
|
||||
use App\Service\Article\CommentService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class PutFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$entityService = $container->get(CommentService::class);
|
||||
return new PutAction($entityService);
|
||||
}
|
||||
}
|
||||
37
src/App/Action/User/DeleteAction.php
Normal file
37
src/App/Action/User/DeleteAction.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\User;
|
||||
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class DeleteAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityServiceInterface
|
||||
*/
|
||||
private $entityService;
|
||||
|
||||
public function __construct(EntityServiceInterface $entityService)
|
||||
{
|
||||
$this->entityService = $entityService;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$id = $request->getAttribute('id');
|
||||
|
||||
$result = $this->entityService->delete($id);
|
||||
|
||||
$return = new JsonResponse($result);
|
||||
|
||||
if (false === $result) {
|
||||
return $return->withStatus(500, 'Failed to delete record.');
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
17
src/App/Action/User/DeleteFactory.php
Normal file
17
src/App/Action/User/DeleteFactory.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\User;
|
||||
|
||||
use App\Action\Article\DeleteAction;
|
||||
use App\Service\Article\UserService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class DeleteFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$entityService = $container->get(UserService::class);
|
||||
return new DeleteAction($entityService);
|
||||
}
|
||||
}
|
||||
35
src/App/Action/User/GetAction.php
Normal file
35
src/App/Action/User/GetAction.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\User;
|
||||
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class GetAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityServiceInterface
|
||||
*/
|
||||
private $entityService;
|
||||
|
||||
public function __construct(EntityServiceInterface $entityService)
|
||||
{
|
||||
$this->entityService = $entityService;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$id = $request->getAttribute('id');
|
||||
$entity = $this->entityService->get($id);
|
||||
|
||||
if (null === $entity) {
|
||||
$ret = new JsonResponse(false);
|
||||
return $ret->withStatus(404);
|
||||
}
|
||||
|
||||
return new JsonResponse($entity);
|
||||
}
|
||||
}
|
||||
17
src/App/Action/User/GetFactory.php
Normal file
17
src/App/Action/User/GetFactory.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\User;
|
||||
|
||||
use App\Action\Article\GetAction;
|
||||
use App\Service\Article\UserService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class GetFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$entityService = $container->get(UserService::class);
|
||||
return new GetAction($entityService);
|
||||
}
|
||||
}
|
||||
27
src/App/Action/User/ListAction.php
Normal file
27
src/App/Action/User/ListAction.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\User;
|
||||
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class ListAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityServiceInterface
|
||||
*/
|
||||
private $entityService;
|
||||
|
||||
public function __construct(EntityServiceInterface $entityService)
|
||||
{
|
||||
$this->entityService = $entityService;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return new JsonResponse($this->entityService->getList());
|
||||
}
|
||||
}
|
||||
17
src/App/Action/User/ListFactory.php
Normal file
17
src/App/Action/User/ListFactory.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\User;
|
||||
|
||||
use App\Action\Article\ListAction;
|
||||
use App\Service\Article\UserService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class ListFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$entityService = $container->get(UserService::class);
|
||||
return new ListAction($entityService);
|
||||
}
|
||||
}
|
||||
37
src/App/Action/User/PostAction.php
Normal file
37
src/App/Action/User/PostAction.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\User;
|
||||
|
||||
use App\Action\AbstractFormAction;
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class PostAction extends AbstractFormAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityServiceInterface
|
||||
*/
|
||||
private $entityService;
|
||||
|
||||
public function __construct(EntityServiceInterface $entityService)
|
||||
{
|
||||
$this->entityService = $entityService;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$data = $this->getRequestData($request);
|
||||
|
||||
$result = $this->entityService->create($data);
|
||||
|
||||
if (false === $result) {
|
||||
$result = new JsonResponse(false);
|
||||
return $result->withStatus(500, 'Failed to insert record.');
|
||||
}
|
||||
|
||||
return new JsonResponse($result);
|
||||
}
|
||||
}
|
||||
17
src/App/Action/User/PostFactory.php
Normal file
17
src/App/Action/User/PostFactory.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\User;
|
||||
|
||||
use App\Action\Article\PostAction;
|
||||
use App\Service\Article\UserService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class PostFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$entityService = $container->get(UserService::class);
|
||||
return new PostAction($entityService);
|
||||
}
|
||||
}
|
||||
38
src/App/Action/User/PutAction.php
Normal file
38
src/App/Action/User/PutAction.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\User;
|
||||
|
||||
use App\Action\AbstractFormAction;
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class PutAction extends AbstractFormAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityServiceInterface
|
||||
*/
|
||||
private $entityService;
|
||||
|
||||
public function __construct(EntityServiceInterface $entityService)
|
||||
{
|
||||
$this->entityService = $entityService;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$id = $request->getAttribute('id', false);
|
||||
$data = $this->getRequestData($request);
|
||||
|
||||
$result = $this->entityService->modify($id, $data);
|
||||
|
||||
if (false === $result) {
|
||||
$result = new JsonResponse(false);
|
||||
return $result->withStatus(500, 'Failed to update record.');
|
||||
}
|
||||
|
||||
return new JsonResponse($result);
|
||||
}
|
||||
}
|
||||
17
src/App/Action/User/PutFactory.php
Normal file
17
src/App/Action/User/PutFactory.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\User;
|
||||
|
||||
use App\Action\Article\PutAction;
|
||||
use App\Service\Article\UserService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class PutFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$entityService = $container->get(UserService::class);
|
||||
return new PutAction($entityService);
|
||||
}
|
||||
}
|
||||
36
src/App/Model/Language.php
Normal file
36
src/App/Model/Language.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
class Language
|
||||
{
|
||||
|
||||
const LANG_ENGLICH = 'eng';
|
||||
const LANG_GERMAN = 'fra';
|
||||
const LANG_FRENCH = 'ger';
|
||||
const LANG_SWEDISH = 'sve';
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private static $languages = [
|
||||
self::LANG_ENGLICH => 'english',
|
||||
self::LANG_GERMAN => 'german',
|
||||
self::LANG_FRENCH => 'french',
|
||||
self::LANG_SWEDISH => 'swedish',
|
||||
];
|
||||
|
||||
/**
|
||||
* Returns all available
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getAllLanguages()
|
||||
{
|
||||
return self::$languages;
|
||||
}
|
||||
|
||||
public static function getLanguageByCode(string $language)
|
||||
{
|
||||
return self::$languages[$language] ?? 'unknown';
|
||||
}
|
||||
}
|
||||
99
src/App/Service/Comment/CommentService.php
Normal file
99
src/App/Service/Comment/CommentService.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Comment;
|
||||
|
||||
use App\Entity\Comment;
|
||||
use App\Hydrator\DoctrineObject;
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\Query;
|
||||
use Exception;
|
||||
|
||||
class CommentService implements EntityServiceInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* @var DoctrineObject
|
||||
*/
|
||||
private $hydrator;
|
||||
|
||||
public function __construct(EntityManager $em, DoctrineObject $hydrator)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->hydrator = $hydrator;
|
||||
}
|
||||
|
||||
public function getList()
|
||||
{
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
$entities = $qb->select('c,u,a')
|
||||
->from(Comment::class, 'c')
|
||||
->leftJoin('c.author', 'u')
|
||||
->leftJoin('c.article', 'a')
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
return $entities;
|
||||
}
|
||||
|
||||
public function get($id)
|
||||
{
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
|
||||
$entity = $qb->select('c,u,a')
|
||||
->from(Comment::class, 'c')
|
||||
->leftJoin('c.author', 'u')
|
||||
->leftJoin('c.article', 'a')
|
||||
->where('c.id = :cid')
|
||||
->setParameter('cid', $id)
|
||||
->getQuery()
|
||||
->getOneOrNullResult(Query::HYDRATE_ARRAY);
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
public function create($data)
|
||||
{
|
||||
$entity = $this->hydrator->hydrate($data, new Comment());
|
||||
try {
|
||||
$this->em->persist($entity);
|
||||
$this->em->flush();
|
||||
} catch (Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
return $entity;
|
||||
}
|
||||
|
||||
public function modify($id, $data)
|
||||
{
|
||||
if (null === ($entity = $this->em->find(Comment::class, $id))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$entity = $this->hydrator->hydrate($data, $entity);
|
||||
$this->em->persist($entity);
|
||||
$this->em->flush();
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
if (null === ($entity = $this->em->find(Comment::class, $id))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->em->remove($entity);
|
||||
$this->em->flush();
|
||||
} catch (Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
16
src/App/Service/Comment/CommentServiceFactory.php
Normal file
16
src/App/Service/Comment/CommentServiceFactory.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Comment;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class ArticleServiceFactory extends \App\Service\AbstractEntityServiceFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$em = $this->getEntityManager($container);
|
||||
$hydrator = $this->getDoctrineHydrator($container);
|
||||
return new CommentService($em, $hydrator);
|
||||
}
|
||||
}
|
||||
99
src/App/Service/User/UserService.php
Normal file
99
src/App/Service/User/UserService.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\User;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Hydrator\DoctrineObject;
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\Query;
|
||||
use Exception;
|
||||
|
||||
class UserService implements EntityServiceInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* @var DoctrineObject
|
||||
*/
|
||||
private $hydrator;
|
||||
|
||||
public function __construct(EntityManager $em, DoctrineObject $hydrator)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->hydrator = $hydrator;
|
||||
}
|
||||
|
||||
public function getList()
|
||||
{
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
$entities = $qb->select('u,a,c')
|
||||
->from(User::class, 'u')
|
||||
->leftJoin('u.articles', 'a')
|
||||
->leftJoin('u.comments', 'c')
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
return $entities;
|
||||
}
|
||||
|
||||
public function get($id)
|
||||
{
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
|
||||
$entity = $qb->select('u,a,c')
|
||||
->from(User::class, 'u')
|
||||
->leftJoin('u.articles', 'a')
|
||||
->leftJoin('u.comments', 'c')
|
||||
->where('u.id = :uid')
|
||||
->setParameter('uid', $id)
|
||||
->getQuery()
|
||||
->getOneOrNullResult(Query::HYDRATE_ARRAY);
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
public function create($data)
|
||||
{
|
||||
$entity = $this->hydrator->hydrate($data, new User());
|
||||
try {
|
||||
$this->em->persist($entity);
|
||||
$this->em->flush();
|
||||
} catch (Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
return $entity;
|
||||
}
|
||||
|
||||
public function modify($id, $data)
|
||||
{
|
||||
if (null === ($entity = $this->em->find(User::class, $id))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$entity = $this->hydrator->hydrate($data, $entity);
|
||||
$this->em->persist($entity);
|
||||
$this->em->flush();
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
if (null === ($entity = $this->em->find(User::class, $id))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->em->remove($entity);
|
||||
$this->em->flush();
|
||||
} catch (Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
16
src/App/Service/User/UserServiceFactory.php
Normal file
16
src/App/Service/User/UserServiceFactory.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\User;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class UserServiceFactory extends \App\Service\AbstractEntityServiceFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$em = $this->getEntityManager($container);
|
||||
$hydrator = $this->getDoctrineHydrator($container);
|
||||
return new userService($em, $hydrator);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user