Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Use ADR pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Jun 18, 2018
1 parent 5a2fcb4 commit f285934
Show file tree
Hide file tree
Showing 16 changed files with 874 additions and 161 deletions.
80 changes: 80 additions & 0 deletions src/Action/AbstractPostArchiveAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\NewsBundle\Action;

use Sonata\NewsBundle\Model\BlogInterface;
use Sonata\NewsBundle\Model\PostManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

abstract class AbstractPostArchiveAction extends Controller
{
/**
* @var BlogInterface
*/
private $blog;

/**
* @var PostManagerInterface
*/
private $postManager;

public function __construct(BlogInterface $blog, PostManagerInterface $postManager)
{
$this->blog = $blog;
$this->postManager = $postManager;
}

/**
* @internal
*
* NEXT_MAJOR: make this method protected
*
* @return Response
*/
final public function renderArchive(Request $request, array $criteria = [], array $parameters = [])
{
$pager = $this->postManager->getPager(
$criteria,
$request->get('page', 1)
);

$parameters = array_merge([
'pager' => $pager,
'blog' => $this->blog,
'tag' => false,
'collection' => false,
'route' => $request->get('_route'),
'route_parameters' => $request->get('_route_params'),
], $parameters);

$response = $this->render(
sprintf('@SonataNews/Post/archive.%s.twig', $request->getRequestFormat()),
$parameters
);

if ('rss' === $request->getRequestFormat()) {
$response->headers->set('Content-Type', 'application/rss+xml');
}

return $response;
}

/**
* @return PostManagerInterface
*/
final protected function getPostManager()
{
return $this->postManager;
}
}
56 changes: 56 additions & 0 deletions src/Action/CollectionPostArchiveAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\NewsBundle\Action;

use Sonata\ClassificationBundle\Model\CollectionManagerInterface;
use Sonata\NewsBundle\Model\BlogInterface;
use Sonata\NewsBundle\Model\PostManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

final class CollectionPostArchiveAction extends AbstractPostArchiveAction
{
/**
* @var CollectionManagerInterface
*/
private $collectionManager;

public function __construct(
BlogInterface $blog,
PostManagerInterface $postManager,
CollectionManagerInterface $collectionManager
) {
parent::__construct($blog, $postManager);

$this->collectionManager = $collectionManager;
}

/**
* @param string $tag
*
* @return Response
*/
public function __invoke(Request $request, $tag)
{
$collection = $this->collectionManager->findOneBy([
'slug' => $tag,
'enabled' => true,
]);

if (!$collection || !$collection->getEnabled()) {
throw new NotFoundHttpException('Unable to find the collection');
}

return $this->renderArchive($request, ['collection' => $collection], ['collection' => $collection]);
}
}
48 changes: 48 additions & 0 deletions src/Action/CommentListAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\NewsBundle\Action;

use Sonata\NewsBundle\Model\CommentInterface;
use Sonata\NewsBundle\Model\CommentManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

final class CommentListAction extends Controller
{
/**
* @var CommentManagerInterface
*/
private $commentManager;

public function __construct(CommentManagerInterface $commentManager)
{
$this->commentManager = $commentManager;
}

/**
* @param int $postId
*
* @return Response
*/
public function __invoke($postId)
{
$pager = $this->commentManager
->getPager([
'postId' => $postId,
'status' => CommentInterface::STATUS_VALID,
], 1, 500); //no limit

return $this->render('@SonataNews/Post/comments.html.twig', [
'pager' => $pager,
]);
}
}
139 changes: 139 additions & 0 deletions src/Action/CreateCommentAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\NewsBundle\Action;

use Sonata\NewsBundle\Form\Type\CommentType;
use Sonata\NewsBundle\Mailer\MailerInterface;
use Sonata\NewsBundle\Model\BlogInterface;
use Sonata\NewsBundle\Model\CommentManagerInterface;
use Sonata\NewsBundle\Model\PostInterface;
use Sonata\NewsBundle\Model\PostManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;

final class CreateCommentAction extends Controller
{
/**
* @var RouterInterface
*/
private $router;

/**
* @var BlogInterface
*/
private $blog;

/**
* @var PostManagerInterface
*/
private $postManager;

/**
* @var CommentManagerInterface
*/
private $commentManager;

/**
* @var FormFactoryInterface
*/
private $formFactory;

/**
* @var MailerInterface
*/
private $mailer;

public function __construct(
RouterInterface $router,
BlogInterface $blog,
PostManagerInterface $postManager,
CommentManagerInterface $commentManager,
FormFactoryInterface $formFactory,
MailerInterface $mailer
) {
$this->router = $router;
$this->blog = $blog;
$this->postManager = $postManager;
$this->commentManager = $commentManager;
$this->formFactory = $formFactory;
$this->mailer = $mailer;
}

/**
* @param string $id
* @param Request $request
*
* @throws NotFoundHttpException
*
* @return Response
*/
public function __invoke(Request $request, $id)
{
$post = $this->postManager->findOneBy([
'id' => $id,
]);

if (!$post) {
throw new NotFoundHttpException(sprintf('Post (%d) not found', $id));
}

if (!$post->isCommentable()) {
// todo add notice.
return new RedirectResponse($this->router->generate('sonata_news_view', [
'permalink' => $this->blog->getPermalinkGenerator()->generate($post),
]));
}

$form = $this->getCommentForm($post);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$comment = $form->getData();

$this->commentManager->save($comment);
$this->mailer->sendCommentNotification($comment);

// todo : add notice
return new RedirectResponse($this->router->generate('sonata_news_view', [
'permalink' => $this->blog->getPermalinkGenerator()->generate($post),
]));
}

return $this->render('@SonataNews/Post/view.html.twig', [
'post' => $post,
'form' => $form,
]);
}

/**
* @return FormInterface
*/
private function getCommentForm(PostInterface $post)
{
$comment = $this->commentManager->create();
$comment->setPost($post);
$comment->setStatus($post->getCommentsDefaultStatus());

return $this->formFactory->createNamed('comment', CommentType::class, $comment, [
'action' => $this->router->generate('sonata_news_add_comment', [
'id' => $post->getId(),
]),
'method' => 'POST',
]);
}
}
Loading

0 comments on commit f285934

Please sign in to comment.