Skip to content

Commit

Permalink
Merge pull request #14 from chernecov/moving-embed-functionality-to-v…
Browse files Browse the repository at this point in the history
…iew-layer

Moving embed functionality to view layer
  • Loading branch information
chernecov committed Aug 26, 2015
2 parents 0389eda + d66e02b commit 6602f14
Show file tree
Hide file tree
Showing 24 changed files with 420 additions and 184 deletions.
8 changes: 1 addition & 7 deletions Entity/AbstractCrudEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
namespace Ecentria\Libraries\EcentriaRestBundle\Entity;

use Ecentria\Libraries\EcentriaRestBundle\Model\CRUD\CrudEntityInterface,
Ecentria\Libraries\EcentriaRestBundle\Model\Embedded\EmbeddedInterface,
Ecentria\Libraries\EcentriaRestBundle\Model\Timestampable\TimestampableInterface,
Ecentria\Libraries\EcentriaRestBundle\Model\Transactional\TransactionalInterface,
Ecentria\Libraries\EcentriaRestBundle\Model\Transactional\TransactionalTrait,
Ecentria\Libraries\EcentriaRestBundle\Model\Timestampable\TimestampableTrait,
Ecentria\Libraries\EcentriaRestBundle\Model\Embedded\EmbeddedTrait;
Ecentria\Libraries\EcentriaRestBundle\Model\Transactional\TransactionalTrait;

