From 715a7bc29216f7de66ef6feae1afc91c20dcb451 Mon Sep 17 00:00:00 2001 From: matks Date: Mon, 18 Jan 2021 18:34:32 +0100 Subject: [PATCH 1/4] Rewrite demoextendsymfonyform1 module without CQRS --- demoextendsymfonyform1/README.md | 18 +- demoextendsymfonyform1/composer.json | 14 +- demoextendsymfonyform1/config/routes.yml | 6 +- demoextendsymfonyform1/config/services.yml | 29 +--- .../demoextendsymfonyform1.php | 158 ++++++++---------- .../Admin/CustomerReviewController.php | 83 +++++++-- .../ToggleIsAllowedToReviewCommand.php | 45 ----- .../UpdateIsAllowedToReviewCommand.php | 60 ------- .../AbstractReviewerHandler.php | 61 ------- .../ToggleIsAllowedToReviewHandler.php | 71 -------- .../UpdateIsAllowedToReviewHandler.php | 64 ------- .../Query/GetReviewerSettingsForForm.php | 42 ----- .../GetReviewerSettingsForFormHandler.php | 45 ----- .../QueryResult/ReviewerSettingsForForm.php | 38 ----- .../src/Entity/Reviewer.php | 4 +- .../CannotCreateReviewerException.php | 2 +- ...otToggleAllowedToReviewStatusException.php | 2 +- .../Exception/ReviewerException.php | 2 +- .../src/Repository/ReviewerRepository.php | 6 +- 19 files changed, 167 insertions(+), 583 deletions(-) delete mode 100755 demoextendsymfonyform1/src/Domain/Reviewer/Command/ToggleIsAllowedToReviewCommand.php delete mode 100644 demoextendsymfonyform1/src/Domain/Reviewer/Command/UpdateIsAllowedToReviewCommand.php delete mode 100644 demoextendsymfonyform1/src/Domain/Reviewer/CommandHandler/AbstractReviewerHandler.php delete mode 100755 demoextendsymfonyform1/src/Domain/Reviewer/CommandHandler/ToggleIsAllowedToReviewHandler.php delete mode 100644 demoextendsymfonyform1/src/Domain/Reviewer/CommandHandler/UpdateIsAllowedToReviewHandler.php delete mode 100644 demoextendsymfonyform1/src/Domain/Reviewer/Query/GetReviewerSettingsForForm.php delete mode 100644 demoextendsymfonyform1/src/Domain/Reviewer/QueryHandler/GetReviewerSettingsForFormHandler.php delete mode 100644 demoextendsymfonyform1/src/Domain/Reviewer/QueryResult/ReviewerSettingsForForm.php rename demoextendsymfonyform1/src/{Domain/Reviewer => }/Exception/CannotCreateReviewerException.php (82%) rename demoextendsymfonyform1/src/{Domain/Reviewer => }/Exception/CannotToggleAllowedToReviewStatusException.php (83%) rename demoextendsymfonyform1/src/{Domain/Reviewer => }/Exception/ReviewerException.php (81%) diff --git a/demoextendsymfonyform1/README.md b/demoextendsymfonyform1/README.md index be2e5fa2..e523c8f7 100755 --- a/demoextendsymfonyform1/README.md +++ b/demoextendsymfonyform1/README.md @@ -1,6 +1,6 @@ # Demonstration of how to insert an inputs inside the Symfony form -Learn using identifiable object and grid hooks as well as combining CQRS pattern from module +Learn using identifiable object and grid hooks. ## About @@ -12,21 +12,23 @@ This new field appears: This modules demonstrates - how to add this field, manage its content and its properties using modern hooks in Symfony pages - - how to use custom [CQRS](https://devdocs.prestashop.com/1.7/development/architecture/domain/cqrs/) Commands and Queries to separate your domain from your application* - how to use Translator inside modern Symfony module -*This part is only demonstrated as a possibility for your module, this is -not mandatory to be done this way. +### Details - ### Supported PrestaShop versions +This module uses an ObjectModel entity to persist the data submitted by the user. +[Other modules](https://github.com/PrestaShop/example-modules/tree/master/demoextendsymfonyform2) demonstrate +how to use DoctrineORM. + +### Supported PrestaShop versions This module is compatible with and 1.7.6.0 and above versions. - ### Requirements +### Requirements 1. Composer, see [Composer](https://getcomposer.org/) to learn more - ### How to install +### How to install 1. Download or clone module into `modules` directory of your PrestaShop installation 2. Rename the directory to make sure that module directory is named `demoextendsymfonyform1`* @@ -35,5 +37,3 @@ not mandatory to be done this way. 4. Install module from Back Office *Because the name of the directory and the name of the main module file must match. - - diff --git a/demoextendsymfonyform1/composer.json b/demoextendsymfonyform1/composer.json index 8c8434cd..7ab61000 100755 --- a/demoextendsymfonyform1/composer.json +++ b/demoextendsymfonyform1/composer.json @@ -1,9 +1,17 @@ { - "name": "prestashop/democqrshooksusage", - "description": "Help developers to understand how to create module using new hooks and apply best practices when using CQRS", + "name": "prestashop/demoextendsymfonyform1", + "authors": [ + { + "name": "Tomas Ilginis" + }, + { + "name": "PrestaShop Core Team" + } + ], + "description": "Help developers to understand how to create module using Symfony hooks", "autoload": { "psr-4": { - "DemoCQRSHooksUsage\\": "src/" + "PrestaShop\\Module\\DemoHowToExtendSymfonyForm\\": "src/" } }, "license": "MIT", diff --git a/demoextendsymfonyform1/config/routes.yml b/demoextendsymfonyform1/config/routes.yml index f7ed5e87..8eb516da 100755 --- a/demoextendsymfonyform1/config/routes.yml +++ b/demoextendsymfonyform1/config/routes.yml @@ -1,8 +1,8 @@ # @see https://devdocs.prestashop.com/1.7/modules/concepts/controllers/admin-controllers/#how-to-map-an-action-of-your-controller-to-a-uri -ps_democqrshooksusage_toggle_is_allowed_for_review: - path: demo-cqrs-hook-usage/{customerId}/toggle-is-allowed-for-review +ps_demoextendsymfonyform_toggle_is_allowed_for_review: + path: ps_demoextendsymfonyform/{customerId}/toggle-is-allowed-for-review methods: [POST] defaults: - _controller: 'DemoCQRSHooksUsage\Controller\Admin\CustomerReviewController::toggleIsAllowedForReviewAction' + _controller: 'PrestaShop\Module\DemoHowToExtendSymfonyForm\Controller\Admin\CustomerReviewController::toggleIsAllowedForReviewAction' requirements: customerId: \d+ diff --git a/demoextendsymfonyform1/config/services.yml b/demoextendsymfonyform1/config/services.yml index 403e4e95..6277c213 100755 --- a/demoextendsymfonyform1/config/services.yml +++ b/demoextendsymfonyform1/config/services.yml @@ -1,33 +1,8 @@ services: _defaults: public: true -# @see https://devdocs.prestashop.com/1.7/development/architecture/migration-guide/forms/cqrs-usage-in-forms/ for CQRS pattern usage examples. - democqrshooksusage.domain.reviewer.command_handler.toggle_is_allowed_to_review_handler: - class: 'DemoCQRSHooksUsage\Domain\Reviewer\CommandHandler\ToggleIsAllowedToReviewHandler' - arguments: - - '@democqrshooksusage.repository.reviewer' - tags: - - name: tactician.handler - command: 'DemoCQRSHooksUsage\Domain\Reviewer\Command\ToggleIsAllowedToReviewCommand' - - democqrshooksusage.domain.reviewer.query_handler.get_reviewer_settings_for_form_handler: - class: 'DemoCQRSHooksUsage\Domain\Reviewer\QueryHandler\GetReviewerSettingsForFormHandler' - arguments: - - '@democqrshooksusage.repository.reviewer' - tags: - - name: tactician.handler - command: 'DemoCQRSHooksUsage\Domain\Reviewer\Query\GetReviewerSettingsForForm' - - democqrshooksusage.domain.reviewer.command_handler.update_is_allowed_to_review_handler: - class: 'DemoCQRSHooksUsage\Domain\Reviewer\CommandHandler\UpdateIsAllowedToReviewHandler' - arguments: - - '@democqrshooksusage.repository.reviewer' - tags: - - name: tactician.handler - command: 'DemoCQRSHooksUsage\Domain\Reviewer\Command\UpdateIsAllowedToReviewCommand' - - democqrshooksusage.repository.reviewer: - class: 'DemoCQRSHooksUsage\Repository\ReviewerRepository' + ps_demoextendsymfonyform.repository.reviewer: + class: 'PrestaShop\Module\DemoHowToExtendSymfonyForm\Repository\ReviewerRepository' arguments: - '@doctrine.dbal.default_connection' - '%database_prefix%' diff --git a/demoextendsymfonyform1/demoextendsymfonyform1.php b/demoextendsymfonyform1/demoextendsymfonyform1.php index 4ac4a24c..9a038db4 100755 --- a/demoextendsymfonyform1/demoextendsymfonyform1.php +++ b/demoextendsymfonyform1/demoextendsymfonyform1.php @@ -8,14 +8,11 @@ * It is also available through the world-wide-web at this URL: https://opensource.org/licenses/AFL-3.0 */ -use DemoCQRSHooksUsage\Domain\Reviewer\Command\UpdateIsAllowedToReviewCommand; -use DemoCQRSHooksUsage\Domain\Reviewer\Exception\CannotCreateReviewerException; -use DemoCQRSHooksUsage\Domain\Reviewer\Exception\CannotToggleAllowedToReviewStatusException; -use DemoCQRSHooksUsage\Domain\Reviewer\Exception\ReviewerException; -use DemoCQRSHooksUsage\Domain\Reviewer\Query\GetReviewerSettingsForForm; -use DemoCQRSHooksUsage\Domain\Reviewer\QueryResult\ReviewerSettingsForForm; + use Doctrine\DBAL\Query\QueryBuilder; -use PrestaShop\PrestaShop\Core\CommandBus\CommandBusInterface; +use PrestaShop\Module\DemoHowToExtendSymfonyForm\Entity\Reviewer; +use PrestaShop\Module\DemoHowToExtendSymfonyForm\Exception\CannotCreateReviewerException; +use PrestaShop\Module\DemoHowToExtendSymfonyForm\Exception\CannotToggleAllowedToReviewStatusException; use PrestaShop\PrestaShop\Core\Domain\Customer\Exception\CustomerException; use PrestaShop\PrestaShop\Core\Grid\Column\Type\Common\ToggleColumn; use PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinitionInterface; @@ -24,18 +21,16 @@ use PrestaShopBundle\Form\Admin\Type\SwitchType; use PrestaShopBundle\Form\Admin\Type\YesAndNoChoiceType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; /** - * Class DemoExtendSymfonyForm1 demonstrates the usage of CQRS pattern and hooks. + * Class DemoExtendSymfonyForm1 demonstrates the usage of Symfony hooks. */ class DemoExtendSymfonyForm1 extends Module { public function __construct() { $this->name = 'demoextendsymfonyform1'; - $this->version = '1.0.0'; + $this->version = '1.1.0'; $this->author = 'Tomas Ilginis'; $this->need_instance = 0; @@ -44,14 +39,14 @@ public function __construct() $this->displayName = $this->getTranslator()->trans( 'Demo Symfony Forms #1', [], - 'Modules.Democqrshooksusage.Admin' + 'Modules.DemoHowToExtendSymfonyForm.Admin' ); $this->description = $this->getTranslator()->trans( - 'Help developers to understand how to create module using new hooks and apply best practices when using CQRS', + 'Help developers to understand how to create module using Symfony page hooks', [], - 'Modules.Democqrshooksusage.Admin' + 'Modules.DemoHowToExtendSymfonyForm.Admin' ); $this->ps_versions_compliancy = [ @@ -96,8 +91,7 @@ public function install() $this->registerHook('actionCustomerFormBuilderModifier') && $this->registerHook('actionAfterCreateCustomerFormHandler') && $this->registerHook('actionAfterUpdateCustomerFormHandler') && - $this->installTables() - ; + $this->installTables(); } public function uninstall() @@ -123,19 +117,18 @@ public function hookActionCustomerGridDefinitionModifier(array $params) ->addAfter( 'optin', (new ToggleColumn('is_allowed_for_review')) - ->setName($translator->trans('Allowed for review', [], 'Modules.Democqrshooksusage.Admin')) + ->setName($translator->trans('Allowed for review', [], 'Modules.DemoHowToExtendSymfonyForm.Admin')) ->setOptions([ 'field' => 'is_allowed_for_review', 'primary_field' => 'id_customer', - 'route' => 'ps_democqrshooksusage_toggle_is_allowed_for_review', + 'route' => 'ps_demoextendsymfonyform_toggle_is_allowed_for_review', 'route_param_name' => 'customerId', ]) - ) - ; + ); $definition->getFilters()->add( (new Filter('is_allowed_for_review', YesAndNoChoiceType::class)) - ->setAssociatedColumn('is_allowed_for_review') + ->setAssociatedColumn('is_allowed_for_review') ); } @@ -158,7 +151,7 @@ public function hookActionCustomerGridQueryBuilderModifier(array $params) $searchQueryBuilder->leftJoin( 'c', - '`' . pSQL(_DB_PREFIX_) . 'democqrshooksusage_reviewer`', + '`' . pSQL(_DB_PREFIX_) . 'demoextendsymfonyform_reviewer`', 'dcur', 'dcur.`id_customer` = c.`id_customer`' ); @@ -189,28 +182,16 @@ public function hookActionCustomerFormBuilderModifier(array $params) /** @var FormBuilderInterface $formBuilder */ $formBuilder = $params['form_builder']; $formBuilder->add('is_allowed_for_review', SwitchType::class, [ - 'label' => $this->getTranslator()->trans('Allow reviews', [], 'Modules.Democqrshooksusage.Admin'), + 'label' => $this->getTranslator()->trans('Allow reviews', [], 'Modules.DemoHowToExtendSymfonyForm.Admin'), 'required' => false, ]); - /** - * @var CommandBusInterface - */ - $queryBus = $this->get('prestashop.core.query_bus'); - - /** - * This part demonstrates the usage of CQRS pattern query to perform read operation from Reviewer entity. - * - * @see https://devdocs.prestashop.com/1.7/development/architecture/cqrs/ for more detailed information. - * - * As this is our recommended approach of reading the data but we not force to use this pattern in modules - - * you can use directly an entity here or wrap it in custom service class. - * - * @var ReviewerSettingsForForm - */ - $reviewerSettings = $queryBus->handle(new GetReviewerSettingsForForm($params['id'])); - - $params['data']['is_allowed_for_review'] = $reviewerSettings->isAllowedForReview(); + $result = false; + if (null !== $params['id']) { + $result = $this->get('ps_demoextendsymfonyform.repository.reviewer')->getIsAllowedToReviewStatus($params['id']); + } + + $params['data']['is_allowed_for_review'] = $result; $formBuilder->setData($params['data']); } @@ -249,25 +230,26 @@ private function updateCustomerReviewStatus(array $params) $customerId = $params['id']; /** @var array $customerFormData */ $customerFormData = $params['form_data']; - $isAllowedForReview = (bool) $customerFormData['is_allowed_for_review']; + $isAllowedForReview = (bool)$customerFormData['is_allowed_for_review']; - /** @var CommandBusInterface $commandBus */ - $commandBus = $this->get('prestashop.core.command_bus'); + $reviewerId = $this->get('ps_demoextendsymfonyform.repository.reviewer')->findIdByCustomer($customerId); + + $reviewer = new Reviewer($reviewerId); + if (0 >= $reviewer->id) { + $reviewer = $this->createReviewer($customerId); + } + $reviewer->is_allowed_for_review = $isAllowedForReview; try { - /* - * This part demonstrates the usage of CQRS pattern command to perform write operation for Reviewer entity. - * @see https://devdocs.prestashop.com/1.7/development/architecture/cqrs/ for more detailed information. - * - * As this is our recommended approach of writing the data but we not force to use this pattern in modules - - * you can use directly an entity here or wrap it in custom service class. - */ - $commandBus->handle(new UpdateIsAllowedToReviewCommand( - $customerId, - $isAllowedForReview - )); - } catch (ReviewerException $exception) { - $this->handleException($exception); + if (false === $reviewer->update()) { + throw new CannotToggleAllowedToReviewStatusException( + sprintf('Failed to change status for reviewer with id "%s"', $reviewer->id) + ); + } + } catch (PrestaShopException $exception) { + throw new CannotToggleAllowedToReviewStatusException( + 'An unexpected error occurred when updating reviewer status' + ); } } @@ -279,7 +261,7 @@ private function updateCustomerReviewStatus(array $params) private function installTables() { $sql = ' - CREATE TABLE IF NOT EXISTS `' . pSQL(_DB_PREFIX_) . 'democqrshooksusage_reviewer` ( + CREATE TABLE IF NOT EXISTS `' . pSQL(_DB_PREFIX_) . 'demoextendsymfonyform_reviewer` ( `id_reviewer` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `id_customer` INT(10) UNSIGNED NOT NULL, `is_allowed_for_review` TINYINT(1) NOT NULL, @@ -297,48 +279,46 @@ private function installTables() */ private function uninstallTables() { - $sql = 'DROP TABLE IF EXISTS `' . pSQL(_DB_PREFIX_) . 'democqrshooksusage_reviewer`'; + $sql = 'DROP TABLE IF EXISTS `' . pSQL(_DB_PREFIX_) . 'demoextendsymfonyform_reviewer`'; return Db::getInstance()->execute($sql); } /** - * Handles exceptions and displays message in more user friendly form. + * Creates a reviewer. * - * @param ReviewerException $exception + * @param $customerId * - * @throws \PrestaShop\PrestaShop\Core\Module\Exception\ModuleErrorException + * @return Reviewer + * + * @throws CannotCreateReviewerException */ - private function handleException(ReviewerException $exception) + protected function createReviewer($customerId) { - $exceptionDictionary = [ - CannotCreateReviewerException::class => $this->getTranslator()->trans( - 'Failed to create a record for customer', - [], - 'Modules.Democqrshooksusage.Admin' - ), - CannotToggleAllowedToReviewStatusException::class => $this->getTranslator()->trans( - 'Failed to toggle is allowed to review status', - [], - 'Modules.Democqrshooksusage.Admin' - ), - ]; - - $exceptionType = get_class($exception); - - if (isset($exceptionDictionary[$exceptionType])) { - $message = $exceptionDictionary[$exceptionType]; - } else { - $message = $this->getTranslator()->trans( - 'An unexpected error occurred. [%type% code %code%]', - [ - '%type%' => $exceptionType, - '%code%' => $exception->getCode(), - ], - 'Admin.Notifications.Error' + try { + $reviewer = new Reviewer(); + $reviewer->id_customer = $customerId; + $reviewer->is_allowed_for_review = 0; + + if (false === $reviewer->save()) { + throw new CannotCreateReviewerException( + sprintf( + 'An error occurred when creating reviewer with customer id "%s"', + $customerId + ) + ); + } + } catch (PrestaShopException $exception) { + throw new CannotCreateReviewerException( + sprintf( + 'An unexpected error occurred when creating reviewer with customer id "%s"', + $customerId + ), + 0, + $exception ); } - throw new \PrestaShop\PrestaShop\Core\Module\Exception\ModuleErrorException($message); + return $reviewer; } } diff --git a/demoextendsymfonyform1/src/Controller/Admin/CustomerReviewController.php b/demoextendsymfonyform1/src/Controller/Admin/CustomerReviewController.php index f5732737..43ff973e 100755 --- a/demoextendsymfonyform1/src/Controller/Admin/CustomerReviewController.php +++ b/demoextendsymfonyform1/src/Controller/Admin/CustomerReviewController.php @@ -8,12 +8,12 @@ * It is also available through the world-wide-web at this URL: https://opensource.org/licenses/AFL-3.0 */ -namespace DemoCQRSHooksUsage\Controller\Admin; +namespace PrestaShop\Module\DemoHowToExtendSymfonyForm\Controller\Admin; -use DemoCQRSHooksUsage\Domain\Reviewer\Command\ToggleIsAllowedToReviewCommand; -use DemoCQRSHooksUsage\Domain\Reviewer\Exception\CannotCreateReviewerException; -use DemoCQRSHooksUsage\Domain\Reviewer\Exception\CannotToggleAllowedToReviewStatusException; -use DemoCQRSHooksUsage\Domain\Reviewer\Exception\ReviewerException; +use PrestaShop\Module\DemoHowToExtendSymfonyForm\Exception\CannotCreateReviewerException; +use PrestaShop\Module\DemoHowToExtendSymfonyForm\Exception\CannotToggleAllowedToReviewStatusException; +use PrestaShop\Module\DemoHowToExtendSymfonyForm\Exception\ReviewerException; +use PrestaShop\Module\DemoHowToExtendSymfonyForm\Entity\Reviewer; use PrestaShop\PrestaShop\Core\Domain\Customer\Exception\CustomerException; use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -35,14 +35,25 @@ class CustomerReviewController extends FrameworkBundleAdminController public function toggleIsAllowedForReviewAction($customerId) { try { - /* - * This part demonstrates the usage of CQRS pattern command to perform write operation for Reviewer entity. - * @see https://devdocs.prestashop.com/1.7/development/architecture/cqrs/ for more detailed information. - * - * As this is our recommended approach of writing the data but we not force to use this pattern in modules - - * you can use directly an entity here or wrap it in custom service class. - */ - $this->getCommandBus()->handle(new ToggleIsAllowedToReviewCommand((int) $customerId)); + $reviewerId = $this->get('ps_demoextendsymfonyform.repository.reviewer')->findIdByCustomer($customerId); + + $reviewer = new Reviewer($reviewerId); + if (0 >= $reviewer->id) { + $reviewer = $this->createReviewerIfNeeded($customerId); + } + $reviewer->is_allowed_for_review = (bool)!$reviewer->is_allowed_for_review; + + try { + if (false === $reviewer->update()) { + throw new CannotToggleAllowedToReviewStatusException( + sprintf('Failed to change status for reviewer with id "%s"', $reviewer->id) + ); + } + } catch (\PrestaShopException $exception) { + throw new CannotToggleAllowedToReviewStatusException( + 'An unexpected error occurred when updating reviewer status' + ); + } $this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success')); } catch (ReviewerException $e) { @@ -56,8 +67,6 @@ public function toggleIsAllowedForReviewAction($customerId) * Gets error message mappings which are later used to display friendly user error message instead of the * exception message. * - * @see https://devdocs.prestashop.com/1.7/development/architecture/domain-exceptions/ for more detailed explanation - * * @return array */ private function getErrorMessageMapping() @@ -65,16 +74,54 @@ private function getErrorMessageMapping() return [ CustomerException::class => $this->trans( 'Something bad happened when trying to get customer id', - 'Modules.Democqrshooksusage.Customerreviewcontroller' + 'Modules.DemoHowToExtendSymfonyForm.Customerreviewcontroller' ), CannotCreateReviewerException::class => $this->trans( 'Failed to create reviewer', - 'Modules.Democqrshooksusage.Customerreviewcontroller' + 'Modules.DemoHowToExtendSymfonyForm.Customerreviewcontroller' ), CannotToggleAllowedToReviewStatusException::class => $this->trans( 'An error occurred while updating the status.', - 'Modules.Democqrshooksusage.Customerreviewcontroller' + 'Modules.DemoHowToExtendSymfonyForm.Customerreviewcontroller' ), ]; } + + /** + * Creates a reviewer. Used when toggle action is used on customer whose data is empty. + * + * @param $customerId + * + * @return Reviewer + * + * @throws CannotCreateReviewerException + */ + protected function createReviewerIfNeeded($customerId) + { + try { + $reviewer = new Reviewer(); + $reviewer->id_customer = $customerId; + $reviewer->is_allowed_for_review = 0; + + if (false === $reviewer->save()) { + throw new CannotCreateReviewerException( + sprintf( + 'An error occurred when creating reviewer with customer id "%s"', + $customerId + ) + ); + } + } catch (\PrestaShopException $exception) { + throw new CannotCreateReviewerException( + sprintf( + 'An unexpected error occurred when creating reviewer with customer id "%s"', + $customerId + ), + 0, + $exception + ); + } + + return $reviewer; + } } diff --git a/demoextendsymfonyform1/src/Domain/Reviewer/Command/ToggleIsAllowedToReviewCommand.php b/demoextendsymfonyform1/src/Domain/Reviewer/Command/ToggleIsAllowedToReviewCommand.php deleted file mode 100755 index f4547fc8..00000000 --- a/demoextendsymfonyform1/src/Domain/Reviewer/Command/ToggleIsAllowedToReviewCommand.php +++ /dev/null @@ -1,45 +0,0 @@ -customerId = new CustomerId($customerId); - } - - /** - * @return CustomerId - */ - public function getCustomerId() - { - return $this->customerId; - } -} diff --git a/demoextendsymfonyform1/src/Domain/Reviewer/Command/UpdateIsAllowedToReviewCommand.php b/demoextendsymfonyform1/src/Domain/Reviewer/Command/UpdateIsAllowedToReviewCommand.php deleted file mode 100644 index b81704f9..00000000 --- a/demoextendsymfonyform1/src/Domain/Reviewer/Command/UpdateIsAllowedToReviewCommand.php +++ /dev/null @@ -1,60 +0,0 @@ -customerId = new CustomerId($customerId); - $this->isAllowedToReview = $isAllowedToReview; - } - - /** - * @return CustomerId - */ - public function getCustomerId() - { - return $this->customerId; - } - - /** - * @return bool - */ - public function isAllowedToReview() - { - return $this->isAllowedToReview; - } -} diff --git a/demoextendsymfonyform1/src/Domain/Reviewer/CommandHandler/AbstractReviewerHandler.php b/demoextendsymfonyform1/src/Domain/Reviewer/CommandHandler/AbstractReviewerHandler.php deleted file mode 100644 index a9773835..00000000 --- a/demoextendsymfonyform1/src/Domain/Reviewer/CommandHandler/AbstractReviewerHandler.php +++ /dev/null @@ -1,61 +0,0 @@ -id_customer = $customerId; - $reviewer->is_allowed_for_review = 0; - - if (false === $reviewer->save()) { - throw new CannotCreateReviewerException( - sprintf( - 'An error occurred when creating reviewer with customer id "%s"', - $customerId - ) - ); - } - } catch (PrestaShopException $exception) { - /* - * @see https://devdocs.prestashop.com/1.7/development/architecture/domain-exceptions/ - */ - throw new CannotCreateReviewerException( - sprintf( - 'An unexpected error occurred when creating reviewer with customer id "%s"', - $customerId - ), - 0, - $exception - ); - } - - return $reviewer; - } -} diff --git a/demoextendsymfonyform1/src/Domain/Reviewer/CommandHandler/ToggleIsAllowedToReviewHandler.php b/demoextendsymfonyform1/src/Domain/Reviewer/CommandHandler/ToggleIsAllowedToReviewHandler.php deleted file mode 100755 index 6484f74b..00000000 --- a/demoextendsymfonyform1/src/Domain/Reviewer/CommandHandler/ToggleIsAllowedToReviewHandler.php +++ /dev/null @@ -1,71 +0,0 @@ -reviewerRepository = $reviewerRepository; - } - - /** - * @param ToggleIsAllowedToReviewCommand $command - * - * @throws CannotCreateReviewerException - * @throws CannotToggleAllowedToReviewStatusException - */ - public function handle(ToggleIsAllowedToReviewCommand $command) - { - $reviewerId = $this->reviewerRepository->findIdByCustomer($command->getCustomerId()->getValue()); - - $reviewer = new Reviewer($reviewerId); - - if (0 >= $reviewer->id) { - $reviewer = $this->createReviewer($command->getCustomerId()->getValue()); - } - - $reviewer->is_allowed_for_review = (bool) !$reviewer->is_allowed_for_review; - - try { - if (false === $reviewer->update()) { - throw new CannotToggleAllowedToReviewStatusException( - sprintf('Failed to change status for reviewer with id "%s"', $reviewer->id) - ); - } - } catch (PrestaShopException $exception) { - /* - * @see https://devdocs.prestashop.com/1.7/development/architecture/domain-exceptions/ - */ - throw new CannotToggleAllowedToReviewStatusException( - 'An unexpected error occurred when updating reviewer status' - ); - } - } -} diff --git a/demoextendsymfonyform1/src/Domain/Reviewer/CommandHandler/UpdateIsAllowedToReviewHandler.php b/demoextendsymfonyform1/src/Domain/Reviewer/CommandHandler/UpdateIsAllowedToReviewHandler.php deleted file mode 100644 index a99c8865..00000000 --- a/demoextendsymfonyform1/src/Domain/Reviewer/CommandHandler/UpdateIsAllowedToReviewHandler.php +++ /dev/null @@ -1,64 +0,0 @@ -reviewerRepository = $reviewerRepository; - } - - public function handle(UpdateIsAllowedToReviewCommand $command) - { - $reviewerId = $this->reviewerRepository->findIdByCustomer($command->getCustomerId()->getValue()); - - $reviewer = new Reviewer($reviewerId); - - if (0 >= $reviewer->id) { - $reviewer = $this->createReviewer($command->getCustomerId()->getValue()); - } - - $reviewer->is_allowed_for_review = $command->isAllowedToReview(); - - try { - if (false === $reviewer->update()) { - throw new CannotToggleAllowedToReviewStatusException( - sprintf('Failed to change status for reviewer with id "%s"', $reviewer->id) - ); - } - } catch (PrestaShopException $exception) { - /* - * @see https://devdocs.prestashop.com/1.7/development/architecture/domain-exceptions/ - */ - throw new CannotToggleAllowedToReviewStatusException( - 'An unexpected error occurred when updating reviewer status' - ); - } - } -} diff --git a/demoextendsymfonyform1/src/Domain/Reviewer/Query/GetReviewerSettingsForForm.php b/demoextendsymfonyform1/src/Domain/Reviewer/Query/GetReviewerSettingsForForm.php deleted file mode 100644 index a36826bf..00000000 --- a/demoextendsymfonyform1/src/Domain/Reviewer/Query/GetReviewerSettingsForForm.php +++ /dev/null @@ -1,42 +0,0 @@ -customerId = null !== $customerId ? new CustomerId((int) $customerId) : null; - } - - /** - * @return CustomerId|null - */ - public function getCustomerId() - { - return $this->customerId; - } -} diff --git a/demoextendsymfonyform1/src/Domain/Reviewer/QueryHandler/GetReviewerSettingsForFormHandler.php b/demoextendsymfonyform1/src/Domain/Reviewer/QueryHandler/GetReviewerSettingsForFormHandler.php deleted file mode 100644 index 28496f6e..00000000 --- a/demoextendsymfonyform1/src/Domain/Reviewer/QueryHandler/GetReviewerSettingsForFormHandler.php +++ /dev/null @@ -1,45 +0,0 @@ -reviewerRepository = $reviewerRepository; - } - - public function handle(GetReviewerSettingsForForm $query) - { - if (null === $query->getCustomerId()) { - return new ReviewerSettingsForForm(false); - } - - return new ReviewerSettingsForForm( - $this->reviewerRepository->getIsAllowedToReviewStatus($query->getCustomerId()->getValue()) - ); - } -} diff --git a/demoextendsymfonyform1/src/Domain/Reviewer/QueryResult/ReviewerSettingsForForm.php b/demoextendsymfonyform1/src/Domain/Reviewer/QueryResult/ReviewerSettingsForForm.php deleted file mode 100644 index 4c1dcc6b..00000000 --- a/demoextendsymfonyform1/src/Domain/Reviewer/QueryResult/ReviewerSettingsForForm.php +++ /dev/null @@ -1,38 +0,0 @@ -isAllowedForReview = $isAllowedForReview; - } - - /** - * @return bool - */ - public function isAllowedForReview() - { - return $this->isAllowedForReview; - } -} diff --git a/demoextendsymfonyform1/src/Entity/Reviewer.php b/demoextendsymfonyform1/src/Entity/Reviewer.php index b33ba505..7b5b2cec 100755 --- a/demoextendsymfonyform1/src/Entity/Reviewer.php +++ b/demoextendsymfonyform1/src/Entity/Reviewer.php @@ -8,7 +8,7 @@ * It is also available through the world-wide-web at this URL: https://opensource.org/licenses/AFL-3.0 */ -namespace DemoCQRSHooksUsage\Entity; +namespace PrestaShop\Module\DemoHowToExtendSymfonyForm\Entity; use PrestaShop\PrestaShop\Adapter\Entity\ObjectModel; @@ -28,7 +28,7 @@ class Reviewer extends ObjectModel public $is_allowed_for_review; public static $definition = [ - 'table' => 'democqrshooksusage_reviewer', + 'table' => 'demoextendsymfonyform_reviewer', 'primary' => 'id_reviewer', 'fields' => [ 'id_customer' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'], diff --git a/demoextendsymfonyform1/src/Domain/Reviewer/Exception/CannotCreateReviewerException.php b/demoextendsymfonyform1/src/Exception/CannotCreateReviewerException.php similarity index 82% rename from demoextendsymfonyform1/src/Domain/Reviewer/Exception/CannotCreateReviewerException.php rename to demoextendsymfonyform1/src/Exception/CannotCreateReviewerException.php index b8dddf4f..1161ca87 100644 --- a/demoextendsymfonyform1/src/Domain/Reviewer/Exception/CannotCreateReviewerException.php +++ b/demoextendsymfonyform1/src/Exception/CannotCreateReviewerException.php @@ -8,7 +8,7 @@ * It is also available through the world-wide-web at this URL: https://opensource.org/licenses/AFL-3.0 */ -namespace DemoCQRSHooksUsage\Domain\Reviewer\Exception; +namespace PrestaShop\Module\DemoHowToExtendSymfonyForm\Exception; class CannotCreateReviewerException extends ReviewerException { diff --git a/demoextendsymfonyform1/src/Domain/Reviewer/Exception/CannotToggleAllowedToReviewStatusException.php b/demoextendsymfonyform1/src/Exception/CannotToggleAllowedToReviewStatusException.php similarity index 83% rename from demoextendsymfonyform1/src/Domain/Reviewer/Exception/CannotToggleAllowedToReviewStatusException.php rename to demoextendsymfonyform1/src/Exception/CannotToggleAllowedToReviewStatusException.php index de0bdc82..e87f3a8a 100644 --- a/demoextendsymfonyform1/src/Domain/Reviewer/Exception/CannotToggleAllowedToReviewStatusException.php +++ b/demoextendsymfonyform1/src/Exception/CannotToggleAllowedToReviewStatusException.php @@ -8,7 +8,7 @@ * It is also available through the world-wide-web at this URL: https://opensource.org/licenses/AFL-3.0 */ -namespace DemoCQRSHooksUsage\Domain\Reviewer\Exception; +namespace PrestaShop\Module\DemoHowToExtendSymfonyForm\Exception; class CannotToggleAllowedToReviewStatusException extends ReviewerException { diff --git a/demoextendsymfonyform1/src/Domain/Reviewer/Exception/ReviewerException.php b/demoextendsymfonyform1/src/Exception/ReviewerException.php similarity index 81% rename from demoextendsymfonyform1/src/Domain/Reviewer/Exception/ReviewerException.php rename to demoextendsymfonyform1/src/Exception/ReviewerException.php index 8b798f82..2cdaf9c7 100644 --- a/demoextendsymfonyform1/src/Domain/Reviewer/Exception/ReviewerException.php +++ b/demoextendsymfonyform1/src/Exception/ReviewerException.php @@ -8,7 +8,7 @@ * It is also available through the world-wide-web at this URL: https://opensource.org/licenses/AFL-3.0 */ -namespace DemoCQRSHooksUsage\Domain\Reviewer\Exception; +namespace PrestaShop\Module\DemoHowToExtendSymfonyForm\Exception; class ReviewerException extends \Exception { diff --git a/demoextendsymfonyform1/src/Repository/ReviewerRepository.php b/demoextendsymfonyform1/src/Repository/ReviewerRepository.php index bc91e20f..2430e260 100644 --- a/demoextendsymfonyform1/src/Repository/ReviewerRepository.php +++ b/demoextendsymfonyform1/src/Repository/ReviewerRepository.php @@ -8,7 +8,7 @@ * It is also available through the world-wide-web at this URL: https://opensource.org/licenses/AFL-3.0 */ -namespace DemoCQRSHooksUsage\Repository; +namespace PrestaShop\Module\DemoHowToExtendSymfonyForm\Repository; use Doctrine\DBAL\Connection; use PDO; @@ -48,7 +48,7 @@ public function findIdByCustomer($customerId) $queryBuilder ->select('`id_reviewer`') - ->from($this->dbPrefix . 'democqrshooksusage_reviewer') + ->from($this->dbPrefix . 'demoextendsymfonyform_reviewer') ->where('`id_customer` = :customer_id') ; @@ -70,7 +70,7 @@ public function getIsAllowedToReviewStatus($customerId) $queryBuilder ->select('`is_allowed_for_review`') - ->from($this->dbPrefix . 'democqrshooksusage_reviewer') + ->from($this->dbPrefix . 'demoextendsymfonyform_reviewer') ->where('`id_customer` = :customer_id') ; From c9aaa8fb0959c36cc0e13d00e666fb48219563d2 Mon Sep 17 00:00:00 2001 From: matks Date: Tue, 19 Jan 2021 10:14:32 +0100 Subject: [PATCH 2/4] Modify demoextendsymfonyform1 author --- demoextendsymfonyform1/demoextendsymfonyform1.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demoextendsymfonyform1/demoextendsymfonyform1.php b/demoextendsymfonyform1/demoextendsymfonyform1.php index 9a038db4..6c212219 100755 --- a/demoextendsymfonyform1/demoextendsymfonyform1.php +++ b/demoextendsymfonyform1/demoextendsymfonyform1.php @@ -31,7 +31,7 @@ public function __construct() { $this->name = 'demoextendsymfonyform1'; $this->version = '1.1.0'; - $this->author = 'Tomas Ilginis'; + $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); From 4ff2352841c63af46b45852140a11e9d3935f853 Mon Sep 17 00:00:00 2001 From: Mathieu Ferment Date: Thu, 21 Jan 2021 18:08:50 +0100 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Krystian Podemski --- demoextendsymfonyform1/demoextendsymfonyform1.php | 10 +++++----- .../src/Controller/Admin/CustomerReviewController.php | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/demoextendsymfonyform1/demoextendsymfonyform1.php b/demoextendsymfonyform1/demoextendsymfonyform1.php index 6c212219..6830b8e5 100755 --- a/demoextendsymfonyform1/demoextendsymfonyform1.php +++ b/demoextendsymfonyform1/demoextendsymfonyform1.php @@ -44,7 +44,7 @@ public function __construct() $this->description = $this->getTranslator()->trans( - 'Help developers to understand how to create module using Symfony page hooks', + 'Help developers to understand how to create module using few Symfony hooks available in PrestaShop', [], 'Modules.DemoHowToExtendSymfonyForm.Admin' ); @@ -188,7 +188,7 @@ public function hookActionCustomerFormBuilderModifier(array $params) $result = false; if (null !== $params['id']) { - $result = $this->get('ps_demoextendsymfonyform.repository.reviewer')->getIsAllowedToReviewStatus($params['id']); + $result = $this->get('ps_demoextendsymfonyform.repository.reviewer')->getIsAllowedToReviewStatus((int) $params['id']); } $params['data']['is_allowed_for_review'] = $result; @@ -230,7 +230,7 @@ private function updateCustomerReviewStatus(array $params) $customerId = $params['id']; /** @var array $customerFormData */ $customerFormData = $params['form_data']; - $isAllowedForReview = (bool)$customerFormData['is_allowed_for_review']; + $isAllowedForReview = (bool) $customerFormData['is_allowed_for_review']; $reviewerId = $this->get('ps_demoextendsymfonyform.repository.reviewer')->findIdByCustomer($customerId); @@ -287,13 +287,13 @@ private function uninstallTables() /** * Creates a reviewer. * - * @param $customerId + * @param int $customerId * * @return Reviewer * * @throws CannotCreateReviewerException */ - protected function createReviewer($customerId) + protected function createReviewer(int $customerId) { try { $reviewer = new Reviewer(); diff --git a/demoextendsymfonyform1/src/Controller/Admin/CustomerReviewController.php b/demoextendsymfonyform1/src/Controller/Admin/CustomerReviewController.php index 43ff973e..79ad4a6e 100755 --- a/demoextendsymfonyform1/src/Controller/Admin/CustomerReviewController.php +++ b/demoextendsymfonyform1/src/Controller/Admin/CustomerReviewController.php @@ -32,16 +32,16 @@ class CustomerReviewController extends FrameworkBundleAdminController * * @return RedirectResponse */ - public function toggleIsAllowedForReviewAction($customerId) + public function toggleIsAllowedForReviewAction(int $customerId) { try { $reviewerId = $this->get('ps_demoextendsymfonyform.repository.reviewer')->findIdByCustomer($customerId); - $reviewer = new Reviewer($reviewerId); + $reviewer = new Reviewer((int) $reviewerId); if (0 >= $reviewer->id) { $reviewer = $this->createReviewerIfNeeded($customerId); } - $reviewer->is_allowed_for_review = (bool)!$reviewer->is_allowed_for_review; + $reviewer->is_allowed_for_review = (bool) !$reviewer->is_allowed_for_review; try { if (false === $reviewer->update()) { @@ -90,13 +90,13 @@ private function getErrorMessageMapping() /** * Creates a reviewer. Used when toggle action is used on customer whose data is empty. * - * @param $customerId + * @param int $customerId * * @return Reviewer * * @throws CannotCreateReviewerException */ - protected function createReviewerIfNeeded($customerId) + protected function createReviewerIfNeeded(int $customerId) { try { $reviewer = new Reviewer(); From 2e89b7a451ad889a6175cc7c01aa6d02f98fd7ac Mon Sep 17 00:00:00 2001 From: matks Date: Tue, 26 Jan 2021 16:01:14 +0100 Subject: [PATCH 4/4] Fix composer configuration for demoextendsymfonyform1 --- demoextendsymfonyform1/composer.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/demoextendsymfonyform1/composer.json b/demoextendsymfonyform1/composer.json index 7ab61000..2c7414fa 100755 --- a/demoextendsymfonyform1/composer.json +++ b/demoextendsymfonyform1/composer.json @@ -15,5 +15,8 @@ } }, "license": "MIT", - "type": "prestashop-module" + "type": "prestashop-module", + "config": { + "prepend-autoloader": false + } }