* command and user actions added

* language model added
This commit is contained in:
Danyi Dávid 2016-08-15 21:45:46 +02:00
parent 15fe800840
commit 773769d7d6
25 changed files with 784 additions and 0 deletions

View 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;
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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());
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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;
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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());
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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';
}
}

View 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;
}
}

View 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);
}
}

View 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;
}
}

View 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);
}
}