Skip to content

Commit

Permalink
extracted mehods to allow easier normalization and serialization of a…
Browse files Browse the repository at this point in the history
…rticle (#170)
  • Loading branch information
wachterjohannes authored and alexander-schranz committed May 9, 2017
1 parent 8d2ec2a commit 4754cc5
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions Controller/WebsiteArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sulu\Bundle\ArticleBundle\Controller;

use JMS\Serializer\SerializationContext;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticleInterface;
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
use Sulu\Component\HttpCache\HttpCache;
Expand Down Expand Up @@ -52,15 +53,48 @@ public function indexAction(Request $request, ArticleInterface $object, $view, $
*/
protected function renderArticle(Request $request, ArticleInterface $object, $view, $pageNumber, $attributes = [])
{
if ($object instanceof ArticlePageDocument) {
// this is necessary because the preview system passes an article-page here.
$object = $object->getParent();
}
$object = $this->normalizeArticle($object);

$requestFormat = $request->getRequestFormat();
$viewTemplate = $view . '.' . $requestFormat . '.twig';

$content = $this->get('jms_serializer')->serialize(
$content = $this->serializeArticle($object, $pageNumber);

return $this->render(
$viewTemplate,
$this->get('sulu_website.resolver.template_attribute')->resolve(array_merge($content, $attributes)),
$this->createResponse($request)
);
}

/**
* Returns all the times the article-document.
* This is necessary because the preview system passes an article-page here.
*
* @param ArticleInterface $object
*
* @return ArticleDocument
*/
protected function normalizeArticle(ArticleInterface $object)
{
if ($object instanceof ArticlePageDocument) {
return $object->getParent();
}

return $object;
}

/**
* Serialize given article with page-number.
*
* @param ArticleInterface $object
* @param int $pageNumber
*
* @return array
*/
protected function serializeArticle(ArticleInterface $object, $pageNumber)
{
return $this->get('jms_serializer')->serialize(
$object,
'array',
SerializationContext::create()
Expand All @@ -69,12 +103,6 @@ protected function renderArticle(Request $request, ArticleInterface $object, $vi
->setAttribute('website', true)
->setAttribute('pageNumber', $pageNumber)
);

return $this->render(
$viewTemplate,
$this->get('sulu_website.resolver.template_attribute')->resolve(array_merge($content, $attributes)),
$this->createResponse($request)
);
}

/**
Expand Down

0 comments on commit 4754cc5

Please sign in to comment.