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 16, 2018
1 parent 5a2fcb4 commit 524831b
Show file tree
Hide file tree
Showing 14 changed files with 681 additions and 114 deletions.
84 changes: 84 additions & 0 deletions src/Action/AbstractPostArchiveAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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;

/**
* AbstractPostArchiveAction constructor.
*
* @param BlogInterface $blog
* @param PostManagerInterface $postManager
*/
public function __construct(BlogInterface $blog, PostManagerInterface $postManager)
{
$this->blog = $blog;
$this->postManager = $postManager;
}

/**
* 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]);
}
}
53 changes: 53 additions & 0 deletions src/Action/CommentListAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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;

/**
* CommentListAction constructor.
*
* @param CommentManagerInterface $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,
]);
}
}
41 changes: 41 additions & 0 deletions src/Action/HomeAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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 Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;

final class HomeAction
{
/**
* @var RouterInterface
*/
private $router;

/**
* HomeAction constructor.
*
* @param RouterInterface $router
*/
public function __construct(RouterInterface $router)
{
$this->router = $router;
}

/**
* @return RedirectResponse
*/
public function __invoke()
{
return new RedirectResponse($this->router->generate('sonata_news_archive'));
}
}
94 changes: 94 additions & 0 deletions src/Action/ModerateCommentAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?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\CommentManagerInterface;
use Sonata\NewsBundle\Util\HashGeneratorInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

final class ModerateCommentAction
{
/**
* @var RouterInterface
*/
private $router;

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

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

/**
* @var HashGeneratorInterface
*/
private $hashGenerator;

/**
* ModerateCommentAction constructor.
*
* @param RouterInterface $router
* @param BlogInterface $blog
* @param CommentManagerInterface $commentManager
* @param HashGeneratorInterface $hashGenerator
*/
public function __construct(
RouterInterface $router,
BlogInterface $blog,
CommentManagerInterface $commentManager,
HashGeneratorInterface $hashGenerator
) {
$this->router = $router;
$this->blog = $blog;
$this->commentManager = $commentManager;
$this->hashGenerator = $hashGenerator;
}

/**
* @param string $commentId
* @param string $hash
* @param string $status
*
* @throws AccessDeniedException
*
* @return RedirectResponse
*/
public function __invoke($commentId, $hash, $status)
{
$comment = $this->commentManager->findOneBy(['id' => $commentId]);

if (!$comment) {
throw new AccessDeniedException();
}

$computedHash = $this->hashGenerator->generate($comment);

if ($computedHash != $hash) {
throw new AccessDeniedException();
}

$comment->setStatus($status);

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

return new RedirectResponse($this->router->generate('sonata_news_view', [
'permalink' => $this->blog->getPermalinkGenerator()->generate($comment->getPost()),
]));
}
}
34 changes: 34 additions & 0 deletions src/Action/MonthlyPostArchiveAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class MonthlyPostArchiveAction extends AbstractPostArchiveAction
{
/**
* @param string $year
* @param string $month
*
* @return Response
*/
public function __invoke(Request $request, $year, $month)
{
return $this->renderArchive($request, [
'date' => $this->getPostManager()->getPublicationDateQueryParts(
sprintf('%d-%d-%d', $year, $month, 1),
'month'
),
], []);
}
}
26 changes: 26 additions & 0 deletions src/Action/PostArchiveAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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 Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class PostArchiveAction extends AbstractPostArchiveAction
{
/**
* @return Response
*/
public function __invoke(Request $request)
{
return $this->renderArchive($request);
}
}
Loading

0 comments on commit 524831b

Please sign in to comment.