From 4754cc515391e6b6a18de74b8c17ee0911e77e5c Mon Sep 17 00:00:00 2001 From: Johannes Wachter Date: Tue, 9 May 2017 16:15:52 +0200 Subject: [PATCH] extracted mehods to allow easier normalization and serialization of article (#170) --- Controller/WebsiteArticleController.php | 50 +++++++++++++++++++------ 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/Controller/WebsiteArticleController.php b/Controller/WebsiteArticleController.php index ead86b38c..27ca64024 100644 --- a/Controller/WebsiteArticleController.php +++ b/Controller/WebsiteArticleController.php @@ -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; @@ -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() @@ -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) - ); } /**