Skip to content

Commit

Permalink
[TASK] Cleanup CommentFormFinisher and CommentService
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott committed May 19, 2023
1 parent 116d2b9 commit 56d7d77
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 95 deletions.
39 changes: 27 additions & 12 deletions Classes/Domain/Finisher/CommentFormFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
use T3G\AgencyPack\Blog\Notification\NotificationManager;
use T3G\AgencyPack\Blog\Service\CacheService;
use T3G\AgencyPack\Blog\Service\CommentService;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Service\ExtensionService;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;

Expand All @@ -29,32 +32,32 @@
*/
class CommentFormFinisher extends AbstractFinisher
{
protected static $messages = [
protected static array $messages = [
CommentService::STATE_ERROR => [
'title' => 'message.addComment.error.title',
'text' => 'message.addComment.error.text',
'severity' => FlashMessage::ERROR,
'severity' => AbstractMessage::ERROR,
],
CommentService::STATE_MODERATION => [
'title' => 'message.addComment.moderation.title',
'text' => 'message.addComment.moderation.text',
'severity' => FlashMessage::INFO,
'severity' => AbstractMessage::INFO,
],
CommentService::STATE_SUCCESS => [
'title' => 'message.addComment.success.title',
'text' => 'message.addComment.success.text',
'severity' => FlashMessage::OK,
'severity' => AbstractMessage::OK,
],
];

protected function executeInternal()
{
$configurationManager = $this->objectManager->get(ConfigurationManagerInterface::class);
$settings = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, 'blog');
$postRepository = $this->objectManager->get(PostRepository::class);
$cacheService = $this->objectManager->get(CacheService::class);
$commentService = $this->objectManager->get(CommentService::class);
$commentService->injectSettings($settings['comments']);
$settings = GeneralUtility::makeInstance(ConfigurationManagerInterface::class)
->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, 'blog');
$postRepository = GeneralUtility::makeInstance(PostRepository::class);
$cacheService = GeneralUtility::makeInstance(CacheService::class);
$commentService = GeneralUtility::makeInstance(CommentService::class);
$commentService->setSettings($settings['comments']);

// Create Comment
$values = $this->finisherContext->getFormValues();
Expand All @@ -64,17 +67,27 @@ protected function executeInternal()
$comment->setUrl($values['url'] ?? '');
$comment->setComment($values['comment'] ?? '');
$post = $postRepository->findCurrentPost();
if ($post === null) {
return null;
}
$state = $commentService->addComment($post, $comment);

// Add FlashMessage
$flashMessage = $this->objectManager->get(
$pluginNamespace = GeneralUtility::makeInstance(ExtensionService::class)->getPluginNamespace(
$this->finisherContext->getRequest()->getControllerExtensionName(),
$this->finisherContext->getRequest()->getPluginName()
);
$flashMessage = GeneralUtility::makeInstance(
FlashMessage::class,
LocalizationUtility::translate(self::$messages[$state]['text'], 'blog'),
LocalizationUtility::translate(self::$messages[$state]['title'], 'blog'),
self::$messages[$state]['severity'],
true
);
$this->finisherContext->getControllerContext()->getFlashMessageQueue()->addMessage($flashMessage);
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
$flashMessageService
->getMessageQueueByIdentifier('extbase.flashmessages.' . $pluginNamespace)
->addMessage($flashMessage);

if ($state !== CommentService::STATE_ERROR) {
$comment->setCrdate(new \DateTime());
Expand All @@ -85,5 +98,7 @@ protected function executeInternal()
]));
$cacheService->flushCacheByTag('tx_blog_post_' . $post->getUid());
}

return null;
}
}
75 changes: 15 additions & 60 deletions Classes/Service/CommentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use T3G\AgencyPack\Blog\Domain\Repository\PostRepository;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;

/**
* Class CommentService.
Expand All @@ -27,69 +27,30 @@ class CommentService
public const STATE_MODERATION = 'moderation';
public const STATE_SUCCESS = 'success';

/**
* @var PostRepository
*/
protected $postRepository;

/**
* @var CommentRepository
*/
protected $commentRepository;
protected PostRepository $postRepository;
protected CommentRepository $commentRepository;
protected PersistenceManagerInterface $persistenceManager;

/**
* @var PersistenceManager
*/
protected $persistenceManager;
public function __construct(
PostRepository $postRepository,
CommentRepository $commentRepository,
PersistenceManagerInterface $persistenceManager
) {
$this->postRepository = $postRepository;
$this->commentRepository = $commentRepository;
$this->persistenceManager = $persistenceManager;
}

