From 56d7d77a937421f3fbdf720c76f68b5dba573bd5 Mon Sep 17 00:00:00 2001 From: Benjamin Kott Date: Fri, 19 May 2023 11:01:25 +0200 Subject: [PATCH] [TASK] Cleanup CommentFormFinisher and CommentService --- .../Domain/Finisher/CommentFormFinisher.php | 39 +++++++--- Classes/Service/CommentService.php | 75 ++++--------------- Configuration/Services.yaml | 3 + Tests/Unit/Service/CommentServiceTest.php | 41 +++++----- 4 files changed, 63 insertions(+), 95 deletions(-) diff --git a/Classes/Domain/Finisher/CommentFormFinisher.php b/Classes/Domain/Finisher/CommentFormFinisher.php index 179d0a52..d92d1ebf 100644 --- a/Classes/Domain/Finisher/CommentFormFinisher.php +++ b/Classes/Domain/Finisher/CommentFormFinisher.php @@ -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; @@ -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(); @@ -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()); @@ -85,5 +98,7 @@ protected function executeInternal() ])); $cacheService->flushCacheByTag('tx_blog_post_' . $post->getUid()); } + + return null; } } diff --git a/Classes/Service/CommentService.php b/Classes/Service/CommentService.php index bb702cec..346ff107 100644 --- a/Classes/Service/CommentService.php +++ b/Classes/Service/CommentService.php @@ -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. @@ -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; @@ -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 { @@ -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) { diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index 6252fb73..55d604e1 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -9,3 +9,6 @@ services: T3G\AgencyPack\Blog\Hooks\PageLayoutHeaderHook: public: true + + T3G\AgencyPack\Blog\Service\CommentService: + public: true diff --git a/Tests/Unit/Service/CommentServiceTest.php b/Tests/Unit/Service/CommentServiceTest.php index 6e790283..536f3c22 100644 --- a/Tests/Unit/Service/CommentServiceTest.php +++ b/Tests/Unit/Service/CommentServiceTest.php @@ -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; @@ -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 + ); } /** @@ -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); } @@ -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()); @@ -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()); @@ -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()); @@ -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); } }