/**
* Abstract CRUD entity class
Expand All @@ -25,7 +20,6 @@
*/
abstract class AbstractCrudEntity implements CrudEntityInterface
{
use EmbeddedTrait;
use TransactionalTrait;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

namespace Ecentria\Libraries\EcentriaRestBundle\EventListener;

use Ecentria\Libraries\EcentriaRestBundle\Model\CollectionResponse;
use Ecentria\Libraries\EcentriaRestBundle\Model\Embedded\EmbeddedInterface;
use Ecentria\Libraries\EcentriaRestBundle\Services\Embedded\EmbeddedManager;
use FOS\RestBundle\View\View;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;

Expand All @@ -20,33 +19,40 @@
*
* @author Sergey Chernecov <[email protected]>
*/
class ResponseListener
class EmbeddedResponseListener
{
/**
* Let's embed our response
* Embedded manager
*
* @param GetResponseForControllerResultEvent $event Event
* @var EmbeddedManager
*/
protected $embeddedManager;

/**
* Constructor
*
* @param EmbeddedManager $embeddedManager
*/
public function __construct(EmbeddedManager $embeddedManager)
{
$this->embeddedManager = $embeddedManager;
}

/**
* Setting embedded serialization groups for current response
*
* @param GetResponseForControllerResultEvent $event
*
* @return void
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$view = $event->getControllerResult();

if (!$view instanceof View) {
return;
}

$embedded = filter_var($request->get('_embedded'), FILTER_VALIDATE_BOOLEAN);
$data = $view->getData();

if ($data instanceof EmbeddedInterface && $data->showAssociations() === null) {
$data->setShowAssociations($embedded);
}

if ($data instanceof CollectionResponse) {
$data->setInheritedShowAssociations($embedded);
}
$groups = $this->embeddedManager->generateGroups($event->getRequest());
$view->getSerializationContext()->setGroups($groups);
}
}
22 changes: 13 additions & 9 deletions EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

namespace Ecentria\Libraries\EcentriaRestBundle\EventListener;

use Doctrine\Common\Collections\ArrayCollection;
use Ecentria\Libraries\EcentriaRestBundle\Model\Alias;
use Ecentria\Libraries\EcentriaRestBundle\Model\CollectionResponse;
use Ecentria\Libraries\EcentriaRestBundle\Model\Embedded\EmbeddedInterface;
use Ecentria\Libraries\EcentriaRestBundle\Services\Embedded\EmbeddedManager;
use Ecentria\Libraries\EcentriaRestBundle\Services\Transaction\TransactionResponseManager;
use FOS\RestBundle\View\View;
use JMS\Serializer\Exception\ValidationFailedException;
Expand Down Expand Up @@ -104,16 +104,20 @@ private function getValidationFailedExceptionResponse(
$request->getMethod(),
$view
);
$eventDispatcher->dispatch('kernel.view', $responseEvent);
$responseData = $view->getData();

if ($responseData instanceof EmbeddedInterface) {
$responseData->setShowAssociations(true);
if ($view->getData() instanceof ArrayCollection) {
$responseEvent->getRequest()->attributes->set(
EmbeddedManager::KEY_EMBED,
EmbeddedManager::GROUP_VIOLATION_COLLECTION
);
} else {
$responseEvent->getRequest()->attributes->set(
EmbeddedManager::KEY_EMBED,
EmbeddedManager::GROUP_VIOLATION_ENTITY
);
}

if ($responseData instanceof CollectionResponse) {
$responseData->setInheritedShowAssociations(false);
}
$eventDispatcher->dispatch('kernel.view', $responseEvent);

return $responseEvent->getResponse();
}
Expand Down
11 changes: 8 additions & 3 deletions EventListener/TransactionalListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Ecentria\Libraries\EcentriaRestBundle\Services\Transaction\TransactionBuilder,
Ecentria\Libraries\EcentriaRestBundle\Services\Transaction\TransactionResponseManager;

use Ecentria\Libraries\EcentriaRestBundle\Services\Embedded\EmbeddedManager;
use FOS\RestBundle\View\View;

use Symfony\Component\HttpKernel\Event\FilterControllerEvent,
Expand Down Expand Up @@ -157,9 +158,13 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
if ($transaction instanceof Transaction) {
$data = $view->getData();
$violations = $request->get('violations');
$view->setData(
$this->transactionResponseManager->handle($transaction, $data, $violations)
);
$view->setData($this->transactionResponseManager->handle($transaction, $data, $violations));
if (!$transaction->getSuccess()) {
$request->attributes->set(
EmbeddedManager::KEY_EMBED,
EmbeddedManager::GROUP_ALL
);
}
}
}

Expand Down
6 changes: 2 additions & 4 deletions Model/CRUD/CrudEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@

namespace Ecentria\Libraries\EcentriaRestBundle\Model\CRUD;

use Ecentria\Libraries\EcentriaRestBundle\Model\Embedded\EmbeddedInterface,
Ecentria\Libraries\EcentriaRestBundle\Model\Timestampable\TimestampableInterface,
Ecentria\Libraries\EcentriaRestBundle\Model\Transactional\TransactionalInterface;
use Ecentria\Libraries\EcentriaRestBundle\Model\Transactional\TransactionalInterface;

/**
* CRUD entity interface
*
* @author Sergey Chernecov <[email protected]>
*/
interface CrudEntityInterface extends EmbeddedInterface, TransactionalInterface
interface CrudEntityInterface extends TransactionalInterface
{
/**
* Primary key getter
Expand Down
23 changes: 2 additions & 21 deletions Model/CollectionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

use Doctrine\Common\Collections\ArrayCollection;

use Ecentria\Libraries\EcentriaRestBundle\Model\Embedded\EmbeddedInterface,
Ecentria\Libraries\EcentriaRestBundle\Model\Embedded\EmbeddedTrait,
Ecentria\Libraries\EcentriaRestBundle\Model\Transactional\TransactionalInterface,
use Ecentria\Libraries\EcentriaRestBundle\Model\Transactional\TransactionalInterface,
Ecentria\Libraries\EcentriaRestBundle\Model\Transactional\TransactionalTrait;

use JMS\Serializer\Annotation as Serializer;
Expand All @@ -24,9 +22,8 @@
*
* @author Sergey Chernecov <[email protected]>
*/
class CollectionResponse implements EmbeddedInterface, TransactionalInterface
class CollectionResponse implements TransactionalInterface
{
use EmbeddedTrait;
use TransactionalTrait;

/**
Expand Down Expand Up @@ -67,20 +64,4 @@ public function getItems()
{
return $this->items;
}

/**
* Setting association to show
*
* @param mixed $value
*
* @return void
*/
public function setInheritedShowAssociations($value)
{
foreach ($this->getItems() as $item) {
if ($item instanceof EmbeddedInterface) {
$item->setShowAssociations((bool) $value);
}
}
}
}
35 changes: 0 additions & 35 deletions Model/Embedded/EmbeddedInterface.php

This file was deleted.

53 changes: 0 additions & 53 deletions Model/Embedded/EmbeddedTrait.php

This file was deleted.

8 changes: 3 additions & 5 deletions Resources/config/serializer/Entity.AbstractCrudEntity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ Ecentria\Libraries\EcentriaRestBundle\Entity\AbstractCrudEntity:

transaction:
exclude: true
embedded:
exclude: true
showAssociations:
exclude: true

relations:

Expand All @@ -22,7 +18,9 @@ Ecentria\Libraries\EcentriaRestBundle\Entity\AbstractCrudEntity:
embedded:
content: expr(object.getTransaction())
exclusion:
exclude_if: expr(object.showAssociations() === false)
groups:
- embedded.all
- embedded.violation.entity

- rel: self
exclusion:
Expand Down
4 changes: 1 addition & 3 deletions Resources/config/serializer/Model.CollectionResponse.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
Ecentria\Libraries\EcentriaRestBundle\Model\CollectionResponse:

properties:
showAssociations:
exclude: true
transaction:
exclude: true

relations:

- rel: service-transaction
exclusion:
exclude_if: expr(object.getTransaction() === null || !object.showAssociations())
exclude_if: expr(object.getTransaction() === null)
href:
route: get_transaction
parameters:
Expand Down
4 changes: 3 additions & 1 deletion Resources/config/services/listeners.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:

ecentria.api.listener.exception.class: Ecentria\Libraries\EcentriaRestBundle\EventListener\ExceptionListener
ecentria.api.listener.response.class: Ecentria\Libraries\EcentriaRestBundle\EventListener\ResponseListener
ecentria.api.listener.response.class: Ecentria\Libraries\EcentriaRestBundle\EventListener\EmbeddedResponseListener
ecentria.api.listener.transactional.class: Ecentria\Libraries\EcentriaRestBundle\EventListener\TransactionalListener

gedmo.listener.timestampable.class: Gedmo\Timestampable\TimestampableListener
Expand All @@ -18,6 +18,8 @@ services:

ecentria.api.listener.response:
class: %ecentria.api.listener.response.class%
arguments:
- @ecentria.utils.embedded_manager
tags:
- { name: kernel.event_listener, event: kernel.view, method: onKernelView, priority: 200 }

Expand Down
2 changes: 1 addition & 1 deletion Resources/config/services/managers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ parameters:
ecentria.api.builder.notice.class: Ecentria\Libraries\EcentriaRestBundle\Services\NoticeBuilder
ecentria.api.builder.info.class: Ecentria\Libraries\EcentriaRestBundle\Services\InfoBuilder
ecentria.api.builder.transaction.class: Ecentria\Libraries\EcentriaRestBundle\Services\Transaction\TransactionBuilder

ecentria.api.configuration.manager.class: Ecentria\Libraries\EcentriaRestBundle\Services\ConfigurationManager

ecentria.api.crud.manager.class: Ecentria\Libraries\EcentriaRestBundle\Services\CRUD\CrudManager
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/services/utils.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
parameters:

ecentria.fos_rest.native_json_encode_serializer.class: Ecentria\Libraries\EcentriaRestBundle\Services\NativeJsonEncodeSerializer
ecentria.utils.embedded_manager.class: Ecentria\Libraries\EcentriaRestBundle\Services\Embedded\EmbeddedManager

services:

ecentria.utils.embedded_manager:
class: %ecentria.utils.embedded_manager.class%

ecentria.fos_rest.native_json_encode_serializer:
class: %ecentria.fos_rest.native_json_encode_serializer.class%
Loading

0 comments on commit 6602f14

Please sign in to comment.