diff --git a/demoextendsymfonyform3/.gitignore b/demoextendsymfonyform3/.gitignore new file mode 100644 index 00000000..0ed33711 --- /dev/null +++ b/demoextendsymfonyform3/.gitignore @@ -0,0 +1,4 @@ +vendor/ +composer.lock +.idea +js/node_modules diff --git a/demoextendsymfonyform3/README.md b/demoextendsymfonyform3/README.md new file mode 100755 index 00000000..68f40488 --- /dev/null +++ b/demoextendsymfonyform3/README.md @@ -0,0 +1,37 @@ +# Demonstration of how to use CQRS in a module + +## About + +This module adds a new field to Customer: a yes/no field "is allowed to review". +This new field appears: +- in the Customers listing as a new column +- in the Customers Add/Edit form as a new field you can manage + +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 demonstrated as a possibility for your module, this is +not mandatory to be done this way. + + ### Supported PrestaShop versions + + This module is compatible with and 1.7.6.0 and above versions. + + ### Requirements + + 1. Composer, see [Composer](https://getcomposer.org/) to learn more + + ### 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 `demoextendsymfonyform3`* + 3. `cd` into module's directory and run following commands: + - `composer install` - to download dependencies into vendor folder + 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/demoextendsymfonyform3/composer.json b/demoextendsymfonyform3/composer.json new file mode 100755 index 00000000..72d1e44f --- /dev/null +++ b/demoextendsymfonyform3/composer.json @@ -0,0 +1,11 @@ +{ + "name": "prestashop/demoextendsymfonyform3", + "description": "Help developers to understand how to create module using new hooks and apply best practices when using CQRS", + "autoload": { + "psr-4": { + "DemoCQRSHooksUsage\\": "src/" + } + }, + "license": "MIT", + "type": "prestashop-module" +} diff --git a/demoextendsymfonyform3/config/routes.yml b/demoextendsymfonyform3/config/routes.yml new file mode 100755 index 00000000..f7ed5e87 --- /dev/null +++ b/demoextendsymfonyform3/config/routes.yml @@ -0,0 +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 + methods: [POST] + defaults: + _controller: 'DemoCQRSHooksUsage\Controller\Admin\CustomerReviewController::toggleIsAllowedForReviewAction' + requirements: + customerId: \d+ diff --git a/demoextendsymfonyform3/config/services.yml b/demoextendsymfonyform3/config/services.yml new file mode 100755 index 00000000..403e4e95 --- /dev/null +++ b/demoextendsymfonyform3/config/services.yml @@ -0,0 +1,33 @@ +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' + arguments: + - '@doctrine.dbal.default_connection' + - '%database_prefix%' diff --git a/demoextendsymfonyform3/demoextendsymfonyform3.php b/demoextendsymfonyform3/demoextendsymfonyform3.php new file mode 100755 index 00000000..9c3745f7 --- /dev/null +++ b/demoextendsymfonyform3/demoextendsymfonyform3.php @@ -0,0 +1,344 @@ +name = 'demoextendsymfonyform3'; + $this->version = '1.0.0'; + $this->author = 'Mathieu Ferment'; + $this->need_instance = 0; + + parent::__construct(); + + $this->displayName = $this->getTranslator()->trans( + 'Demo Symfony Forms #3', + [], + 'Modules.Democqrshooksusage.Admin' + ); + + $this->description = + $this->getTranslator()->trans( + 'Help developers to understand how to create module using CQRS', + [], + 'Modules.Democqrshooksusage.Admin' + ); + + $this->ps_versions_compliancy = [ + 'min' => '1.7.6.0', + 'max' => _PS_VERSION_, + ]; + } + + /** + * This function is required in order to make module compatible with new translation system. + * + * @return bool + */ + public function isUsingNewTranslationSystem() + { + return true; + } + + /** + * Install module and register hooks to allow grid modification. + * + * @see https://devdocs.prestashop.com/1.7/modules/concepts/hooks/use-hooks-on-modern-pages/ + * + * @return bool + */ + public function install() + { + return parent::install() && + // Register hook to allow Customer grid definition modifications. + // Each grid's definition modification hook has it's own name. Hook name is built using + // this structure: "action{grid_id}GridDefinitionModifier", in this case "grid_id" is "customer" + // this means we will be modifying "Sell > Customers" page grid. + // You can check any definition factory service in PrestaShop\PrestaShop\Core\Grid\Definition\Factory + // to see available grid ids. Grid id is returned by `getId()` method. + $this->registerHook('actionCustomerGridDefinitionModifier') && + // Register hook to allow Customer grid query modifications which allows to add any sql condition. + $this->registerHook('actionCustomerGridQueryBuilderModifier') && + // Register hook to allow overriding customer form + // this structure: "action{block_prefix}FormBuilderModifier", in this case "block_prefix" is "customer" + // {block_prefix} is either retrieved automatically by its type. E.g "ManufacturerType" will be "manufacturer" + // or it can be modified in form type by overriding "getBlockPrefix" function + $this->registerHook('actionCustomerFormBuilderModifier') && + $this->registerHook('actionAfterCreateCustomerFormHandler') && + $this->registerHook('actionAfterUpdateCustomerFormHandler') && + $this->installTables() + ; + } + + public function uninstall() + { + return parent::uninstall() && $this->uninstallTables(); + } + + /** + * Hook allows to modify Customers grid definition. + * This hook is a right place to add/remove columns or actions (bulk, grid). + * + * @param array $params + */ + public function hookActionCustomerGridDefinitionModifier(array $params) + { + /** @var GridDefinitionInterface $definition */ + $definition = $params['definition']; + + $translator = $this->getTranslator(); + + $definition + ->getColumns() + ->addAfter( + 'optin', + (new ToggleColumn('is_allowed_for_review')) + ->setName($translator->trans('Allowed for review', [], 'Modules.Democqrshooksusage.Admin')) + ->setOptions([ + 'field' => 'is_allowed_for_review', + 'primary_field' => 'id_customer', + 'route' => 'ps_democqrshooksusage_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') + ); + } + + /** + * Hook allows to modify Customers query builder and add custom sql statements. + * + * @param array $params + */ + public function hookActionCustomerGridQueryBuilderModifier(array $params) + { + /** @var QueryBuilder $searchQueryBuilder */ + $searchQueryBuilder = $params['search_query_builder']; + + /** @var CustomerFilters $searchCriteria */ + $searchCriteria = $params['search_criteria']; + + $searchQueryBuilder->addSelect( + 'IF(dcur.`is_allowed_for_review` IS NULL,0,dcur.`is_allowed_for_review`) AS `is_allowed_for_review`' + ); + + $searchQueryBuilder->leftJoin( + 'c', + '`' . pSQL(_DB_PREFIX_) . 'democqrshooksusage_reviewer`', + 'dcur', + 'dcur.`id_customer` = c.`id_customer`' + ); + + if ('is_allowed_for_review' === $searchCriteria->getOrderBy()) { + $searchQueryBuilder->orderBy('dcur.`is_allowed_for_review`', $searchCriteria->getOrderWay()); + } + + foreach ($searchCriteria->getFilters() as $filterName => $filterValue) { + if ('is_allowed_for_review' === $filterName) { + $searchQueryBuilder->andWhere('dcur.`is_allowed_for_review` = :is_allowed_for_review'); + $searchQueryBuilder->setParameter('is_allowed_for_review', $filterValue); + + if (!$filterValue) { + $searchQueryBuilder->orWhere('dcur.`is_allowed_for_review` IS NULL'); + } + } + } + } + + /** + * Hook allows to modify Customers form and add additional form fields as well as modify or add new data to the forms. + * + * @param array $params + */ + 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'), + '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(); + + $formBuilder->setData($params['data']); + } + + /** + * Hook allows to modify Customers form and add additional form fields as well as modify or add new data to the forms. + * + * @param array $params + * + * @throws CustomerException + */ + public function hookActionAfterUpdateCustomerFormHandler(array $params) + { + $this->updateCustomerReviewStatus($params); + } + + /** + * Hook allows to modify Customers form and add additional form fields as well as modify or add new data to the forms. + * + * @param array $params + * + * @throws CustomerException + */ + public function hookActionAfterCreateCustomerFormHandler(array $params) + { + $this->updateCustomerReviewStatus($params); + } + + /** + * @param array $params + * + * @throws \PrestaShop\PrestaShop\Core\Module\Exception\ModuleErrorException + */ + private function updateCustomerReviewStatus(array $params) + { + $customerId = $params['id']; + /** @var array $customerFormData */ + $customerFormData = $params['form_data']; + $isAllowedForReview = (bool) $customerFormData['is_allowed_for_review']; + + /** @var CommandBusInterface $commandBus */ + $commandBus = $this->get('prestashop.core.command_bus'); + + 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); + } + } + + /** + * Installs sample tables required for demonstration. + * + * @return bool + */ + private function installTables() + { + $sql = ' + CREATE TABLE IF NOT EXISTS `' . pSQL(_DB_PREFIX_) . 'democqrshooksusage_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, + PRIMARY KEY (`id_reviewer`) + ) ENGINE=' . pSQL(_MYSQL_ENGINE_) . ' COLLATE=utf8_unicode_ci; + '; + + return Db::getInstance()->execute($sql); + } + + /** + * Uninstalls sample tables required for demonstration. + * + * @return bool + */ + private function uninstallTables() + { + $sql = 'DROP TABLE IF EXISTS `' . pSQL(_DB_PREFIX_) . 'democqrshooksusage_reviewer`'; + + return Db::getInstance()->execute($sql); + } + + /** + * Handles exceptions and displays message in more user friendly form. + * + * @param ReviewerException $exception + * + * @throws \PrestaShop\PrestaShop\Core\Module\Exception\ModuleErrorException + */ + private function handleException(ReviewerException $exception) + { + $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' + ); + } + + throw new \PrestaShop\PrestaShop\Core\Module\Exception\ModuleErrorException($message); + } +} diff --git a/demoextendsymfonyform3/src/Controller/Admin/CustomerReviewController.php b/demoextendsymfonyform3/src/Controller/Admin/CustomerReviewController.php new file mode 100755 index 00000000..f5732737 --- /dev/null +++ b/demoextendsymfonyform3/src/Controller/Admin/CustomerReviewController.php @@ -0,0 +1,80 @@ + Customers" page. + * + * @see https://devdocs.prestashop.com/1.7/modules/concepts/controllers/admin-controllers/ for more details. + */ +class CustomerReviewController extends FrameworkBundleAdminController +{ + /** + * Catches the toggle action of customer review. + * + * @param int $customerId + * + * @return RedirectResponse + */ + 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)); + + $this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success')); + } catch (ReviewerException $e) { + $this->addFlash('error', $this->getErrorMessageForException($e, $this->getErrorMessageMapping())); + } + + return $this->redirectToRoute('admin_customers_index'); + } + + /** + * 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() + { + return [ + CustomerException::class => $this->trans( + 'Something bad happened when trying to get customer id', + 'Modules.Democqrshooksusage.Customerreviewcontroller' + ), + CannotCreateReviewerException::class => $this->trans( + 'Failed to create reviewer', + 'Modules.Democqrshooksusage.Customerreviewcontroller' + ), + CannotToggleAllowedToReviewStatusException::class => $this->trans( + 'An error occurred while updating the status.', + 'Modules.Democqrshooksusage.Customerreviewcontroller' + ), + ]; + } +} diff --git a/demoextendsymfonyform3/src/Domain/Reviewer/Command/ToggleIsAllowedToReviewCommand.php b/demoextendsymfonyform3/src/Domain/Reviewer/Command/ToggleIsAllowedToReviewCommand.php new file mode 100755 index 00000000..f4547fc8 --- /dev/null +++ b/demoextendsymfonyform3/src/Domain/Reviewer/Command/ToggleIsAllowedToReviewCommand.php @@ -0,0 +1,45 @@ +customerId = new CustomerId($customerId); + } + + /** + * @return CustomerId + */ + public function getCustomerId() + { + return $this->customerId; + } +} diff --git a/demoextendsymfonyform3/src/Domain/Reviewer/Command/UpdateIsAllowedToReviewCommand.php b/demoextendsymfonyform3/src/Domain/Reviewer/Command/UpdateIsAllowedToReviewCommand.php new file mode 100644 index 00000000..b81704f9 --- /dev/null +++ b/demoextendsymfonyform3/src/Domain/Reviewer/Command/UpdateIsAllowedToReviewCommand.php @@ -0,0 +1,60 @@ +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/demoextendsymfonyform3/src/Domain/Reviewer/CommandHandler/AbstractReviewerHandler.php b/demoextendsymfonyform3/src/Domain/Reviewer/CommandHandler/AbstractReviewerHandler.php new file mode 100644 index 00000000..a9773835 --- /dev/null +++ b/demoextendsymfonyform3/src/Domain/Reviewer/CommandHandler/AbstractReviewerHandler.php @@ -0,0 +1,61 @@ +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/demoextendsymfonyform3/src/Domain/Reviewer/CommandHandler/ToggleIsAllowedToReviewHandler.php b/demoextendsymfonyform3/src/Domain/Reviewer/CommandHandler/ToggleIsAllowedToReviewHandler.php new file mode 100755 index 00000000..6484f74b --- /dev/null +++ b/demoextendsymfonyform3/src/Domain/Reviewer/CommandHandler/ToggleIsAllowedToReviewHandler.php @@ -0,0 +1,71 @@ +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/demoextendsymfonyform3/src/Domain/Reviewer/CommandHandler/UpdateIsAllowedToReviewHandler.php b/demoextendsymfonyform3/src/Domain/Reviewer/CommandHandler/UpdateIsAllowedToReviewHandler.php new file mode 100644 index 00000000..a99c8865 --- /dev/null +++ b/demoextendsymfonyform3/src/Domain/Reviewer/CommandHandler/UpdateIsAllowedToReviewHandler.php @@ -0,0 +1,64 @@ +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/demoextendsymfonyform3/src/Domain/Reviewer/Exception/CannotCreateReviewerException.php b/demoextendsymfonyform3/src/Domain/Reviewer/Exception/CannotCreateReviewerException.php new file mode 100644 index 00000000..b8dddf4f --- /dev/null +++ b/demoextendsymfonyform3/src/Domain/Reviewer/Exception/CannotCreateReviewerException.php @@ -0,0 +1,15 @@ +customerId = null !== $customerId ? new CustomerId((int) $customerId) : null; + } + + /** + * @return CustomerId|null + */ + public function getCustomerId() + { + return $this->customerId; + } +} diff --git a/demoextendsymfonyform3/src/Domain/Reviewer/QueryHandler/GetReviewerSettingsForFormHandler.php b/demoextendsymfonyform3/src/Domain/Reviewer/QueryHandler/GetReviewerSettingsForFormHandler.php new file mode 100644 index 00000000..28496f6e --- /dev/null +++ b/demoextendsymfonyform3/src/Domain/Reviewer/QueryHandler/GetReviewerSettingsForFormHandler.php @@ -0,0 +1,45 @@ +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/demoextendsymfonyform3/src/Domain/Reviewer/QueryResult/ReviewerSettingsForForm.php b/demoextendsymfonyform3/src/Domain/Reviewer/QueryResult/ReviewerSettingsForForm.php new file mode 100644 index 00000000..4c1dcc6b --- /dev/null +++ b/demoextendsymfonyform3/src/Domain/Reviewer/QueryResult/ReviewerSettingsForForm.php @@ -0,0 +1,38 @@ +isAllowedForReview = $isAllowedForReview; + } + + /** + * @return bool + */ + public function isAllowedForReview() + { + return $this->isAllowedForReview; + } +} diff --git a/demoextendsymfonyform3/src/Entity/Reviewer.php b/demoextendsymfonyform3/src/Entity/Reviewer.php new file mode 100755 index 00000000..b33ba505 --- /dev/null +++ b/demoextendsymfonyform3/src/Entity/Reviewer.php @@ -0,0 +1,38 @@ + 'democqrshooksusage_reviewer', + 'primary' => 'id_reviewer', + 'fields' => [ + 'id_customer' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'], + 'is_allowed_for_review' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'], + ], + ]; +} diff --git a/demoextendsymfonyform3/src/Repository/ReviewerRepository.php b/demoextendsymfonyform3/src/Repository/ReviewerRepository.php new file mode 100644 index 00000000..bc91e20f --- /dev/null +++ b/demoextendsymfonyform3/src/Repository/ReviewerRepository.php @@ -0,0 +1,81 @@ +connection = $connection; + $this->dbPrefix = $dbPrefix; + } + + /** + * Finds customer id if such exists. + * + * @param int $customerId + * + * @return int + */ + public function findIdByCustomer($customerId) + { + $queryBuilder = $this->connection->createQueryBuilder(); + + $queryBuilder + ->select('`id_reviewer`') + ->from($this->dbPrefix . 'democqrshooksusage_reviewer') + ->where('`id_customer` = :customer_id') + ; + + $queryBuilder->setParameter('customer_id', $customerId); + + return (int) $queryBuilder->execute()->fetch(PDO::FETCH_COLUMN); + } + + /** + * Gets allowed to review status by customer. + * + * @param int $customerId + * + * @return bool + */ + public function getIsAllowedToReviewStatus($customerId) + { + $queryBuilder = $this->connection->createQueryBuilder(); + + $queryBuilder + ->select('`is_allowed_for_review`') + ->from($this->dbPrefix . 'democqrshooksusage_reviewer') + ->where('`id_customer` = :customer_id') + ; + + $queryBuilder->setParameter('customer_id', $customerId); + + return (bool) $queryBuilder->execute()->fetch(PDO::FETCH_COLUMN); + } +}