/**
* @var array
*/
protected $settings = [
protected array $settings = [
'active' => 0,
'moderation' => 0,
];

/**
* @param array $settings
*/
public function injectSettings(array $settings): void
public function setSettings(array $settings): void
{
$this->settings = $settings;
}

/**
* @param \T3G\AgencyPack\Blog\Domain\Repository\PostRepository $postRepository
*/
public function injectPostRepository(PostRepository $postRepository): void
{
$this->postRepository = $postRepository;
}

/**
* @param \T3G\AgencyPack\Blog\Domain\Repository\CommentRepository $commentRepository
*/
public function injectCommentRepository(CommentRepository $commentRepository): void
{
$this->commentRepository = $commentRepository;
}

/**
* @param PersistenceManager $persistenceManager
*/
public function injectPersistenceManager(PersistenceManager $persistenceManager): void
{
$this->persistenceManager = $persistenceManager;
}

/**
* @param Post $post
* @param Comment $comment
* @return string
* @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
*/
public function addComment(Post $post, Comment $comment): string
{
$result = self::STATE_ERROR;
Expand Down Expand Up @@ -126,9 +87,6 @@ public function addComment(Post $post, Comment $comment): string
/**
* This method checks if an comment exists for the same email
* address in the given comment.
*
* @param Comment $comment
* @return bool
*/
protected function approvedCommentExistsForSameEmail(Comment $comment): bool
{
Expand All @@ -142,10 +100,7 @@ protected function approvedCommentExistsForSameEmail(Comment $comment): bool
}

/**
* @param Post $post
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function getCommentsByPost(Post $post)
{
Expand Down
3 changes: 3 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ services:

T3G\AgencyPack\Blog\Hooks\PageLayoutHeaderHook:
public: true

T3G\AgencyPack\Blog\Service\CommentService:
public: true
41 changes: 18 additions & 23 deletions Tests/Unit/Service/CommentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use T3G\AgencyPack\Blog\Domain\Model\Comment;
use T3G\AgencyPack\Blog\Domain\Model\Post;
use T3G\AgencyPack\Blog\Domain\Repository\CommentRepository;
use T3G\AgencyPack\Blog\Domain\Repository\PostRepository;
use T3G\AgencyPack\Blog\Service\CommentService;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
Expand All @@ -22,24 +23,23 @@
class CommentServiceTest extends UnitTestCase
{
protected bool $resetSingletonInstances = true;

/**
* @var MockObject
*/
protected $postRepositoryMock;

/**
* @var CommentService
*/
protected $commentService;
protected MockObject $postRepositoryMock;
protected MockObject $commentRepositoryMock;
protected MockObject $persistenceManagerMock;
protected CommentService $commentService;

public function initialize(): void
{
$GLOBALS['TSFE'] = $this->getMockBuilder(TypoScriptFrontendController::class)->disableOriginalConstructor()->getMock();
$this->postRepositoryMock = $this->getMockBuilder(PostRepository::class)->disableOriginalConstructor()->getMock();
$this->commentService = new CommentService();
$this->commentService->injectPostRepository($this->postRepositoryMock);
$this->commentService->injectPersistenceManager($this->getMockBuilder(PersistenceManager::class)->disableOriginalConstructor()->getMock());
$this->commentRepositoryMock = $this->getMockBuilder(CommentRepository::class)->disableOriginalConstructor()->getMock();
$this->persistenceManagerMock = $this->getMockBuilder(PersistenceManager::class)->disableOriginalConstructor()->getMock();

$this->commentService = new CommentService(
$this->postRepositoryMock,
$this->commentRepositoryMock,
$this->persistenceManagerMock
);
}

/**
Expand All @@ -52,7 +52,7 @@ public function inactiveCommentsReturnErrorOnAdd(): void
$post = new Post();
$post->_setProperty('uid', 1);
$comment = new Comment();
$result = (new CommentService())->addComment($post, $comment);
$result = $this->commentService->addComment($post, $comment);

self::assertSame(CommentService::STATE_ERROR, $result);
}
Expand All @@ -67,9 +67,8 @@ public function activeCommentsWithoutModerationReturnSuccessOnAdd(): void
$post = new Post();
$post->_setProperty('uid', 1);
$comment = new Comment();
$settings = ['active' => 1, 'moderation' => 0];

$this->commentService->injectSettings($settings);
$this->commentService->setSettings(['active' => 1, 'moderation' => 0]);
$result = $this->commentService->addComment($post, $comment);

self::assertEquals(0, $comment->getHidden());
Expand All @@ -86,9 +85,8 @@ public function activeCommentsWithModerationReturnModerationOnAdd(): void
$post = new Post();
$post->_setProperty('uid', 1);
$comment = new Comment();
$settings = ['active' => 1, 'moderation' => 1];

$this->commentService->injectSettings($settings);
$this->commentService->setSettings(['active' => 1, 'moderation' => 1]);
$result = $this->commentService->addComment($post, $comment);

self::assertEquals(Comment::STATUS_PENDING, $comment->getStatus());
Expand All @@ -106,9 +104,7 @@ public function commentGetsAddedToPost(): void
$post->_setProperty('uid', 1);
$comment = new Comment();

$settings = ['active' => 1, 'moderation' => 0];

$this->commentService->injectSettings($settings);
$this->commentService->setSettings(['active' => 1, 'moderation' => 0]);
$this->commentService->addComment($post, $comment);

self::assertSame($comment, $post->getComments()->current());
Expand All @@ -128,9 +124,8 @@ public function postGetsUpdatedInDatabase(): void
$post = new Post();
$post->_setProperty('uid', 1);
$comment = new Comment();
$settings = ['active' => 1, 'moderation' => 0];

$this->commentService->injectSettings($settings);
$this->commentService->setSettings(['active' => 1, 'moderation' => 0]);
$this->commentService->addComment($post, $comment);
}
}

0 comments on commit 56d7d77

Please sign in to comment.