From cc0416ee38c4f82ff247c607f6f2fc75fd31c354 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 21 Sep 2022 12:21:49 +0200 Subject: [PATCH 1/8] build(symfony): change to sf 5 or 6 --- .phpunit.result.cache | 1 + Controller/MessageController.php | 141 +++++++++++------- DataTransformer/RecipientsDataTransformer.php | 2 +- DependencyInjection/Configuration.php | 2 +- FormFactory/AbstractMessageFormFactory.php | 2 +- FormFactory/FactoryInterface.php | 14 ++ FormFactory/NewThreadMessageFormFactory.php | 3 +- FormFactory/ReplyMessageFormFactory.php | 2 +- FormHandler/AbstractMessageFormHandler.php | 2 +- FormHandler/FormHandlerInterface.php | 27 ++++ FormType/NewThreadMessageFormType.php | 2 +- FormType/NewThreadMultipleMessageFormType.php | 2 +- FormType/RecipientsType.php | 4 +- FormType/ReplyMessageFormType.php | 2 +- Resources/config/config.xml | 17 ++- Tests/Functional/Entity/User.php | 9 +- Tests/Functional/Entity/UserProvider.php | 6 +- .../Form/UserToUsernameTransformer.php | 4 +- Tests/Functional/TestKernel.php | 36 +++-- Tests/Functional/WebTestCase.php | 2 +- Tests/Twig/Extension/MessageExtensionTest.php | 2 +- Twig/{Extension => }/MessageExtension.php | 4 +- composer.json | 24 +-- 23 files changed, 199 insertions(+), 111 deletions(-) create mode 100644 .phpunit.result.cache create mode 100644 FormFactory/FactoryInterface.php create mode 100644 FormHandler/FormHandlerInterface.php rename Twig/{Extension => }/MessageExtension.php (97%) diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 00000000..c344da23 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":{"FOS\\MessageBundle\\Document\\ThreadDenormalizerTest::testDenormalize":2,"FOS\\MessageBundle\\Tests\\Functional\\FunctionalTest::testController":3,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testIsReadReturnsTrueWhenRead":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testIsReadReturnsFalseWhenNotRead":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testCanDeleteThreadWhenHasPermission":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testCanDeleteThreadWhenNoPermission":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testIsThreadDeletedByParticipantWhenDeleted":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetNbUnreadCacheStartsEmpty":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetNbUnread":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetNbUnreadStoresCache":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetName":4},"times":{"FOS\\MessageBundle\\Document\\ThreadDenormalizerTest::testDenormalize":0.003,"FOS\\MessageBundle\\Tests\\EntityManager\\ThreadManagerTest::testDoCreatedByAndAt":0.008,"FOS\\MessageBundle\\Tests\\EntityManager\\ThreadManagerTest::testDoCreatedByAndAtWithCreatedBy":0,"FOS\\MessageBundle\\Tests\\EntityManager\\ThreadManagerTest::testDoCreatedByAndAtWithCreatedAt":0,"FOS\\MessageBundle\\Tests\\EntityManager\\ThreadManagerTest::testDoCreatedByAndAtWithCreatedAtAndBy":0,"FOS\\MessageBundle\\Tests\\EntityManager\\ThreadManagerTest::testDoCreatedByAndNoMessage":0,"FOS\\MessageBundle\\Tests\\Functional\\FunctionalTest::testController":0.293,"FOS\\MessageBundle\\Tests\\Model\\ThreadTest::testGetOtherParticipants":0.001,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testIsReadReturnsTrueWhenRead":0.001,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testIsReadReturnsFalseWhenNotRead":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testCanDeleteThreadWhenHasPermission":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testCanDeleteThreadWhenNoPermission":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testIsThreadDeletedByParticipantWhenDeleted":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetNbUnreadCacheStartsEmpty":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetNbUnread":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetNbUnreadStoresCache":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetName":0}} \ No newline at end of file diff --git a/Controller/MessageController.php b/Controller/MessageController.php index 9fdcfbbc..a654948c 100644 --- a/Controller/MessageController.php +++ b/Controller/MessageController.php @@ -2,23 +2,62 @@ namespace FOS\MessageBundle\Controller; +use FOS\MessageBundle\Deleter\DeleterInterface; +use FOS\MessageBundle\FormFactory\FactoryInterface; +use FOS\MessageBundle\FormHandler\FormHandlerInterface; +use FOS\MessageBundle\ModelManager\ThreadManagerInterface; use FOS\MessageBundle\Provider\ProviderInterface; +use FOS\MessageBundle\Search\FinderInterface; +use FOS\MessageBundle\Search\QueryFactoryInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\DependencyInjection\ContainerInterface; +// use Symfony\Component\DependencyInjection\ContainerInterface; class MessageController extends AbstractController { - public function __construct(ContainerInterface $container) - { - $this->setContainer($container); - } + /** + * @var ProviderInterface + */ + protected $provider; + + /** + * @var FactoryInterface + */ + protected $replyFormfactory; + + /** + * @var FormHandlerInterface + */ + protected $replyFormHandler; /** - * @var ContainerInterface + * @var FactoryInterface */ - protected $container; + protected $newThreadFormFactory; + + /** + * @var FormHandlerInterface + */ + protected $newThreadFormHandler; + + // public function __construct(ContainerInterface $container) + // { + // $this->setContainer($container); + // } + public function __construct( + ProviderInterface $provider, + FactoryInterface $replyFormfactory, + FormHandlerInterface $replyFormHandler, + FactoryInterface $newThreadFormFactory, + FormHandlerInterface $newThreadFormHandler + ) { + $this->provider = $provider; + $this->replyFormfactory = $replyFormfactory; + $this->replyFormHandler = $replyFormHandler; + $this->newThreadFormFactory = $newThreadFormFactory; + $this->newThreadFormHandler = $newThreadFormHandler; + } /** * Displays the authenticated participant inbox. @@ -27,7 +66,7 @@ public function __construct(ContainerInterface $container) */ public function inboxAction() { - $threads = $this->getProvider()->getInboxThreads(); + $threads = $this->provider->getInboxThreads(); return $this->render('@FOSMessage/Message/inbox.html.twig', array( 'threads' => $threads, @@ -41,7 +80,7 @@ public function inboxAction() */ public function sentAction() { - $threads = $this->getProvider()->getSentThreads(); + $threads = $this->provider->getSentThreads(); return $this->render('@FOSMessage/Message/sent.html.twig', array( 'threads' => $threads, @@ -55,7 +94,7 @@ public function sentAction() */ public function deletedAction() { - $threads = $this->getProvider()->getDeletedThreads(); + $threads = $this->provider->getDeletedThreads(); return $this->render('@FOSMessage/Message/deleted.html.twig', array( 'threads' => $threads, @@ -71,14 +110,16 @@ public function deletedAction() */ public function threadAction($threadId) { - $thread = $this->getProvider()->getThread($threadId); - $form = $this->container->get('fos_message.reply_form.factory')->create($thread); - $formHandler = $this->container->get('fos_message.reply_form.handler'); - - if ($message = $formHandler->process($form)) { - return new RedirectResponse($this->container->get('router')->generate('fos_message_thread_view', array( - 'threadId' => $message->getThread()->getId(), - ))); + $thread = $this->provider->getThread($threadId); + // $form = $this->container->get('fos_message.reply_form.factory')->create($thread); + $form = $this->replyFormfactory->create($thread); + // $formHandler = $this->container->get('fos_message.reply_form.handler'); + + if ($message = $this->replyFormHandler->process($form)) { + return $this->redirectToRoute('fos_message_thread_view', ['threadId' => $message->getThread()->getId()]); + // return new RedirectResponse($this->container->get('router')->generate('fos_message_thread_view', array( + // 'threadId' => $message->getThread()->getId(), + // ))); } return $this->render('@FOSMessage/Message/thread.html.twig', array( @@ -94,13 +135,15 @@ public function threadAction($threadId) */ public function newThreadAction() { - $form = $this->container->get('fos_message.new_thread_form.factory')->create(); - $formHandler = $this->container->get('fos_message.new_thread_form.handler'); - - if ($message = $formHandler->process($form)) { - return new RedirectResponse($this->container->get('router')->generate('fos_message_thread_view', array( - 'threadId' => $message->getThread()->getId(), - ))); + // $form = $this->container->get('fos_message.new_thread_form.factory')->create(); + $form = $this->newThreadFormFactory->create(); + // $formHandler = $this->container->get('fos_message.new_thread_form.handler'); + + if ($message = $this->newThreadFormHandler->process($form)) { + return $this->redirectToRoute('fos_message_thread_view', ['threadId' => $message->getThread()->getId()]); + // return new RedirectResponse($this->container->get('router')->generate('fos_message_thread_view', array( + // 'threadId' => $message->getThread()->getId(), + // ))); } return $this->render('@FOSMessage/Message/newThread.html.twig', array( @@ -116,13 +159,16 @@ public function newThreadAction() * * @return RedirectResponse */ - public function deleteAction($threadId) + public function deleteAction($threadId, DeleterInterface $deleter, ThreadManagerInterface $threadManager) { - $thread = $this->getProvider()->getThread($threadId); - $this->container->get('fos_message.deleter')->markAsDeleted($thread); - $this->container->get('fos_message.thread_manager')->saveThread($thread); - - return new RedirectResponse($this->container->get('router')->generate('fos_message_inbox')); + $thread = $this->provider->getThread($threadId); + // $this->container->get('fos_message.deleter')->markAsDeleted($thread); + $deleter->markAsDeleted($thread); + // $this->container->get('fos_message.thread_manager')->saveThread($thread); + $threadManager->saveThread($thread); + + // return new RedirectResponse($this->container->get('router')->generate('fos_message_inbox')); + return $this->redirectToRoute('fos_message_inbox'); } /** @@ -132,13 +178,16 @@ public function deleteAction($threadId) * * @return RedirectResponse */ - public function undeleteAction($threadId) + public function undeleteAction($threadId, DeleterInterface $deleter, ThreadManagerInterface $threadManager) { - $thread = $this->getProvider()->getThread($threadId); - $this->container->get('fos_message.deleter')->markAsUndeleted($thread); - $this->container->get('fos_message.thread_manager')->saveThread($thread); - - return new RedirectResponse($this->container->get('router')->generate('fos_message_inbox')); + $thread = $this->provider->getThread($threadId); + // $this->container->get('fos_message.deleter')->markAsUndeleted($thread); + // $this->container->get('fos_message.thread_manager')->saveThread($thread); + $deleter->markAsUndeleted($thread); + $threadManager->saveThread($thread); + + // return new RedirectResponse($this->container->get('router')->generate('fos_message_inbox')); + return $this->redirectToRoute('fos_message_inbox'); } /** @@ -146,24 +195,16 @@ public function undeleteAction($threadId) * * @return Response */ - public function searchAction() + public function searchAction(QueryFactoryInterface $queryFactory, FinderInterface $finder) { - $query = $this->container->get('fos_message.search_query_factory')->createFromRequest(); - $threads = $this->container->get('fos_message.search_finder')->find($query); + // $query = $this->container->get('fos_message.search_query_factory')->createFromRequest(); + // $threads = $this->container->get('fos_message.search_finder')->find($query); + $query = $queryFactory->createFromRequest(); + $threads = $finder->find($query); return $this->render('@FOSMessage/Message/search.html.twig', array( 'query' => $query, 'threads' => $threads, )); } - - /** - * Gets the provider service. - * - * @return ProviderInterface - */ - protected function getProvider() - { - return $this->container->get('fos_message.provider'); - } } diff --git a/DataTransformer/RecipientsDataTransformer.php b/DataTransformer/RecipientsDataTransformer.php index 02c7809d..d437ea8c 100644 --- a/DataTransformer/RecipientsDataTransformer.php +++ b/DataTransformer/RecipientsDataTransformer.php @@ -33,7 +33,7 @@ public function __construct(DataTransformerInterface $userToUsernameTransformer) * * @return string */ - public function transform($recipients) + public function transform($recipients): mixed { if (null === $recipients || 0 === $recipients->count()) { return ''; diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index bc39b192..884a160a 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -26,7 +26,7 @@ public function getConfigTreeBuilder() $rootNode = $treeBuilder->root(self::ROOT_NAME); } - $rootNode + $treeBuilder->getRootNode() ->children() ->scalarNode('db_driver')->cannotBeOverwritten()->isRequired()->cannotBeEmpty()->end() ->scalarNode('thread_class')->isRequired()->cannotBeEmpty()->end() diff --git a/FormFactory/AbstractMessageFormFactory.php b/FormFactory/AbstractMessageFormFactory.php index fe6ace22..e0ae3801 100644 --- a/FormFactory/AbstractMessageFormFactory.php +++ b/FormFactory/AbstractMessageFormFactory.php @@ -11,7 +11,7 @@ * * @author Thibault Duplessis */ -abstract class AbstractMessageFormFactory +abstract class AbstractMessageFormFactory implements FactoryInterface { /** * The Symfony form factory. diff --git a/FormFactory/FactoryInterface.php b/FormFactory/FactoryInterface.php new file mode 100644 index 00000000..309d0af1 --- /dev/null +++ b/FormFactory/FactoryInterface.php @@ -0,0 +1,14 @@ +formFactory->createNamed($this->formName, $this->formType, $this->createModelInstance()); } diff --git a/FormFactory/ReplyMessageFormFactory.php b/FormFactory/ReplyMessageFormFactory.php index 27cc599a..89632899 100644 --- a/FormFactory/ReplyMessageFormFactory.php +++ b/FormFactory/ReplyMessageFormFactory.php @@ -19,7 +19,7 @@ class ReplyMessageFormFactory extends AbstractMessageFormFactory * * @return FormInterface */ - public function create(ThreadInterface $thread) + public function create(?ThreadInterface $thread = null) { $message = $this->createModelInstance(); $message->setThread($thread); diff --git a/FormHandler/AbstractMessageFormHandler.php b/FormHandler/AbstractMessageFormHandler.php index 08e3aa75..32a9c79c 100644 --- a/FormHandler/AbstractMessageFormHandler.php +++ b/FormHandler/AbstractMessageFormHandler.php @@ -17,7 +17,7 @@ * * @author Thibault Duplessis */ -abstract class AbstractMessageFormHandler +abstract class AbstractMessageFormHandler implements FormHandlerInterface { protected $request; protected $composer; diff --git a/FormHandler/FormHandlerInterface.php b/FormHandler/FormHandlerInterface.php new file mode 100644 index 00000000..b6545516 --- /dev/null +++ b/FormHandler/FormHandlerInterface.php @@ -0,0 +1,27 @@ + - - + + + + + + + + + + @@ -58,7 +63,7 @@ - + diff --git a/Tests/Functional/Entity/User.php b/Tests/Functional/Entity/User.php index 5ebbe971..317f8df1 100644 --- a/Tests/Functional/Entity/User.php +++ b/Tests/Functional/Entity/User.php @@ -3,16 +3,17 @@ namespace FOS\MessageBundle\Tests\Functional\Entity; use FOS\MessageBundle\Model\ParticipantInterface; +use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; -class User implements ParticipantInterface, UserInterface +class User implements ParticipantInterface, UserInterface, PasswordAuthenticatedUserInterface { - public function getUsername() + public function getUserIdentifier(): string { return 'guilhem'; } - public function getPassword() + public function getPassword(): ?string { return 'pass'; } @@ -21,7 +22,7 @@ public function getSalt() { } - public function getRoles() + public function getRoles(): array { return array(); } diff --git a/Tests/Functional/Entity/UserProvider.php b/Tests/Functional/Entity/UserProvider.php index 704995d7..102740be 100644 --- a/Tests/Functional/Entity/UserProvider.php +++ b/Tests/Functional/Entity/UserProvider.php @@ -7,17 +7,17 @@ class UserProvider implements UserProviderInterface { - public function loadUserByUsername($username) + public function loadUserByIdentifier($username): UserInterface { return new User(); } - public function refreshUser(UserInterface $user) + public function refreshUser(UserInterface $user): UserInterface { return $user; } - public function supportsClass($class) + public function supportsClass($class): bool { return User::class === $class; } diff --git a/Tests/Functional/Form/UserToUsernameTransformer.php b/Tests/Functional/Form/UserToUsernameTransformer.php index 915ab171..65377f14 100644 --- a/Tests/Functional/Form/UserToUsernameTransformer.php +++ b/Tests/Functional/Form/UserToUsernameTransformer.php @@ -8,10 +8,10 @@ class UserToUsernameTransformer implements DataTransformerInterface { - public function transform($value) + public function transform($value): ?string { if (null === $value) { - return; + return null; } if (!$value instanceof User) { diff --git a/Tests/Functional/TestKernel.php b/Tests/Functional/TestKernel.php index 22f04dc9..8ca97425 100644 --- a/Tests/Functional/TestKernel.php +++ b/Tests/Functional/TestKernel.php @@ -17,7 +17,7 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Kernel; -use Symfony\Component\Routing\RouteCollectionBuilder; +use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; /** * @author Guilhem N. @@ -29,14 +29,14 @@ class TestKernel extends Kernel /** * {@inheritdoc} */ - public function registerBundles() + public function registerBundles(): iterable { - $bundles = array( + $bundles = [ new FrameworkBundle(), new SecurityBundle(), new TwigBundle(), new FOSMessageBundle(), - ); + ]; return $bundles; } @@ -44,7 +44,7 @@ public function registerBundles() /** * {@inheritdoc} */ - protected function configureRoutes(RouteCollectionBuilder $routes) + protected function configureRoutes(RoutingConfigurator $routes) { $routes->import('@FOSMessageBundle/Resources/config/routing.xml'); } @@ -54,30 +54,28 @@ protected function configureRoutes(RouteCollectionBuilder $routes) */ protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) { - $c->loadFromExtension('framework', array( + $c->loadFromExtension('framework', [ 'secret' => 'MySecretKey', 'test' => null, 'form' => null, - 'templating' => array( - 'engines' => array('twig'), - ), - )); + 'http_method_override' => false + ]); - $c->loadFromExtension('security', array( - 'providers' => array('permissive' => array('id' => 'app.user_provider')), - 'encoders' => array('FOS\MessageBundle\Tests\Functional\Entity\User' => 'plaintext'), - 'firewalls' => array('main' => array('http_basic' => true)), - )); + $c->loadFromExtension('security', [ + 'providers' => ['permissive' => ['id' => 'app.user_provider']], + 'password_hashers' => ['FOS\MessageBundle\Tests\Functional\Entity\User' => 'plaintext'], + 'firewalls' => ['main' => ['http_basic' => true]], + ]); - $c->loadFromExtension('twig', array( + $c->loadFromExtension('twig', [ 'strict_variables' => '%kernel.debug%', - )); + ]); - $c->loadFromExtension('fos_message', array( + $c->loadFromExtension('fos_message', [ 'db_driver' => 'orm', 'thread_class' => Thread::class, 'message_class' => Message::class, - )); + ]); $c->register('fos_user.user_to_username_transformer', UserToUsernameTransformer::class); $c->register('app.user_provider', UserProvider::class); diff --git a/Tests/Functional/WebTestCase.php b/Tests/Functional/WebTestCase.php index e853eed5..8364df2c 100644 --- a/Tests/Functional/WebTestCase.php +++ b/Tests/Functional/WebTestCase.php @@ -6,7 +6,7 @@ class WebTestCase extends BaseWebTestCase { - protected static function getKernelClass() + protected static function getKernelClass(): string { return 'FOS\MessageBundle\Tests\Functional\TestKernel'; } diff --git a/Tests/Twig/Extension/MessageExtensionTest.php b/Tests/Twig/Extension/MessageExtensionTest.php index 85597bc8..2f8ab94d 100644 --- a/Tests/Twig/Extension/MessageExtensionTest.php +++ b/Tests/Twig/Extension/MessageExtensionTest.php @@ -2,7 +2,7 @@ namespace FOS\MessageBundle\Tests\Twig\Extension; -use FOS\MessageBundle\Twig\Extension\MessageExtension; +use FOS\MessageBundle\Twig\MessageExtension; use PHPUnit\Framework\TestCase; /** diff --git a/Twig/Extension/MessageExtension.php b/Twig/MessageExtension.php similarity index 97% rename from Twig/Extension/MessageExtension.php rename to Twig/MessageExtension.php index dba25a4c..ef4ab5df 100644 --- a/Twig/Extension/MessageExtension.php +++ b/Twig/MessageExtension.php @@ -1,6 +1,6 @@ =5.5.9", + "php": ">=7.2", "doctrine/collections": "^1.3", - "symfony/form": "^3.4|^4.0", - "symfony/framework-bundle": "^3.4|^4.0", - "symfony/security": "^3.4|^4.0", - "symfony/twig-bundle": "^3.4|^4.0" + "symfony/form": "^5.0|^6.0", + "symfony/framework-bundle": "^5.0|^6.0", + "symfony/security-bundle": "^5.0|^6.0", + "symfony/twig-bundle": "^5.0|^6.0" }, "require-dev": { - "symfony/validator": "^3.4|^4.0", - "symfony/translation": "^3.4|^4.0", - "symfony/yaml": "^3.4|^4.0", - "symfony/security-bundle": "^3.4|^4.0", - "symfony/templating": "^3.4|^4.0", - "symfony/browser-kit": "^3.4|^4.0", - "symfony/phpunit-bridge": "^4.2.8", + "symfony/validator": "^5.0|^6.0", + "symfony/translation": "^5.0|^6.0", + "symfony/yaml": "^5.0|^6.0", + "symfony/security-bundle": "^5.0|^6.0", + "symfony/templating": "^5.0|^6.0", + "symfony/browser-kit": "^5.0|^6.0", + "symfony/phpunit-bridge": "^6.1", "doctrine/orm": "^2.0" }, "suggest": { From 75184f989b588a8f45ac14f120b1b8a0a2dc6630 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 21 Sep 2022 12:41:25 +0200 Subject: [PATCH 2/8] chore(array): change syntax for array --- .gitignore | 1 + .phpunit.result.cache | 1 - Command/MongoDBMigrateMetadataCommand.php | 68 +++++++++---------- Controller/MessageController.php | 52 +++----------- DataTransformer/RecipientsDataTransformer.php | 2 +- DependencyInjection/FOSMessageExtension.php | 2 +- Document/Message.php | 4 +- Document/Thread.php | 12 ++-- DocumentManager/MessageManager.php | 4 +- FormType/NewThreadMessageFormType.php | 22 +++--- FormType/NewThreadMultipleMessageFormType.php | 18 ++--- FormType/RecipientsType.php | 4 +- FormType/ReplyMessageFormType.php | 11 ++- SpamDetection/AkismetSpamDetector.php | 6 +- Tests/Document/ThreadDenormalizerTest.php | 32 ++++----- Tests/Functional/Entity/User.php | 2 +- .../EntityManager/ThreadManager.php | 10 +-- Tests/Functional/FunctionalTest.php | 5 +- Tests/Model/ThreadTest.php | 8 +-- Twig/MessageExtension.php | 19 +++--- Util/LegacyFormHelper.php | 6 +- 21 files changed, 123 insertions(+), 166 deletions(-) delete mode 100644 .phpunit.result.cache diff --git a/.gitignore b/.gitignore index 8fc16d75..498d6c99 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ composer.lock phpunit.xml .phpunit +.phpunit.* vendor .php_cs.cache .php_cs diff --git a/.phpunit.result.cache b/.phpunit.result.cache deleted file mode 100644 index c344da23..00000000 --- a/.phpunit.result.cache +++ /dev/null @@ -1 +0,0 @@ -{"version":1,"defects":{"FOS\\MessageBundle\\Document\\ThreadDenormalizerTest::testDenormalize":2,"FOS\\MessageBundle\\Tests\\Functional\\FunctionalTest::testController":3,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testIsReadReturnsTrueWhenRead":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testIsReadReturnsFalseWhenNotRead":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testCanDeleteThreadWhenHasPermission":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testCanDeleteThreadWhenNoPermission":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testIsThreadDeletedByParticipantWhenDeleted":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetNbUnreadCacheStartsEmpty":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetNbUnread":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetNbUnreadStoresCache":4,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetName":4},"times":{"FOS\\MessageBundle\\Document\\ThreadDenormalizerTest::testDenormalize":0.003,"FOS\\MessageBundle\\Tests\\EntityManager\\ThreadManagerTest::testDoCreatedByAndAt":0.008,"FOS\\MessageBundle\\Tests\\EntityManager\\ThreadManagerTest::testDoCreatedByAndAtWithCreatedBy":0,"FOS\\MessageBundle\\Tests\\EntityManager\\ThreadManagerTest::testDoCreatedByAndAtWithCreatedAt":0,"FOS\\MessageBundle\\Tests\\EntityManager\\ThreadManagerTest::testDoCreatedByAndAtWithCreatedAtAndBy":0,"FOS\\MessageBundle\\Tests\\EntityManager\\ThreadManagerTest::testDoCreatedByAndNoMessage":0,"FOS\\MessageBundle\\Tests\\Functional\\FunctionalTest::testController":0.293,"FOS\\MessageBundle\\Tests\\Model\\ThreadTest::testGetOtherParticipants":0.001,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testIsReadReturnsTrueWhenRead":0.001,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testIsReadReturnsFalseWhenNotRead":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testCanDeleteThreadWhenHasPermission":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testCanDeleteThreadWhenNoPermission":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testIsThreadDeletedByParticipantWhenDeleted":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetNbUnreadCacheStartsEmpty":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetNbUnread":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetNbUnreadStoresCache":0,"FOS\\MessageBundle\\Tests\\Twig\\Extension\\MessageExtensionTest::testGetName":0}} \ No newline at end of file diff --git a/Command/MongoDBMigrateMetadataCommand.php b/Command/MongoDBMigrateMetadataCommand.php index 3bd6689d..3cd0d516 100644 --- a/Command/MongoDBMigrateMetadataCommand.php +++ b/Command/MongoDBMigrateMetadataCommand.php @@ -104,15 +104,15 @@ protected function initialize(InputInterface $input, OutputInterface $output) $this->threadCollection = $this->getMongoCollectionForClass($registry, $this->getContainer()->getParameter('fos_message.thread_class')); $this->participantCollection = $this->getMongoCollectionForClass($registry, $input->getArgument('participantClass')); - $this->updateOptions = array( + $this->updateOptions = [ 'multiple' => false, 'safe' => $input->getOption('safe'), - 'fsync' => $input->getOption('fsync'), - ); + 'fsync' => $input->getOption('fsync') + ]; $this->printStatusCallback = function () { }; - register_tick_function(array($this, 'printStatus')); + register_tick_function([$this, 'printStatus']); } /** @@ -124,7 +124,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->migrateThreads($output); $size = memory_get_peak_usage(true); - $unit = array('b', 'k', 'm', 'g', 't', 'p'); + $unit = ['b', 'k', 'm', 'g', 't', 'p']; $output->writeln(sprintf('Peak Memory Usage: %s', round($size / pow(1024, $i = floor(log($size, 1024))), 2).$unit[$i])); } @@ -134,11 +134,8 @@ protected function execute(InputInterface $input, OutputInterface $output) private function migrateMessages(OutputInterface $output) { $cursor = $this->messageCollection->find( - array('metadata' => array('$exists' => false)), - array( - 'isReadByParticipant' => 1, - 'isSpam' => 1, - ) + ['metadata' => ['$exists' => false]], + ['isReadByParticipant' => 1, 'isSpam' => 1] ); $cursor->snapshot(); @@ -160,11 +157,11 @@ private function migrateMessages(OutputInterface $output) $this->createMessageUnreadForParticipants($message); $this->messageCollection->update( - array('_id' => $message['_id']), - array('$set' => array( + ['_id' => $message['_id']], + ['$set' => [ 'metadata' => $message['metadata'], - 'unreadForParticipants' => $message['unreadForParticipants'], - )), + 'unreadForParticipants' => $message['unreadForParticipants'] + ]], $this->updateOptions ); ++$numProcessed; @@ -181,15 +178,15 @@ private function migrateMessages(OutputInterface $output) private function migrateThreads(OutputInterface $output) { $cursor = $this->threadCollection->find( - array('metadata' => array('$exists' => false)), - array( + ['metadata' => ['$exists' => false]], + [ 'datesOfLastMessageWrittenByOtherParticipant' => 1, 'datesOfLastMessageWrittenByParticipant' => 1, 'isDeletedByParticipant' => 1, 'isSpam' => 1, 'messages' => 1, - 'participants' => 1, - ) + 'participants' => 1 + ] ); $numProcessed = 0; @@ -211,14 +208,14 @@ private function migrateThreads(OutputInterface $output) $this->createThreadActiveParticipantArrays($thread); $this->threadCollection->update( - array('_id' => $thread['_id']), - array('$set' => array( + ['_id' => $thread['_id']], + ['$set' => [ 'activeParticipants' => $thread['activeParticipants'], 'activeRecipients' => $thread['activeRecipients'], 'activeSenders' => $thread['activeSenders'], 'lastMessageDate' => $thread['lastMessageDate'], - 'metadata' => $thread['metadata'], - )), + 'metadata' => $thread['metadata'] + ]], $this->updateOptions ); ++$numProcessed; @@ -237,13 +234,13 @@ private function migrateThreads(OutputInterface $output) */ private function createMessageMetadata(array &$message) { - $metadata = array(); + $metadata = []; foreach ($message['isReadByParticipant'] as $participantId => $isRead) { - $metadata[] = array( + $metadata[] = [ 'isRead' => $isRead, - 'participant' => $this->participantCollection->createDBRef(array('_id' => new \MongoId($participantId))) + array('$db' => (string) $this->participantCollection->db), - ); + 'participant' => $this->participantCollection->createDBRef(['_id' => new \MongoId($participantId)]) + ['$db' => (string) $this->participantCollection->db] + ]; } $message['metadata'] = $metadata; @@ -258,7 +255,7 @@ private function createMessageMetadata(array &$message) */ private function createMessageUnreadForParticipants(array &$message) { - $unreadForParticipants = array(); + $unreadForParticipants = []; if (!$message['isSpam']) { foreach ($message['metadata'] as $metadata) { @@ -281,15 +278,12 @@ private function createMessageUnreadForParticipants(array &$message) */ private function createThreadMetadata(array &$thread) { - $metadata = array(); + $metadata = []; $participantIds = array_keys($thread['datesOfLastMessageWrittenByOtherParticipant'] + $thread['datesOfLastMessageWrittenByParticipant'] + $thread['isDeletedByParticipant']); foreach ($participantIds as $participantId) { - $meta = array( - 'isDeleted' => false, - 'participant' => $this->participantCollection->createDBRef(array('_id' => new \MongoId($participantId))) + array('$db' => (string) $this->participantCollection->db), - ); + $meta = ['isDeleted' => false, 'participant' => $this->participantCollection->createDBRef(['_id' => new \MongoId($participantId)]) + ['$db' => (string) $this->participantCollection->db]]; if (isset($thread['isDeletedByParticipant'][$participantId])) { $meta['isDeleted'] = $thread['isDeletedByParticipant'][$participantId]; @@ -320,8 +314,8 @@ private function createThreadLastMessageDate(array &$thread) if (false !== $lastMessageRef) { $lastMessage = $this->messageCollection->findOne( - array('_id' => $lastMessageRef['$id']), - array('createdAt' => 1) + ['_id' => $lastMessageRef['$id']], + ['createdAt' => 1] ); } @@ -337,9 +331,9 @@ private function createThreadLastMessageDate(array &$thread) */ private function createThreadActiveParticipantArrays(array &$thread) { - $activeParticipants = array(); - $activeRecipients = array(); - $activeSenders = array(); + $activeParticipants = []; + $activeRecipients = []; + $activeSenders = []; foreach ($thread['participants'] as $participantRef) { foreach ($thread['metadata'] as $metadata) { diff --git a/Controller/MessageController.php b/Controller/MessageController.php index a654948c..cd838c7e 100644 --- a/Controller/MessageController.php +++ b/Controller/MessageController.php @@ -12,7 +12,6 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; -// use Symfony\Component\DependencyInjection\ContainerInterface; class MessageController extends AbstractController { @@ -41,10 +40,6 @@ class MessageController extends AbstractController */ protected $newThreadFormHandler; - // public function __construct(ContainerInterface $container) - // { - // $this->setContainer($container); - // } public function __construct( ProviderInterface $provider, FactoryInterface $replyFormfactory, @@ -68,9 +63,7 @@ public function inboxAction() { $threads = $this->provider->getInboxThreads(); - return $this->render('@FOSMessage/Message/inbox.html.twig', array( - 'threads' => $threads, - )); + return $this->render('@FOSMessage/Message/inbox.html.twig', ['threads' => $threads]); } /** @@ -82,9 +75,7 @@ public function sentAction() { $threads = $this->provider->getSentThreads(); - return $this->render('@FOSMessage/Message/sent.html.twig', array( - 'threads' => $threads, - )); + return $this->render('@FOSMessage/Message/sent.html.twig', ['threads' => $threads]); } /** @@ -96,9 +87,7 @@ public function deletedAction() { $threads = $this->provider->getDeletedThreads(); - return $this->render('@FOSMessage/Message/deleted.html.twig', array( - 'threads' => $threads, - )); + return $this->render('@FOSMessage/Message/deleted.html.twig', ['threads' => $threads]); } /** @@ -111,21 +100,16 @@ public function deletedAction() public function threadAction($threadId) { $thread = $this->provider->getThread($threadId); - // $form = $this->container->get('fos_message.reply_form.factory')->create($thread); $form = $this->replyFormfactory->create($thread); - // $formHandler = $this->container->get('fos_message.reply_form.handler'); if ($message = $this->replyFormHandler->process($form)) { return $this->redirectToRoute('fos_message_thread_view', ['threadId' => $message->getThread()->getId()]); - // return new RedirectResponse($this->container->get('router')->generate('fos_message_thread_view', array( - // 'threadId' => $message->getThread()->getId(), - // ))); } - return $this->render('@FOSMessage/Message/thread.html.twig', array( + return $this->render('@FOSMessage/Message/thread.html.twig', [ 'form' => $form->createView(), - 'thread' => $thread, - )); + 'thread' => $thread + ]); } /** @@ -135,21 +119,16 @@ public function threadAction($threadId) */ public function newThreadAction() { - // $form = $this->container->get('fos_message.new_thread_form.factory')->create(); $form = $this->newThreadFormFactory->create(); - // $formHandler = $this->container->get('fos_message.new_thread_form.handler'); if ($message = $this->newThreadFormHandler->process($form)) { return $this->redirectToRoute('fos_message_thread_view', ['threadId' => $message->getThread()->getId()]); - // return new RedirectResponse($this->container->get('router')->generate('fos_message_thread_view', array( - // 'threadId' => $message->getThread()->getId(), - // ))); } - return $this->render('@FOSMessage/Message/newThread.html.twig', array( + return $this->render('@FOSMessage/Message/newThread.html.twig', [ 'form' => $form->createView(), - 'data' => $form->getData(), - )); + 'data' => $form->getData() + ]); } /** @@ -162,12 +141,9 @@ public function newThreadAction() public function deleteAction($threadId, DeleterInterface $deleter, ThreadManagerInterface $threadManager) { $thread = $this->provider->getThread($threadId); - // $this->container->get('fos_message.deleter')->markAsDeleted($thread); $deleter->markAsDeleted($thread); - // $this->container->get('fos_message.thread_manager')->saveThread($thread); $threadManager->saveThread($thread); - // return new RedirectResponse($this->container->get('router')->generate('fos_message_inbox')); return $this->redirectToRoute('fos_message_inbox'); } @@ -181,12 +157,9 @@ public function deleteAction($threadId, DeleterInterface $deleter, ThreadManager public function undeleteAction($threadId, DeleterInterface $deleter, ThreadManagerInterface $threadManager) { $thread = $this->provider->getThread($threadId); - // $this->container->get('fos_message.deleter')->markAsUndeleted($thread); - // $this->container->get('fos_message.thread_manager')->saveThread($thread); $deleter->markAsUndeleted($thread); $threadManager->saveThread($thread); - // return new RedirectResponse($this->container->get('router')->generate('fos_message_inbox')); return $this->redirectToRoute('fos_message_inbox'); } @@ -197,14 +170,9 @@ public function undeleteAction($threadId, DeleterInterface $deleter, ThreadManag */ public function searchAction(QueryFactoryInterface $queryFactory, FinderInterface $finder) { - // $query = $this->container->get('fos_message.search_query_factory')->createFromRequest(); - // $threads = $this->container->get('fos_message.search_finder')->find($query); $query = $queryFactory->createFromRequest(); $threads = $finder->find($query); - return $this->render('@FOSMessage/Message/search.html.twig', array( - 'query' => $query, - 'threads' => $threads, - )); + return $this->render('@FOSMessage/Message/search.html.twig', ['query' => $query, 'threads' => $threads]); } } diff --git a/DataTransformer/RecipientsDataTransformer.php b/DataTransformer/RecipientsDataTransformer.php index d437ea8c..a0242c56 100644 --- a/DataTransformer/RecipientsDataTransformer.php +++ b/DataTransformer/RecipientsDataTransformer.php @@ -39,7 +39,7 @@ public function transform($recipients): mixed return ''; } - $usernames = array(); + $usernames = []; foreach ($recipients as $recipient) { $usernames[] = $this->userToUsernameTransformer->transform($recipient); diff --git a/DependencyInjection/FOSMessageExtension.php b/DependencyInjection/FOSMessageExtension.php index 169f3b82..318109ba 100644 --- a/DependencyInjection/FOSMessageExtension.php +++ b/DependencyInjection/FOSMessageExtension.php @@ -21,7 +21,7 @@ public function load(array $configs, ContainerBuilder $container) $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); - if (!in_array(strtolower($config['db_driver']), array('orm', 'mongodb'))) { + if (!in_array(strtolower($config['db_driver']), ['orm', 'mongodb'])) { throw new \InvalidArgumentException(sprintf('Invalid db driver "%s".', $config['db_driver'])); } diff --git a/Document/Message.php b/Document/Message.php index c92760ff..443d2d44 100644 --- a/Document/Message.php +++ b/Document/Message.php @@ -20,7 +20,7 @@ abstract class Message extends BaseMessage * * @var array of participant ID's */ - protected $unreadForParticipants = array(); + protected $unreadForParticipants = []; /** * @param bool $isSpam @@ -58,7 +58,7 @@ protected function doSenderIsRead() */ protected function doEnsureUnreadForParticipantsArray() { - $this->unreadForParticipants = array(); + $this->unreadForParticipants = []; if ($this->isSpam) { return; diff --git a/Document/Thread.php b/Document/Thread.php index 237a072d..a70d5c4c 100644 --- a/Document/Thread.php +++ b/Document/Thread.php @@ -31,7 +31,7 @@ abstract class Thread extends AbstractThread * * @var array of participant ID's */ - protected $activeParticipants = array(); + protected $activeParticipants = []; /** * The activeRecipients array will contain a participant's ID if the thread @@ -40,7 +40,7 @@ abstract class Thread extends AbstractThread * * @var array of participant ID's */ - protected $activeRecipients = array(); + protected $activeRecipients = []; /** * The activeSenders array will contain a participant's ID if the thread is @@ -49,7 +49,7 @@ abstract class Thread extends AbstractThread * * @var array of participant ID's */ - protected $activeSenders = array(); + protected $activeSenders = []; /** * Gets the users participating in this conversation. @@ -186,9 +186,9 @@ protected function doMetadataLastMessageDates() */ protected function doEnsureActiveParticipantArrays() { - $this->activeParticipants = array(); - $this->activeRecipients = array(); - $this->activeSenders = array(); + $this->activeParticipants = []; + $this->activeRecipients = []; + $this->activeSenders = []; foreach ($this->getParticipants() as $participant) { if ($this->isDeletedByParticipant($participant)) { diff --git a/DocumentManager/MessageManager.php b/DocumentManager/MessageManager.php index 0b2c9c59..21bac32c 100644 --- a/DocumentManager/MessageManager.php +++ b/DocumentManager/MessageManager.php @@ -133,7 +133,7 @@ protected function markIsReadByCondition(ParticipantInterface $participant, $isR $queryBuilder ->field('metadata.$.isRead')->set((bool) $isRead) - ->getQuery(array('multiple' => true)) + ->getQuery(['multiple' => true]) ->execute(); /* If marking the message as unread for a participant, add their ID to @@ -147,7 +147,7 @@ protected function markIsReadByCondition(ParticipantInterface $participant, $isR ->field('metadata.participant.$id')->equals(new \MongoId($participant->getId())) ->field('isSpam')->equals(false) ->field('unreadForParticipants')->addToSet($participant->getId()) - ->getQuery(array('multiple' => true)) + ->getQuery(['multiple' => true]) ->execute(); } } diff --git a/FormType/NewThreadMessageFormType.php b/FormType/NewThreadMessageFormType.php index fa1687e1..45d45535 100644 --- a/FormType/NewThreadMessageFormType.php +++ b/FormType/NewThreadMessageFormType.php @@ -18,25 +18,23 @@ class NewThreadMessageFormType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('recipient', LegacyFormHelper::getType('FOS\UserBundle\Form\Type\UsernameFormType'), array( + ->add('recipient', LegacyFormHelper::getType('FOS\UserBundle\Form\Type\UsernameFormType'), [ 'label' => 'recipient', - 'translation_domain' => 'FOSMessageBundle', - )) - ->add('subject', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType'), array( + 'translation_domain' => 'FOSMessageBundle' + ]) + ->add('subject', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType'), [ 'label' => 'subject', - 'translation_domain' => 'FOSMessageBundle', - )) - ->add('body', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextareaType'), array( + 'translation_domain' => 'FOSMessageBundle' + ]) + ->add('body', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextareaType'), [ 'label' => 'body', - 'translation_domain' => 'FOSMessageBundle', - )); + 'translation_domain' => 'FOSMessageBundle' + ]); } public function configureOptions(OptionsResolver $resolver) { - $resolver->setDefaults(array( - 'intention' => 'message', - )); + $resolver->setDefaults(['intention' => 'message']); } /** diff --git a/FormType/NewThreadMultipleMessageFormType.php b/FormType/NewThreadMultipleMessageFormType.php index 405265d6..19582fe8 100644 --- a/FormType/NewThreadMultipleMessageFormType.php +++ b/FormType/NewThreadMultipleMessageFormType.php @@ -16,18 +16,18 @@ class NewThreadMultipleMessageFormType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('recipients', LegacyFormHelper::getType('FOS\MessageBundle\FormType\RecipientsType'), array( + ->add('recipients', LegacyFormHelper::getType('FOS\MessageBundle\FormType\RecipientsType'), [ 'label' => 'recipients', - 'translation_domain' => 'FOSMessageBundle', - )) - ->add('subject', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType'), array( + 'translation_domain' => 'FOSMessageBundle' + ]) + ->add('subject', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType'), [ 'label' => 'subject', - 'translation_domain' => 'FOSMessageBundle', - )) - ->add('body', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextareaType'), array( + 'translation_domain' => 'FOSMessageBundle' + ]) + ->add('body', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextareaType'), [ 'label' => 'body', - 'translation_domain' => 'FOSMessageBundle', - )); + 'translation_domain' => 'FOSMessageBundle' + ]); } /** diff --git a/FormType/RecipientsType.php b/FormType/RecipientsType.php index 2808b611..43cd8ad6 100644 --- a/FormType/RecipientsType.php +++ b/FormType/RecipientsType.php @@ -42,9 +42,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) */ public function configureOptions(OptionsResolver $resolver) { - $resolver->setDefaults(array( - 'invalid_message' => 'The selected recipient does not exist', - )); + $resolver->setDefaults(['invalid_message' => 'The selected recipient does not exist']); } public function setDefaultOptions(OptionsResolverInterface $resolver) diff --git a/FormType/ReplyMessageFormType.php b/FormType/ReplyMessageFormType.php index 6340d0f4..83a42709 100644 --- a/FormType/ReplyMessageFormType.php +++ b/FormType/ReplyMessageFormType.php @@ -18,17 +18,16 @@ class ReplyMessageFormType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('body', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextareaType'), array( + ->add('body', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextareaType'), [ 'label' => 'body', - 'translation_domain' => 'FOSMessageBundle', - )); + 'translation_domain' => 'FOSMessageBundle' + ] + ); } public function configureOptions(OptionsResolver $resolver) { - $resolver->setDefaults(array( - 'intention' => 'reply', - )); + $resolver->setDefaults(['intention' => 'reply']); } /** diff --git a/SpamDetection/AkismetSpamDetector.php b/SpamDetection/AkismetSpamDetector.php index 070d8160..3ef54208 100644 --- a/SpamDetection/AkismetSpamDetector.php +++ b/SpamDetection/AkismetSpamDetector.php @@ -29,9 +29,9 @@ public function __construct(AkismetInterface $akismet, ParticipantProviderInterf */ public function isSpam(NewThreadMessage $message) { - return $this->akismet->isSpam(array( + return $this->akismet->isSpam([ 'comment_author' => (string) $this->participantProvider->getAuthenticatedParticipant(), - 'comment_content' => $message->getBody(), - )); + 'comment_content' => $message->getBody() + ]); } } diff --git a/Tests/Document/ThreadDenormalizerTest.php b/Tests/Document/ThreadDenormalizerTest.php index 0196b206..81902f5d 100644 --- a/Tests/Document/ThreadDenormalizerTest.php +++ b/Tests/Document/ThreadDenormalizerTest.php @@ -20,12 +20,12 @@ protected function setUpBeforeTest() { $this->markTestIncomplete('Broken, needs to be fixed'); - $this->dates = array( + $this->dates = [ new DateTime('- 3 days'), new DateTime('- 2 days'), new DateTime('- 1 days'), - new DateTime('- 1 hour'), - ); + new DateTime('- 1 hour') + ]; } public function testDenormalize() @@ -44,9 +44,9 @@ public function testDenormalize() $thread->addParticipant($user2); $thread->addMessage($message); - $this->assertSame(array($user1, $user2), $thread->getParticipants()); - $this->assertSame(array('u2' => $this->dates[0]->getTimestamp()), $thread->getDatesOfLastMessageWrittenByOtherParticipant()); - $this->assertSame(array('u1' => $this->dates[0]->getTimestamp()), $thread->getDatesOfLastMessageWrittenByParticipant()); + $this->assertSame([$user1, $user2], $thread->getParticipants()); + $this->assertSame(['u2' => $this->dates[0]->getTimestamp()], $thread->getDatesOfLastMessageWrittenByOtherParticipant()); + $this->assertSame(['u1' => $this->dates[0]->getTimestamp()], $thread->getDatesOfLastMessageWrittenByParticipant()); /* * Second message @@ -54,9 +54,9 @@ public function testDenormalize() $message = $this->createMessageMock($user2, $user1, $this->dates[1]); $thread->addMessage($message); - $this->assertSame(array($user1, $user2), $thread->getParticipants()); - $this->assertSame(array('u1' => $this->dates[1]->getTimestamp(), 'u2' => $this->dates[0]->getTimestamp()), $thread->getDatesOfLastMessageWrittenByOtherParticipant()); - $this->assertSame(array('u1' => $this->dates[0]->getTimestamp(), 'u2' => $this->dates[1]->getTimestamp()), $thread->getDatesOfLastMessageWrittenByParticipant()); + $this->assertSame([$user1, $user2], $thread->getParticipants()); + $this->assertSame(['u1' => $this->dates[1]->getTimestamp(), 'u2' => $this->dates[0]->getTimestamp()], $thread->getDatesOfLastMessageWrittenByOtherParticipant()); + $this->assertSame(['u1' => $this->dates[0]->getTimestamp(), 'u2' => $this->dates[1]->getTimestamp()], $thread->getDatesOfLastMessageWrittenByParticipant()); /* * Third message @@ -64,9 +64,9 @@ public function testDenormalize() $message = $this->createMessageMock($user2, $user1, $this->dates[2]); $thread->addMessage($message); - $this->assertSame(array($user1, $user2), $thread->getParticipants()); - $this->assertSame(array('u1' => $this->dates[2]->getTimestamp(), 'u2' => $this->dates[0]->getTimestamp()), $thread->getDatesOfLastMessageWrittenByOtherParticipant()); - $this->assertSame(array('u1' => $this->dates[0]->getTimestamp(), 'u2' => $this->dates[2]->getTimestamp()), $thread->getDatesOfLastMessageWrittenByParticipant()); + $this->assertSame([$user1, $user2], $thread->getParticipants()); + $this->assertSame(['u1' => $this->dates[2]->getTimestamp(), 'u2' => $this->dates[0]->getTimestamp()], $thread->getDatesOfLastMessageWrittenByOtherParticipant()); + $this->assertSame(['u1' => $this->dates[0]->getTimestamp(), 'u2' => $this->dates[2]->getTimestamp()], $thread->getDatesOfLastMessageWrittenByParticipant()); /* * Fourth message @@ -74,12 +74,12 @@ public function testDenormalize() $message = $this->createMessageMock($user1, $user2, $this->dates[3]); $thread->addMessage($message); - $this->assertSame(array($user1, $user2), $thread->getParticipants()); - $this->assertSame(array('u1' => $this->dates[2]->getTimestamp(), 'u2' => $this->dates[3]->getTimestamp()), $thread->getDatesOfLastMessageWrittenByOtherParticipant()); - $this->assertSame(array('u1' => $this->dates[3]->getTimestamp(), 'u2' => $this->dates[2]->getTimestamp()), $thread->getDatesOfLastMessageWrittenByParticipant()); + $this->assertSame([$user1, $user2], $thread->getParticipants()); + $this->assertSame(['u1' => $this->dates[2]->getTimestamp(), 'u2' => $this->dates[3]->getTimestamp()], $thread->getDatesOfLastMessageWrittenByOtherParticipant()); + $this->assertSame(['u1' => $this->dates[3]->getTimestamp(), 'u2' => $this->dates[2]->getTimestamp()], $thread->getDatesOfLastMessageWrittenByParticipant()); $this->assertEquals('test thread subject hi dude', $thread->getKeywords()); - $this->assertSame(array('u1' => false, 'u2' => false), $thread->getIsDeletedByParticipant()); + $this->assertSame(['u1' => false, 'u2' => false], $thread->getIsDeletedByParticipant()); } protected function createMessageMock($sender, $recipient, DateTime $date) diff --git a/Tests/Functional/Entity/User.php b/Tests/Functional/Entity/User.php index 317f8df1..dfd739a7 100644 --- a/Tests/Functional/Entity/User.php +++ b/Tests/Functional/Entity/User.php @@ -24,7 +24,7 @@ public function getSalt() public function getRoles(): array { - return array(); + return []; } public function eraseCredentials() diff --git a/Tests/Functional/EntityManager/ThreadManager.php b/Tests/Functional/EntityManager/ThreadManager.php index fa03ef74..aed6f0b2 100644 --- a/Tests/Functional/EntityManager/ThreadManager.php +++ b/Tests/Functional/EntityManager/ThreadManager.php @@ -21,7 +21,7 @@ public function getParticipantInboxThreadsQueryBuilder(ParticipantInterface $par public function findParticipantInboxThreads(ParticipantInterface $participant) { - return array(new Thread()); + return [new Thread()]; } public function getParticipantSentThreadsQueryBuilder(ParticipantInterface $participant) @@ -30,7 +30,7 @@ public function getParticipantSentThreadsQueryBuilder(ParticipantInterface $part public function findParticipantSentThreads(ParticipantInterface $participant) { - return array(); + return []; } public function getParticipantDeletedThreadsQueryBuilder(ParticipantInterface $participant) @@ -39,7 +39,7 @@ public function getParticipantDeletedThreadsQueryBuilder(ParticipantInterface $p public function findParticipantDeletedThreads(ParticipantInterface $participant) { - return array(); + return []; } public function getParticipantThreadsBySearchQueryBuilder(ParticipantInterface $participant, $search) @@ -48,12 +48,12 @@ public function getParticipantThreadsBySearchQueryBuilder(ParticipantInterface $ public function findParticipantThreadsBySearch(ParticipantInterface $participant, $search) { - return array(); + return []; } public function findThreadsCreatedBy(ParticipantInterface $participant) { - return array(); + return []; } public function markAsReadByParticipant(ReadableInterface $readable, ParticipantInterface $participant) diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index bd0dffcc..c4b3e4eb 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -6,10 +6,7 @@ class FunctionalTest extends WebTestCase { public function testController() { - $client = self::createClient(array(), array( - 'PHP_AUTH_USER' => 'guilhem', - 'PHP_AUTH_PW' => 'pass', - )); + $client = self::createClient([], ['PHP_AUTH_USER' => 'guilhem', 'PHP_AUTH_PW' => 'pass']); $crawler = $client->request('GET', '/sent'); $response = $client->getResponse(); diff --git a/Tests/Model/ThreadTest.php b/Tests/Model/ThreadTest.php index d1eb8d01..b006c5d7 100644 --- a/Tests/Model/ThreadTest.php +++ b/Tests/Model/ThreadTest.php @@ -16,7 +16,7 @@ public function testGetOtherParticipants() $thread = $this->getMockForAbstractClass('FOS\MessageBundle\Model\Thread'); $thread->expects($this->atLeastOnce()) ->method('getParticipants') - ->will($this->returnValue(array($u1, $u2, $u3))); + ->will($this->returnValue([$u1, $u2, $u3])); $toIds = function (array $participants) { return array_map(function (ParticipantInterface $participant) { @@ -24,9 +24,9 @@ public function testGetOtherParticipants() }, $participants); }; - $this->assertSame($toIds(array($u2, $u3)), $toIds($thread->getOtherParticipants($u1))); - $this->assertSame($toIds(array($u1, $u3)), $toIds($thread->getOtherParticipants($u2))); - $this->assertSame($toIds(array($u1, $u2)), $toIds($thread->getOtherParticipants($u3))); + $this->assertSame($toIds([$u2, $u3]), $toIds($thread->getOtherParticipants($u1))); + $this->assertSame($toIds([$u1, $u3]), $toIds($thread->getOtherParticipants($u2))); + $this->assertSame($toIds([$u1, $u2]), $toIds($thread->getOtherParticipants($u3))); } protected function createParticipantMock($id) diff --git a/Twig/MessageExtension.php b/Twig/MessageExtension.php index ef4ab5df..08ae9cca 100644 --- a/Twig/MessageExtension.php +++ b/Twig/MessageExtension.php @@ -19,8 +19,11 @@ class MessageExtension extends AbstractExtension protected $nbUnreadMessagesCache; - public function __construct(ParticipantProviderInterface $participantProvider, ProviderInterface $provider, AuthorizerInterface $authorizer) - { + public function __construct( + ParticipantProviderInterface $participantProvider, + ProviderInterface $provider, + AuthorizerInterface $authorizer + ) { $this->participantProvider = $participantProvider; $this->provider = $provider; $this->authorizer = $authorizer; @@ -31,12 +34,12 @@ public function __construct(ParticipantProviderInterface $participantProvider, P */ public function getFunctions(): array { - return array( - new TwigFunction('fos_message_is_read', array($this, 'isRead')), - new TwigFunction('fos_message_nb_unread', array($this, 'getNbUnread')), - new TwigFunction('fos_message_can_delete_thread', array($this, 'canDeleteThread')), - new TwigFunction('fos_message_deleted_by_participant', array($this, 'isThreadDeletedByParticipant')), - ); + return [ + new TwigFunction('fos_message_is_read', [$this, 'isRead']), + new TwigFunction('fos_message_nb_unread', [$this, 'getNbUnread']), + new TwigFunction('fos_message_can_delete_thread', [$this, 'canDeleteThread']), + new TwigFunction('fos_message_deleted_by_participant', [$this, 'isThreadDeletedByParticipant']) + ]; } /** diff --git a/Util/LegacyFormHelper.php b/Util/LegacyFormHelper.php index 54cc5bf5..171aaa92 100644 --- a/Util/LegacyFormHelper.php +++ b/Util/LegacyFormHelper.php @@ -11,15 +11,15 @@ */ final class LegacyFormHelper { - private static $map = array( + private static $map = [ 'FOS\UserBundle\Form\Type\UsernameFormType' => 'fos_user_username', 'FOS\MessageBundle\FormType\RecipientsType' => 'recipients_selector', 'Symfony\Component\Form\Extension\Core\Type\EmailType' => 'email', 'Symfony\Component\Form\Extension\Core\Type\PasswordType' => 'password', 'Symfony\Component\Form\Extension\Core\Type\RepeatedType' => 'repeated', 'Symfony\Component\Form\Extension\Core\Type\TextType' => 'text', - 'Symfony\Component\Form\Extension\Core\Type\TextareaType' => 'textarea', - ); + 'Symfony\Component\Form\Extension\Core\Type\TextareaType' => 'textarea' + ]; public static function getType($class) { From 003b82d99dcce23c14074e5bc09882a66c2fe72f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gre=CC=81goire=20Willmann?= <5655900+GWillmann@users.noreply.github.com> Date: Wed, 21 Sep 2022 15:39:17 +0200 Subject: [PATCH 3/8] feat(ci): add Github Action workflow --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b7886d1e..e583db4e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: operating-system: ['ubuntu-latest'] - php-versions: ['5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0'] + php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1'] phpunit-versions: ['latest'] steps: - name: Checkout From 5973004e938a4a1b8bf8adee71a40036fc81cf44 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 21 Sep 2022 15:43:41 +0200 Subject: [PATCH 4/8] fix(user): change user identifier --- Tests/Functional/Form/UserToUsernameTransformer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Functional/Form/UserToUsernameTransformer.php b/Tests/Functional/Form/UserToUsernameTransformer.php index 65377f14..4e123863 100644 --- a/Tests/Functional/Form/UserToUsernameTransformer.php +++ b/Tests/Functional/Form/UserToUsernameTransformer.php @@ -18,7 +18,7 @@ public function transform($value): ?string throw new \RuntimeException(); } - return $value->getUsername(); + return $value->getUserIdentifier(); } /** From f0f3cd1de6fde07bfc4eff556e9cc9658a161408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gre=CC=81goire=20Willmann?= <5655900+GWillmann@users.noreply.github.com> Date: Wed, 21 Sep 2022 16:43:57 +0200 Subject: [PATCH 5/8] fix(tests): add SYMFONY_DEPRECATIONS_HELPER also readd previous Symfony 5.x deprecated methods --- Tests/Functional/Entity/User.php | 5 +++++ Tests/Functional/Entity/UserProvider.php | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/Tests/Functional/Entity/User.php b/Tests/Functional/Entity/User.php index dfd739a7..f243152f 100644 --- a/Tests/Functional/Entity/User.php +++ b/Tests/Functional/Entity/User.php @@ -35,4 +35,9 @@ public function getId() { return 1; } + + public function getUserName(): string + { + return $this->getUserIdentifier(); + } } diff --git a/Tests/Functional/Entity/UserProvider.php b/Tests/Functional/Entity/UserProvider.php index 102740be..1b3658e4 100644 --- a/Tests/Functional/Entity/UserProvider.php +++ b/Tests/Functional/Entity/UserProvider.php @@ -26,4 +26,9 @@ private function fetchUser($username) { return new User(); } + + public function loadUserByUsername($username): UserInterface + { + return $this->loadUserByIdentifier($username); + } } From f9a06c91ecb5cf61f1c64378a044d7dcf5a267c2 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 21 Sep 2022 19:03:58 +0200 Subject: [PATCH 6/8] fix: after dep upgrade --- Controller/MessageController.php | 42 +++++++++++++++++---- Deleter/Deleter.php | 4 +- DependencyInjection/Configuration.php | 4 +- DependencyInjection/FOSMessageExtension.php | 6 +-- Event/ReadableEvent.php | 2 +- Event/ThreadEvent.php | 2 +- Model/ParticipantInterface.php | 2 +- Reader/Reader.php | 4 +- Resources/config/config.xml | 9 +++-- Resources/doc/99-config-reference.md | 4 +- Sender/Sender.php | 9 +++-- Tests/Functional/Entity/Message.php | 34 +++++++++++++++++ Tests/Functional/Entity/User.php | 5 +++ composer.json | 3 +- 14 files changed, 102 insertions(+), 28 deletions(-) diff --git a/Controller/MessageController.php b/Controller/MessageController.php index cd838c7e..cdb47b6f 100644 --- a/Controller/MessageController.php +++ b/Controller/MessageController.php @@ -40,18 +40,46 @@ class MessageController extends AbstractController */ protected $newThreadFormHandler; + /** + * @var DeleterInterface + */ + protected $deleter; + + /** + * @var ThreadManagerInterface + */ + protected $threadManager; + + /** + * @var QueryFactoryInterface + */ + protected $queryFactory; + + /** + * @var FinderInterface + */ + protected $finder; + public function __construct( ProviderInterface $provider, FactoryInterface $replyFormfactory, FormHandlerInterface $replyFormHandler, FactoryInterface $newThreadFormFactory, - FormHandlerInterface $newThreadFormHandler + FormHandlerInterface $newThreadFormHandler, + DeleterInterface $deleter, + ThreadManagerInterface $threadManager, + QueryFactoryInterface $queryFactory, + FinderInterface $finder, ) { $this->provider = $provider; $this->replyFormfactory = $replyFormfactory; $this->replyFormHandler = $replyFormHandler; $this->newThreadFormFactory = $newThreadFormFactory; $this->newThreadFormHandler = $newThreadFormHandler; + $this->deleter = $deleter; + $this->threadManager = $threadManager; + $this->queryFactory = $queryFactory; + $this->finder = $finder; } /** @@ -138,11 +166,11 @@ public function newThreadAction() * * @return RedirectResponse */ - public function deleteAction($threadId, DeleterInterface $deleter, ThreadManagerInterface $threadManager) + public function deleteAction($threadId) { $thread = $this->provider->getThread($threadId); - $deleter->markAsDeleted($thread); - $threadManager->saveThread($thread); + $this->deleter->markAsDeleted($thread); + $this->threadManager->saveThread($thread); return $this->redirectToRoute('fos_message_inbox'); } @@ -168,10 +196,10 @@ public function undeleteAction($threadId, DeleterInterface $deleter, ThreadManag * * @return Response */ - public function searchAction(QueryFactoryInterface $queryFactory, FinderInterface $finder) + public function searchAction() { - $query = $queryFactory->createFromRequest(); - $threads = $finder->find($query); + $query = $this->queryFactory->createFromRequest(); + $threads = $this->finder->find($query); return $this->render('@FOSMessage/Message/search.html.twig', ['query' => $query, 'threads' => $threads]); } diff --git a/Deleter/Deleter.php b/Deleter/Deleter.php index b9685337..d0e8fade 100644 --- a/Deleter/Deleter.php +++ b/Deleter/Deleter.php @@ -56,7 +56,7 @@ public function markAsDeleted(ThreadInterface $thread) } $thread->setIsDeletedByParticipant($this->getAuthenticatedParticipant(), true); - $this->dispatcher->dispatch(FOSMessageEvents::POST_DELETE, new ThreadEvent($thread)); + $this->dispatcher->dispatch(new ThreadEvent($thread), FOSMessageEvents::POST_DELETE); } /** @@ -69,7 +69,7 @@ public function markAsUndeleted(ThreadInterface $thread) } $thread->setIsDeletedByParticipant($this->getAuthenticatedParticipant(), false); - $this->dispatcher->dispatch(FOSMessageEvents::POST_UNDELETE, new ThreadEvent($thread)); + $this->dispatcher->dispatch(new ThreadEvent($thread), FOSMessageEvents::POST_UNDELETE); } /** diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 884a160a..bae5c758 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -47,8 +47,8 @@ public function getConfigTreeBuilder() ->arrayNode('search') ->addDefaultsIfNotSet() ->children() - ->scalarNode('query_factory')->defaultValue('fos_message.search_query_factory.default')->cannotBeEmpty()->end() - ->scalarNode('finder')->defaultValue('fos_message.search_finder.default')->cannotBeEmpty()->end() + ->scalarNode('query_factory')->defaultValue('fos_message.search.query_factory.default')->cannotBeEmpty()->end() + ->scalarNode('finder')->defaultValue('fos_message.search.finder.default')->cannotBeEmpty()->end() ->scalarNode('query_parameter')->defaultValue('q')->cannotBeEmpty()->end() ->end() ->end() diff --git a/DependencyInjection/FOSMessageExtension.php b/DependencyInjection/FOSMessageExtension.php index 318109ba..e895a85b 100644 --- a/DependencyInjection/FOSMessageExtension.php +++ b/DependencyInjection/FOSMessageExtension.php @@ -84,9 +84,9 @@ public function load(array $configs, ContainerBuilder $container) $container->setAlias('fos_message.reply_form.factory', new Alias($config['reply_form']['factory'], true)); $container->setAlias('fos_message.reply_form.handler', new Alias($config['reply_form']['handler'], true)); - $container->setAlias('fos_message.search_query_factory', new Alias($config['search']['query_factory'], true)); - $container->setAlias('fos_message.search_finder', new Alias($config['search']['finder'], true)); - $container->getDefinition('fos_message.search_query_factory.default') + $container->setAlias('fos_message.search.query_factory', new Alias($config['search']['query_factory'], true)); + $container->setAlias('fos_message.search.finder', new Alias($config['search']['finder'], true)); + $container->getDefinition('fos_message.search.query_factory.default') ->replaceArgument(1, $config['search']['query_parameter']); $container->getDefinition('fos_message.recipients_data_transformer') diff --git a/Event/ReadableEvent.php b/Event/ReadableEvent.php index 3f9e18c6..3cc17083 100644 --- a/Event/ReadableEvent.php +++ b/Event/ReadableEvent.php @@ -3,7 +3,7 @@ namespace FOS\MessageBundle\Event; use FOS\MessageBundle\Model\ReadableInterface; -use Symfony\Component\EventDispatcher\Event; +use Symfony\Contracts\EventDispatcher\Event; class ReadableEvent extends Event { diff --git a/Event/ThreadEvent.php b/Event/ThreadEvent.php index b7176180..0ae3fa7c 100644 --- a/Event/ThreadEvent.php +++ b/Event/ThreadEvent.php @@ -3,7 +3,7 @@ namespace FOS\MessageBundle\Event; use FOS\MessageBundle\Model\ThreadInterface; -use Symfony\Component\EventDispatcher\Event; +use Symfony\Contracts\EventDispatcher\Event; class ThreadEvent extends Event { diff --git a/Model/ParticipantInterface.php b/Model/ParticipantInterface.php index 6849dec9..b8d10011 100644 --- a/Model/ParticipantInterface.php +++ b/Model/ParticipantInterface.php @@ -9,7 +9,7 @@ * * @author Thibault Duplessis */ -interface ParticipantInterface +interface ParticipantInterface extends \Stringable { /** * Gets the unique identifier of the participant. diff --git a/Reader/Reader.php b/Reader/Reader.php index f0a25812..a45052a8 100644 --- a/Reader/Reader.php +++ b/Reader/Reader.php @@ -56,7 +56,7 @@ public function markAsRead(ReadableInterface $readable) } $this->readableManager->markAsReadByParticipant($readable, $participant); - $this->dispatcher->dispatch(FOSMessageEvents::POST_READ, new ReadableEvent($readable)); + $this->dispatcher->dispatch(new ReadableEvent($readable), FOSMessageEvents::POST_READ); } /** @@ -70,7 +70,7 @@ public function markAsUnread(ReadableInterface $readable) } $this->readableManager->markAsUnreadByParticipant($readable, $participant); - $this->dispatcher->dispatch(FOSMessageEvents::POST_UNREAD, new ReadableEvent($readable)); + $this->dispatcher->dispatch(new ReadableEvent($readable), FOSMessageEvents::POST_UNREAD); } /** diff --git a/Resources/config/config.xml b/Resources/config/config.xml index f8d79317..fbcb30a6 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -6,12 +6,15 @@ - + + + + @@ -70,12 +73,12 @@ - + - + diff --git a/Resources/doc/99-config-reference.md b/Resources/doc/99-config-reference.md index b7f5a391..8522680a 100644 --- a/Resources/doc/99-config-reference.md +++ b/Resources/doc/99-config-reference.md @@ -24,8 +24,8 @@ fos_message: twig_extension: fos_message.twig_extension # See Twig\Extension\MessageExtension user_transformer: fos_user.user_transformer # See Symfony\Component\Form\DataTransformerInterface search: - finder: fos_message.search_finder # See Finder\FinderInterface - query_factory: fos_message.search_query_factory # See Finder\QueryFactoryInterface + finder: fos_message.search.finder # See Finder\FinderInterface + query_factory: fos_message.search.query_factory # See Finder\QueryFactoryInterface query_parameter: 'q' # Request query parameter containing the term new_thread_form: factory: fos_message.new_thread_form.factory # See FormFactory\NewThreadMessageFormFactory diff --git a/Sender/Sender.php b/Sender/Sender.php index 471d44f2..7201fd43 100644 --- a/Sender/Sender.php +++ b/Sender/Sender.php @@ -31,8 +31,11 @@ class Sender implements SenderInterface */ protected $dispatcher; - public function __construct(MessageManagerInterface $messageManager, ThreadManagerInterface $threadManager, EventDispatcherInterface $dispatcher) - { + public function __construct( + MessageManagerInterface $messageManager, + ThreadManagerInterface $threadManager, + EventDispatcherInterface $dispatcher + ) { $this->messageManager = $messageManager; $this->threadManager = $threadManager; $this->dispatcher = $dispatcher; @@ -54,6 +57,6 @@ public function send(MessageInterface $message) $message->getThread()->setIsDeleted(false); $this->messageManager->saveMessage($message); - $this->dispatcher->dispatch(FOSMessageEvents::POST_SEND, new MessageEvent($message)); + $this->dispatcher->dispatch(new MessageEvent($message), FOSMessageEvents::POST_SEND); } } diff --git a/Tests/Functional/Entity/Message.php b/Tests/Functional/Entity/Message.php index de629e15..9e9c97d4 100644 --- a/Tests/Functional/Entity/Message.php +++ b/Tests/Functional/Entity/Message.php @@ -4,6 +4,40 @@ use FOS\MessageBundle\Entity\Message as BaseMessage; +/** + * @ORM\Entity + */ class Message extends BaseMessage { + /** + * @ORM\Id + * @ORM\Column(type="integer") + * @ORM\GeneratedValue(strategy="AUTO") + */ + protected $id; + + /** + * @ORM\ManyToOne( + * targetEntity="App\Entity\Thread", + * inversedBy="messages" + * ) + * @var \FOS\MessageBundle\Model\ThreadInterface + */ + protected $thread; + + /** + * @ORM\ManyToOne(targetEntity="App\Entity\User") + * @var \FOS\MessageBundle\Model\ParticipantInterface + */ + protected $sender; + + /** + * @ORM\OneToMany( + * targetEntity="App\Entity\MessageMetadata", + * mappedBy="message", + * cascade={"all"} + * ) + * @var MessageMetadata[]|Collection + */ + protected $metadata; } diff --git a/Tests/Functional/Entity/User.php b/Tests/Functional/Entity/User.php index f243152f..135b7974 100644 --- a/Tests/Functional/Entity/User.php +++ b/Tests/Functional/Entity/User.php @@ -40,4 +40,9 @@ public function getUserName(): string { return $this->getUserIdentifier(); } + + public function __toString(): string + { + return $this->getUserIdentifier(); + } } diff --git a/composer.json b/composer.json index 275e8a67..b9fe3136 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,8 @@ "require": { "php": ">=7.2", "doctrine/collections": "^1.3", + "symfony/event-dispatcher": "^5.0 || ^6.0", + "symfony/event-dispatcher-contracts": "^2.0 || ^3.0", "symfony/form": "^5.0|^6.0", "symfony/framework-bundle": "^5.0|^6.0", "symfony/security-bundle": "^5.0|^6.0", @@ -29,7 +31,6 @@ "symfony/translation": "^5.0|^6.0", "symfony/yaml": "^5.0|^6.0", "symfony/security-bundle": "^5.0|^6.0", - "symfony/templating": "^5.0|^6.0", "symfony/browser-kit": "^5.0|^6.0", "symfony/phpunit-bridge": "^6.1", "doctrine/orm": "^2.0" From 4caef84b9c5721b8995b3373c64c5741cd7f670a Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 21 Sep 2022 20:18:40 +0200 Subject: [PATCH 7/8] fix(php<8): syntax error --- Controller/MessageController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controller/MessageController.php b/Controller/MessageController.php index cdb47b6f..ac582334 100644 --- a/Controller/MessageController.php +++ b/Controller/MessageController.php @@ -69,7 +69,7 @@ public function __construct( DeleterInterface $deleter, ThreadManagerInterface $threadManager, QueryFactoryInterface $queryFactory, - FinderInterface $finder, + FinderInterface $finder ) { $this->provider = $provider; $this->replyFormfactory = $replyFormfactory; From 513270de56ff6315ce6daae142ae5c3b3b919f89 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 22 Sep 2022 11:36:34 +0200 Subject: [PATCH 8/8] doc: improve doc --- Command/MongoDBMigrateMetadataCommand.php | 2 +- Resources/doc/01-installation.md | 29 +-- Resources/doc/01a-orm-models.md | 112 ++++------ Resources/doc/01b-odm-models.md | 44 ++-- Resources/doc/04-spam-detection.md | 2 +- Resources/doc/05-permissions.md | 2 +- Resources/doc/06-events.md | 2 +- Resources/doc/90-multiple-recipients.md | 6 +- Resources/doc/99-config-reference.md | 6 +- Resources/doc/99-using-other-user-bundles.md | 219 +++++++++++++++---- UPGRADING.md | 4 +- 11 files changed, 254 insertions(+), 174 deletions(-) diff --git a/Command/MongoDBMigrateMetadataCommand.php b/Command/MongoDBMigrateMetadataCommand.php index 3cd0d516..d5e399cf 100644 --- a/Command/MongoDBMigrateMetadataCommand.php +++ b/Command/MongoDBMigrateMetadataCommand.php @@ -66,7 +66,7 @@ protected function configure() fields to a new schema optimized for MongoDB queries. This command requires the participant class to be provided as its first and only parameter: - php app/console fos:message:mongodb:migrate:metadata "Acme\Document\User" + php bin/console fos:message:mongodb:migrate:metadata "Acme\Document\User" The following hash fields will become obsolete after migration: diff --git a/Resources/doc/01-installation.md b/Resources/doc/01-installation.md index 28ff3b11..e8a5b010 100644 --- a/Resources/doc/01-installation.md +++ b/Resources/doc/01-installation.md @@ -32,7 +32,7 @@ Your user class may look something like the following: ```php ['all' => true], + // ... +]; ``` ### Step 5 - Import the bundle routing @@ -80,7 +77,7 @@ public function registerBundles() Add FOSMessageBundle's routing to your application with an optional routing prefix. ```yaml -# app/config/routing.yml +# config/routing.yaml fos_message: resource: "@FOSMessageBundle/Resources/config/routing.xml" @@ -89,13 +86,7 @@ fos_message: ### Step 6 - Check templating -Make sure to add this to `framwork.yaml` (and check twig is installed) if you run into the non-existant service `templating` issue: - -```yaml -templating: - engines: - twig -``` +Make sure to have twig installed and ready to be used ## Installation Finished diff --git a/Resources/doc/01a-orm-models.md b/Resources/doc/01a-orm-models.md index 4b3e2246..ba7d6189 100644 --- a/Resources/doc/01a-orm-models.md +++ b/Resources/doc/01a-orm-models.md @@ -7,15 +7,15 @@ ORM. Given the examples below with their namespaces and class names, you need to configure FOSMessageBundle to tell them about these classes. -Add the following to your `app/config/config.yml` file. +Create and add the following config to your `config/fos_message.yaml` file. ```yaml -# app/config/config.yml +# config/fos_message.yaml fos_message: db_driver: orm - thread_class: AppBundle\Entity\Thread - message_class: AppBundle\Entity\Message + thread_class: App\Entity\Thread + message_class: App\Entity\Message ``` [Continue with the installation][] @@ -25,49 +25,38 @@ Message class ```php repository = $doctrine->getManager()->getRepository('AppBundle:User'); + $this->em = $em; } /** - * Transforms a User instance into a username string. + * Transforms a UserInterface instance into a username string. * - * @param User|null $value User instance + * @param UserInterface|null $value UserInterface instance * * @return string|null Username * - * @throws UnexpectedTypeException if the given value is not a User instance + * @throws UnexpectedTypeException if the given value is not a UserInterface instance */ - public function transform($value) + public function transform($value): ?string { if (null === $value) { return null; } - if (! $value instanceof User) { - throw new UnexpectedTypeException($value, 'AppBundle\Entity\User'); + if (!$value instanceof UserInterface) { + throw new UnexpectedTypeException($value, 'Symfony\Component\Security\Core\User\UserInterface'); } - return $value->getUsername(); + return $value->getUserIdentifier(); } /** - * Transforms a username string into a User instance. + * Transforms a username string into a UserInterface instance. * * @param string $value Username * - * @return User the corresponding User instance + * @return UserInterface|null the corresponding UserInterface instance * * @throws UnexpectedTypeException if the given value is not a string */ - public function reverseTransform($value) + public function reverseTransform($value): ?UserInterface { if (null === $value || '' === $value) { return null; } - if (! is_string($value)) { + if (!is_string($value)) { throw new UnexpectedTypeException($value, 'string'); } - return $this->repository->findOneByUsername($value); + return $this->em->getRepository(User::class)->findOneByIdentifier($value); } } ``` @@ -93,15 +92,142 @@ For the moment, there is no configuration key to do it so we will emulate the FOSUserBundle transformer by using its name as an alias of our own service: ``` xml - + - - + ``` +Or + +``` yaml +# config/services.yaml +services: + ... + app.user_to_username_transformer: + class: App\Form\DataTransformer\UserToUsernameTransformer + + fos_user.user_to_username_transformer: + alias: app.user_to_username_transformer +``` + +As `NewThreadMessageFormType` used `FOS\UserBundle\Form\Type\UsernameFormType` for recipied field, you need to create +a new form type to replace it + +```php +add('recipient', UsernameFormType::class, [ + 'label' => 'recipient', + 'translation_domain' => 'FOSMessageBundle', + ]) + ->add('subject', TextType::class, [ + 'label' => 'subject', + 'translation_domain' => 'FOSMessageBundle', + ]) + ->add('body', TextareaType::class, [ + 'label' => 'body', + 'translation_domain' => 'FOSMessageBundle', + ]); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'intention' => 'message', + ]); + } + + /** + * {@inheritdoc} + */ + public function getBlockPrefix() + { + return 'new_thread_message'; + } +} +``` + +And create a new form type linked to `UserToUsernameTransformer` + +```php +usernameTransformer = $usernameTransformer; + } + + /** + * {@inheritdoc} + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder->addModelTransformer($this->usernameTransformer); + } + + /** + * {@inheritdoc} + */ + public function getParent() + { + return TextType::class; + } + + /** + * {@inheritdoc} + */ + public function getBlockPrefix() + { + return 'app_username_type'; + } +} + +``` + ### Problems you may encounter #### User identifier field is not `id` @@ -113,32 +239,31 @@ made by the bundle. You can copy the default ones (in `FOS\MessageBundle\EntityManager` if you use the Doctrine ORM) into your bundle, change their queries and register them as services: -``` xml - - - - - %fos_message.message_class% - %fos_message.message_meta_class% - - - - - %fos_message.thread_class% - %fos_message.thread_meta_class% - - +``` yaml +# config/services.yaml + +services: + ... + App\Manager\MessageManager: + bind: + $class: '%fos_message.message_class%' + $metaClass: '%fos_message.message_meta_class%' + + App\Manager\ThreadManager: + bind: + $class: '%fos_message.thread_class%' + $metaClass: '%fos_message.thread_meta_class%' ``` Once done, tell FOSMessageBundle to use them in the configuration: ``` yaml -# app/config/config.yml +# config/fos_message.yaml fos_message: # ... - thread_manager: app.thread_manager - message_manager: app.message_manager + thread_manager: App\Manager\ThreadManager + message_manager: App\Manager\MessageManager ``` #### The default form does not work with my User entity @@ -150,11 +275,11 @@ You have to redefine two things : You can copy and paste the bundle versions into your application and define them as services: ``` xml - + - + %fos_message.new_thread_form.name% @@ -167,7 +292,7 @@ You can copy and paste the bundle versions into your application and define them And configure the bundle to use your services: ``` yaml -# app/config/config.yml +# config/fos_message.yaml fos_message: # ... diff --git a/UPGRADING.md b/UPGRADING.md index 74a9257a..4751a69d 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -30,7 +30,7 @@ The 1.3 version added the support for Symfony 3.0+. Several changes were made fo # ... new_thread_form: - type: AppBundle\Form\Type\NewThreadFormType + type: App\Form\Type\NewThreadFormType reply_form: - type: AppBundle\Form\Type\ReplyFormType + type: App\Form\Type\ReplyFormType ```