Skip to content

Commit 06971e1

Browse files
committed
Refacto CommonController based on PrestaShopAdminController, introduce new type of service locator
1 parent 2f7b0f4 commit 06971e1

File tree

10 files changed

+183
-86
lines changed

10 files changed

+183
-86
lines changed

phpstan-disallowed-calls.neon

+1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ parameters:
285285
disallowIn:
286286
- src/PrestaShopBundle/Controller/*
287287
allowIn:
288+
- src/PrestaShopBundle/Controller/Admin/CommonController.php
288289
- src/PrestaShopBundle/Controller/Admin/FrameworkBundleAdminController.php
289290
- src/PrestaShopBundle/Controller/Admin/PrestaShopAdminController.php
290291
- src/PrestaShopBundle/Controller/Admin/Improve/Modules/ModuleAbstractController.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/OSL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to [email protected] so we can send you a copy immediately.
15+
*
16+
* DISCLAIMER
17+
*
18+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19+
* versions in the future. If you wish to customize PrestaShop for your
20+
* needs please refer to https://devdocs.prestashop.com/ for more information.
21+
*
22+
* @author PrestaShop SA and Contributors <[email protected]>
23+
* @copyright Since 2007 PrestaShop SA and Contributors
24+
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25+
*/
26+
27+
namespace PrestaShop\PrestaShop\Core\Grid\Definition\Factory;
28+
29+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
30+
use Symfony\Contracts\Service\ServiceProviderInterface;
31+
32+
/**
33+
* This is a service locator that allows fetching grid definition factories via their index.
34+
*/
35+
class GridDefinitionFactoryProvider
36+
{
37+
public function __construct(
38+
#[AutowireLocator('core.grid_definition_factory')]
39+
protected ServiceProviderInterface $factories
40+
) {
41+
}
42+
43+
public function getFactory(string $name): GridDefinitionFactoryInterface
44+
{
45+
return $this->factories->get($name);
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/OSL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to [email protected] so we can send you a copy immediately.
15+
*
16+
* DISCLAIMER
17+
*
18+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19+
* versions in the future. If you wish to customize PrestaShop for your
20+
* needs please refer to https://devdocs.prestashop.com/ for more information.
21+
*
22+
* @author PrestaShop SA and Contributors <[email protected]>
23+
* @copyright Since 2007 PrestaShop SA and Contributors
24+
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25+
*/
26+
27+
namespace PrestaShop\PrestaShop\Core\Grid\Position;
28+
29+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
30+
use Symfony\Contracts\Service\ServiceProviderInterface;
31+
32+
/**
33+
* This is a service locator that allows fetching position definitions via their index.
34+
*/
35+
class PositionDefinitionProvider
36+
{
37+
public function __construct(
38+
#[AutowireLocator('core.grid_position_definition')]
39+
protected ServiceProviderInterface $positionDefinitions
40+
) {
41+
}
42+
43+
public function getPositionDefinition(string $name): PositionDefinitionInterface
44+
{
45+
return $this->positionDefinitions->get($name);
46+
}
47+
}

src/PrestaShopBundle/Controller/Admin/CommonController.php

+51-78
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,20 @@
2626

2727
namespace PrestaShopBundle\Controller\Admin;
2828

29-
use Context;
3029
use PrestaShop\PrestaShop\Adapter\Tools;
3130
use PrestaShop\PrestaShop\Core\Domain\Notification\Command\UpdateEmployeeNotificationLastElementCommand;
3231
use PrestaShop\PrestaShop\Core\Domain\Notification\Query\GetNotificationLastElements;
3332
use PrestaShop\PrestaShop\Core\Domain\Notification\QueryResult\NotificationsResults;
3433
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\AbstractGridDefinitionFactory;
3534
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\FilterableGridDefinitionFactoryInterface;
36-
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\GridDefinitionFactoryInterface;
35+
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\GridDefinitionFactoryProvider;
3736
use PrestaShop\PrestaShop\Core\Grid\Position\Exception\PositionUpdateException;
38-
use PrestaShop\PrestaShop\Core\Grid\Position\GridPositionUpdaterInterface;
39-
use PrestaShop\PrestaShop\Core\Grid\Position\PositionDefinitionInterface;
40-
use PrestaShop\PrestaShop\Core\Grid\Position\PositionUpdateFactoryInterface;
37+
use PrestaShop\PrestaShop\Core\Grid\Position\PositionDefinitionProvider;
4138
use PrestaShop\PrestaShop\Core\Kpi\Row\KpiRowInterface;
42-
use PrestaShopBundle\Entity\Employee\Employee;
39+
use PrestaShop\PrestaShop\Core\Kpi\Row\KpiRowPresenter;
40+
use PrestaShopBundle\Entity\Repository\AdminFilterRepository;
4341
use PrestaShopBundle\Security\Attribute\AdminSecurity;
4442
use PrestaShopBundle\Service\Grid\ControllerResponseBuilder;
45-
use PrestaShopBundle\Service\Grid\ResponseBuilder;
4643
use ReflectionClass;
4744
use Symfony\Component\HttpFoundation\JsonResponse;
4845
use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -52,8 +49,15 @@
5249
/**
5350
* Admin controller for the common actions across the whole admin interface.
5451
*/
55-
class CommonController extends FrameworkBundleAdminController
52+
class CommonController extends PrestaShopAdminController
5653
{
54+
public static function getSubscribedServices(): array
55+
{
56+
return parent::getSubscribedServices() + [
57+
ControllerResponseBuilder::class => ControllerResponseBuilder::class,
58+
];
59+
}
60+
5761
/**
5862
* Get a summary of recent events on the shop.
5963
* This includes:
@@ -63,11 +67,10 @@ class CommonController extends FrameworkBundleAdminController
6367
*
6468
* @return JsonResponse
6569
*/
66-
public function notificationsAction()
70+
public function notificationsAction(): JsonResponse
6771
{
68-
$employeeId = Context::getContext()->employee->id;
6972
/** @var NotificationsResults $elements */
70-
$elements = $this->getQueryBus()->handle(new GetNotificationLastElements($employeeId));
73+
$elements = $this->dispatchQuery(new GetNotificationLastElements($this->getEmployeeContext()->getEmployee()->getId()));
7174

7275
return new JsonResponse($elements->getNotificationsResultsForJS());
7376
}
@@ -79,10 +82,9 @@ public function notificationsAction()
7982
*
8083
* @return JsonResponse
8184
*/
82-
public function notificationsAckAction(Request $request)
85+
public function notificationsAckAction(Request $request): JsonResponse
8386
{
84-
$type = $request->request->get('type');
85-
$this->getCommandBus()->handle(new UpdateEmployeeNotificationLastElementCommand($type));
87+
$this->dispatchCommand(new UpdateEmployeeNotificationLastElementCommand($request->request->get('type')));
8688

8789
return new JsonResponse(true);
8890
}
@@ -112,7 +114,7 @@ public function notificationsAckAction(Request $request)
112114
*
113115
* @return Response
114116
*/
115-
public function paginationAction(Request $request, $limit = 10, $offset = 0, $total = 0, $view = 'full', $prefix = ''): Response
117+
public function paginationAction(Request $request, ?int $limit = 10, ?int $offset = 0, ?int $total = 0, string $view = 'full', string $prefix = ''): Response
116118
{
117119
$offsetParam = empty($prefix) ? 'offset' : sprintf('%s[offset]', $prefix);
118120
$limitParam = empty($prefix) ? 'limit' : sprintf('%s[limit]', $prefix);
@@ -210,10 +212,12 @@ public function paginationAction(Request $request, $limit = 10, $offset = 0, $to
210212
*
211213
* @return Response
212214
*/
213-
public function renderSidebarAction($url, $title = '', $footer = '')
214-
{
215-
$tools = $this->get(Tools::class);
216-
215+
public function renderSidebarAction(
216+
Tools $tools,
217+
string $url,
218+
string $title = '',
219+
string $footer = '',
220+
): Response {
217221
return $this->render('@PrestaShop/Admin/Common/_partials/_sidebar.html.twig', [
218222
'footer' => $tools->purifyHTML($footer),
219223
'title' => $title,
@@ -228,12 +232,12 @@ public function renderSidebarAction($url, $title = '', $footer = '')
228232
*
229233
* @return Response
230234
*/
231-
public function renderKpiRowAction(KpiRowInterface $kpiRow)
232-
{
233-
$presenter = $this->get('prestashop.core.kpi_row.presenter');
234-
235+
public function renderKpiRowAction(
236+
KpiRowInterface $kpiRow,
237+
KpiRowPresenter $kpiRowPresenter,
238+
): Response {
235239
return $this->render('@PrestaShop/Admin/Common/Kpi/kpi_row.html.twig', [
236-
'kpiRow' => $presenter->present($kpiRow),
240+
'kpiRow' => $kpiRowPresenter->present($kpiRow),
237241
]);
238242
}
239243

@@ -246,11 +250,14 @@ public function renderKpiRowAction(KpiRowInterface $kpiRow)
246250
*
247251
* @throws \Doctrine\ORM\OptimisticLockException
248252
*/
249-
public function resetSearchAction($controller = '', $action = '', $filterId = '')
250-
{
251-
$adminFiltersRepository = $this->get('prestashop.core.admin.admin_filter.repository');
252-
$employeeId = $this->getUser() instanceof Employee ? $this->getUser()->getId() : 0;
253-
$shopId = $this->getContext()->shop->id;
253+
public function resetSearchAction(
254+
AdminFilterRepository $adminFiltersRepository,
255+
string $controller = '',
256+
string $action = '',
257+
string $filterId = '',
258+
): JsonResponse {
259+
$employeeId = $this->getEmployeeContext()->getEmployee()->getId();
260+
$shopId = $this->getShopContext()->getId();
254261

255262
// for compatibility when $controller and $action are used
256263
if (!empty($controller) && !empty($action)) {
@@ -270,33 +277,6 @@ public function resetSearchAction($controller = '', $action = '', $filterId = ''
270277
return new JsonResponse();
271278
}
272279

273-
/**
274-
* Specific action to render a specific field twice.
275-
*
276-
* @param string $formName the form name
277-
* @param string $formType the form type FQCN
278-
* @param string $fieldName the field name
279-
* @param array $fieldData the field data
280-
*
281-
* @return Response
282-
*/
283-
public function renderFieldAction($formName, $formType, $fieldName, $fieldData)
284-
{
285-
$formData = [
286-
$formName => [
287-
$fieldName => $fieldData,
288-
],
289-
];
290-
291-
$form = $this->createFormBuilder($formData);
292-
$form->add($formName, $formType);
293-
294-
return $this->render('@PrestaShop/Admin/Common/_partials/_form_field.html.twig', [
295-
'form' => $form->getForm()->get($formName)->get($fieldName)->createView(),
296-
'formId' => $formName . '_' . $fieldName . '_rendered',
297-
]);
298-
}
299-
300280
/**
301281
* Process Grid search.
302282
*
@@ -309,16 +289,15 @@ public function renderFieldAction($formName, $formType, $fieldName, $fieldData)
309289
*/
310290
#[AdminSecurity("is_granted('read', request.get('_legacy_controller'))")]
311291
public function searchGridAction(
292+
GridDefinitionFactoryProvider $gridDefinitionFactoryCollection,
312293
Request $request,
313-
$gridDefinitionFactoryServiceId,
314-
$redirectRoute,
294+
string $gridDefinitionFactoryServiceId,
295+
string $redirectRoute,
315296
array $redirectQueryParamsToKeep = []
316297
) {
317-
/** @var GridDefinitionFactoryInterface $definitionFactory */
318-
$definitionFactory = $this->get($gridDefinitionFactoryServiceId);
298+
$definitionFactory = $gridDefinitionFactoryCollection->getFactory($gridDefinitionFactoryServiceId);
319299

320300
$filterId = null;
321-
322301
if ($definitionFactory instanceof FilterableGridDefinitionFactoryInterface) {
323302
$filterId = $definitionFactory->getFilterId();
324303
} elseif ($definitionFactory instanceof AbstractGridDefinitionFactory) {
@@ -332,21 +311,18 @@ public function searchGridAction(
332311
}
333312

334313
if (null !== $filterId) {
335-
/** @var ResponseBuilder $responseBuilder */
336-
$responseBuilder = $this->get('prestashop.bundle.grid.response_builder');
337-
338-
return $responseBuilder->buildSearchResponse(
314+
return $this->buildSearchResponse(
339315
$definitionFactory,
340316
$request,
341317
$filterId,
342318
$redirectRoute,
343-
$redirectQueryParamsToKeep
319+
$redirectQueryParamsToKeep,
344320
);
345321
}
346322

347323
// Legacy grid definition which use controller/action as filter keys (and no scope for parameters)
348324
/** @var ControllerResponseBuilder $controllerResponseBuilder */
349-
$controllerResponseBuilder = $this->get('prestashop.bundle.grid.controller_response_builder');
325+
$controllerResponseBuilder = $this->container->get(ControllerResponseBuilder::class);
350326

351327
return $controllerResponseBuilder->buildSearchResponse(
352328
$definitionFactory,
@@ -362,24 +338,21 @@ public function searchGridAction(
362338
* @return RedirectResponse
363339
*/
364340
#[AdminSecurity("is_granted('update', request.get('_legacy_controller'))")]
365-
public function updatePositionAction(Request $request): RedirectResponse
366-
{
341+
public function updatePositionAction(
342+
Request $request,
343+
PositionDefinitionProvider $positionDefinitionProvider,
344+
): RedirectResponse {
367345
$positionsData = [
368346
'positions' => $request->request->all('positions'),
369347
];
370348

371-
/** @var PositionDefinitionInterface $positionDefinition */
372-
$positionDefinition = $this->get($request->attributes->get('position_definition'));
373-
$positionUpdateFactory = $this->get(PositionUpdateFactoryInterface::class);
374-
349+
$positionDefinition = $positionDefinitionProvider->getPositionDefinition($request->attributes->get('position_definition'));
375350
try {
376-
$positionUpdate = $positionUpdateFactory->buildPositionUpdate($positionsData, $positionDefinition);
377-
$updater = $this->get(GridPositionUpdaterInterface::class);
378-
$updater->update($positionUpdate);
379-
$this->addFlash('success', $this->trans('Successful update', 'Admin.Notifications.Success'));
351+
$this->updateGridPosition($positionDefinition, $positionsData);
352+
$this->addFlash('success', $this->trans('Successful update', [], 'Admin.Notifications.Success'));
380353
} catch (PositionUpdateException $e) {
381354
$errors = [$e->toArray()];
382-
$this->flashErrors($errors);
355+
$this->addFlashErrors($errors);
383356
}
384357

385358
return $this->redirectToRoute($request->attributes->get('redirect_route'));

src/PrestaShopBundle/Controller/Admin/PrestaShopAdminController.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use PrestaShop\PrestaShop\Core\Grid\GridInterface;
4545
use PrestaShop\PrestaShop\Core\Grid\Position\GridPositionUpdaterInterface;
4646
use PrestaShop\PrestaShop\Core\Grid\Position\PositionDefinition;
47+
use PrestaShop\PrestaShop\Core\Grid\Position\PositionDefinitionInterface;
4748
use PrestaShop\PrestaShop\Core\Grid\Position\PositionUpdateFactoryInterface;
4849
use PrestaShop\PrestaShop\Core\Grid\Presenter\GridPresenterInterface;
4950
use PrestaShop\PrestaShop\Core\Help\Documentation;
@@ -324,15 +325,15 @@ protected function buildSearchResponse(
324325
/**
325326
* Updates the position of a grid based on the provided PositionDefinition and provided data.
326327
*
327-
* @param PositionDefinition $positionDefinition
328+
* @param PositionDefinitionInterface $positionDefinition
328329
* @param array $positionsData
329330
*
330331
* @return void
331332
*
332333
* @throws ContainerExceptionInterface
333334
* @throws NotFoundExceptionInterface
334335
*/
335-
protected function updateGridPosition(PositionDefinition $positionDefinition, array $positionsData): void
336+
protected function updateGridPosition(PositionDefinitionInterface $positionDefinition, array $positionsData): void
336337
{
337338
$positionUpdateFactory = $this->container->get(PositionUpdateFactoryInterface::class);
338339
$positionUpdate = $positionUpdateFactory->buildPositionUpdate($positionsData, $positionDefinition);

src/PrestaShopBundle/Resources/config/services/bundle/grid.yml

+9-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ services:
1919
package: PrestaShop\PrestaShop
2020
version: 9.0
2121

22-
prestashop.bundle.grid.controller_response_builder:
23-
class: PrestaShopBundle\Service\Grid\ControllerResponseBuilder
22+
PrestaShopBundle\Service\Grid\ControllerResponseBuilder:
23+
public: false
2424
arguments:
2525
- '@prestashop.core.grid.filter.form_factory'
2626
- '@router'
27+
28+
prestashop.bundle.grid.controller_response_builder:
29+
alias: PrestaShopBundle\Service\Grid\ControllerResponseBuilder
30+
public: true
31+
deprecated:
32+
package: PrestaShop\PrestaShop
33+
version: 9.0

0 commit comments

Comments
 (0)