Skip to content

Commit

Permalink
[UPMERGE] 1.12 -> 1.13 (#15520)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Nov 13, 2023
2 parents 1e168c7 + 8a5ccd9 commit d75e146
Show file tree
Hide file tree
Showing 21 changed files with 119 additions and 53 deletions.
Expand Up @@ -79,7 +79,7 @@ private function handleExpiredPasswordRequest(Request $request): RedirectRespons
->add('error', 'sylius.admin.password_reset.token_expired')
;

$attributes = $request->attributes->get('_sylius');
$attributes = $request->attributes->get('_sylius', []);
$redirect = $attributes['redirect'] ?? 'sylius_admin_login';

if (is_array($redirect)) {
Expand Down
Expand Up @@ -67,7 +67,7 @@ public function __invoke(Request $request): Response
->add('success', 'sylius.admin.request_reset_password.success')
;

$options = $request->attributes->get('_sylius');
$options = $request->attributes->get('_sylius', []);
$redirectRoute = $options['redirect'] ?? 'sylius_admin_login';

if (is_array($redirectRoute)) {
Expand Down
Expand Up @@ -63,7 +63,7 @@ public function __invoke(Request $request, string $token): Response
->add('success', 'sylius.admin.password_reset.success')
;

$attributes = $request->attributes->get('_sylius');
$attributes = $request->attributes->get('_sylius', []);
$redirect = $attributes['redirect'] ?? 'sylius_admin_login';

if (is_array($redirect)) {
Expand Down
6 changes: 4 additions & 2 deletions src/Sylius/Bundle/AdminBundle/Action/RemoveAvatarAction.php
Expand Up @@ -34,9 +34,11 @@ public function __construct(

public function __invoke(Request $request): Response
{
$userId = $request->attributes->get('id');
$userId = $request->attributes->get('id', '');

if (!$this->csrfTokenManager->isTokenValid(new CsrfToken($userId, (string) $request->query->get('_csrf_token')))) {
if (!$this->csrfTokenManager->isTokenValid(
new CsrfToken($userId, (string) $request->query->get('_csrf_token', '')),
)) {
throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
}

Expand Down
Expand Up @@ -49,9 +49,11 @@ public function __construct(

public function __invoke(Request $request): Response
{
$orderId = $request->attributes->get('id');
$orderId = $request->attributes->get('id', '');

if (!$this->csrfTokenManager->isTokenValid(new CsrfToken($orderId, (string) $request->query->get('_csrf_token')))) {
if (!$this->csrfTokenManager->isTokenValid(
new CsrfToken($orderId, (string) $request->query->get('_csrf_token', '')),
)) {
throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
}

Expand Down
Expand Up @@ -49,9 +49,11 @@ public function __construct(

public function __invoke(Request $request): Response
{
$shipmentId = $request->attributes->get('id');
$shipmentId = $request->attributes->get('id', '');

if (!$this->csrfTokenManager->isTokenValid(new CsrfToken($shipmentId, (string) $request->query->get('_csrf_token')))) {
if (!$this->csrfTokenManager->isTokenValid(
new CsrfToken($shipmentId, (string) $request->query->get('_csrf_token', '')),
)) {
throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
}

Expand Down
Expand Up @@ -31,8 +31,13 @@ public function __construct(private CatalogPromotionRemovalProcessorInterface $c

public function __invoke(Request $request): Response
{
$catalogPromotionCode = $request->attributes->get('code');
if (null === $catalogPromotionCode) {
throw new NotFoundHttpException('The catalog promotion has not been found');
}

try {
$this->catalogPromotionRemovalProcessor->removeCatalogPromotion($request->attributes->get('code'));
$this->catalogPromotionRemovalProcessor->removeCatalogPromotion($catalogPromotionCode);

/** @var Session $session */
$session = $request->getSession();
Expand Down
Expand Up @@ -44,17 +44,12 @@ public function onKernelRequest(RequestEvent $event): void
}

$requestAttributes = $eventRequest->attributes;
$originalRoute = $requestAttributes->get('_route');

if (!$this->isIndexResourceRoute($originalRoute)) {
return;
}

if (!$this->isAdminSection($requestAttributes->get('_sylius', []))) {
return;
}

if (null === $requestAttributes->get('_controller')) {
if (
null === $requestAttributes->get('_controller') ||
!$this->isIndexResourceRoute($requestAttributes->get('_route', '')) ||
!$this->isAdminSection($requestAttributes->get('_sylius', []))
) {
return;
}

Expand All @@ -80,6 +75,6 @@ private function isIndexResourceRoute(string $route): bool

private function isAdminSection(array $syliusParameters): bool
{
return array_key_exists('section', $syliusParameters) && 'admin' === $syliusParameters['section'];
return isset($syliusParameters['section']) && 'admin' === $syliusParameters['section'];
}
}
Expand Up @@ -69,7 +69,7 @@ public function onResourceDelete(ExceptionEvent $event): void

$eventRequest = $event->getRequest();
$requestAttributes = $eventRequest->attributes;
$originalRoute = $requestAttributes->get('_route');
$originalRoute = $requestAttributes->get('_route', '');

if (!$this->isMethodDelete($eventRequest) ||
!$this->isSyliusRoute($originalRoute) ||
Expand Down
Expand Up @@ -81,7 +81,7 @@ public function it_sends_reset_password_request_to_message_bus(

$flashBag->add('success', 'sylius.admin.request_reset_password.success')->shouldBeCalled();

$attributesBag->get('_sylius')->shouldBeCalled()->willReturn([
$attributesBag->get('_sylius', [])->shouldBeCalled()->willReturn([
'redirect' => 'my_custom_route',
]);
$request->attributes = $attributesBag->getWrappedObject();
Expand Down Expand Up @@ -120,7 +120,7 @@ public function it_is_able_to_send_reset_password_request_when_sylius_redirect_p
$parameters = [
'my_parameter' => 'my_value',
];
$attributesBag->get('_sylius')->shouldBeCalled()->willReturn([
$attributesBag->get('_sylius', [])->shouldBeCalled()->willReturn([
'redirect' => [
'route' => $route,
'params' => $parameters,
Expand Down Expand Up @@ -158,7 +158,7 @@ public function it_redirects_to_default_route_if_custom_one_is_not_defined(

$flashBag->add('success', 'sylius.admin.request_reset_password.success')->shouldBeCalled();

$attributesBag->get('_sylius')->shouldBeCalled()->willReturn(null);
$attributesBag->get('_sylius', [])->shouldBeCalled()->willReturn([]);
$request->attributes = $attributesBag->getWrappedObject();

$router->generate('sylius_admin_login')->shouldBeCalled()->willReturn('/login');
Expand Down
Expand Up @@ -43,7 +43,7 @@ function it_adds_filter_to_filter_storage(
$event->isMainRequest()->willReturn(true);
$request->getRequestFormat()->willReturn('html');

$attributes->get('_route')->willReturn('sylius_admin_product_index');
$attributes->get('_route', '')->willReturn('sylius_admin_product_index');
$attributes->get('_sylius', [])->willReturn(['section' => 'admin']);
$attributes->get('_controller')->willReturn('Sylius\Bundle\AdminBundle\Controller\ProductController::indexAction');
$request->attributes = $attributes;
Expand Down Expand Up @@ -82,7 +82,7 @@ function it_does_not_add_filter_to_filter_storage_if_request_format_is_not_html(
$event->isMainRequest()->willReturn(true);
$request->getRequestFormat()->willReturn('json');

$attributes->get('_route')->willReturn('sylius_admin_product_index');
$attributes->get('_route', '')->willReturn('sylius_admin_product_index');
$attributes->get('_sylius', [])->willReturn(['section' => 'admin']);
$attributes->get('_controller')->willReturn('Sylius\Bundle\AdminBundle\Controller\ProductController::indexAction');
$request->attributes = $attributes;
Expand All @@ -109,7 +109,7 @@ function it_does_not_add_filter_to_filter_storage_if_it_is_not_an_admin_section(
$event->isMainRequest()->willReturn(true);
$request->getRequestFormat()->willReturn('json');

$attributes->get('_route')->willReturn('sylius_admin_product_index');
$attributes->get('_route', '')->willReturn('sylius_admin_product_index');
$attributes->get('_sylius', [])->willReturn(['section' => 'shop']);
$attributes->get('_controller')->willReturn('Sylius\Bundle\AdminBundle\Controller\ProductController::indexAction');
$request->attributes = $attributes;
Expand All @@ -136,7 +136,7 @@ function it_does_not_add_filter_to_filter_storage_if_controller_is_null(
$event->isMainRequest()->willReturn(true);
$request->getRequestFormat()->willReturn('json');

$attributes->get('_route')->willReturn('sylius_admin_product_index');
$attributes->get('_route', '')->willReturn('sylius_admin_product_index');
$attributes->get('_sylius', [])->willReturn(['section' => 'shop']);
$attributes->get('_controller')->willReturn(null);
$request->attributes = $attributes;
Expand All @@ -153,6 +153,33 @@ function it_does_not_add_filter_to_filter_storage_if_controller_is_null(
$this->onKernelRequest($event);
}

function it_does_not_add_filter_to_filter_storage_if_route_is_missing(
RequestEvent $event,
Request $request,
ParameterBag $attributes,
ParameterBag $query,
FilterStorageInterface $filterStorage,
): void {
$event->isMainRequest()->willReturn(true);
$request->getRequestFormat()->willReturn('json');

$attributes->get('_route', '')->willReturn('');
$attributes->get('_sylius', [])->willReturn(['section' => 'shop']);
$attributes->get('_controller')->willReturn('Sylius\Bundle\AdminBundle\Controller\ProductController::indexAction');
$request->attributes = $attributes;

$query->all()->willReturn(['filter' => 'foo']);
$request->query = $query;

$filterStorage->all()->willReturn([]);

$event->getRequest()->willReturn($request);

$filterStorage->set(Argument::any())->shouldNotBeCalled();

$this->onKernelRequest($event);
}

function it_does_not_add_filter_to_filter_storage_if_it_is_not_a_index_resource_route(
RequestEvent $event,
Request $request,
Expand All @@ -163,7 +190,7 @@ function it_does_not_add_filter_to_filter_storage_if_it_is_not_a_index_resource_
$event->isMainRequest()->willReturn(true);
$request->getRequestFormat()->willReturn('json');

$attributes->get('_route')->willReturn('sylius_admin_product_update');
$attributes->get('_route', '')->willReturn('sylius_admin_product_update');
$attributes->get('_sylius', [])->willReturn(['section' => 'shop']);
$attributes->get('_controller')->willReturn('Sylius\Bundle\AdminBundle\Controller\ProductController::indexAction');
$request->attributes = $attributes;
Expand Down
Expand Up @@ -33,9 +33,11 @@ public function __invoke(Request $request): Response
{
$orderItemId = $request->attributes->get('itemId');
$tokenValue = $request->attributes->get('tokenValue');
if (null === $orderItemId || null === $tokenValue) {
throw new OrderItemNotFoundException();
}

$orderItem = $this->orderItemRepository->findOneByIdAndCartTokenValue($orderItemId, $tokenValue);

if ($orderItem === null) {
throw new OrderItemNotFoundException();
}
Expand Down
Expand Up @@ -31,11 +31,14 @@ public function __construct(

public function __invoke(Request $request): JsonResponse
{
$paymentId = $request->attributes->get('paymentId');
$tokenValue = $request->attributes->get('tokenValue');
if (null === $paymentId || null === $tokenValue) {
throw new PaymentNotFoundException();
}

/** @var PaymentInterface|null $payment */
$payment = $this->paymentRepository->findOneByOrderToken(
$request->attributes->get('paymentId'),
$request->attributes->get('tokenValue'),
);
$payment = $this->paymentRepository->findOneByOrderToken($paymentId, $tokenValue);

if ($payment === null) {
throw new PaymentNotFoundException();
Expand Down
Expand Up @@ -30,8 +30,13 @@ public function __construct(

public function __invoke(Request $request): Response
{
$catalogPromotionCode = $request->attributes->get('code');
if (null === $catalogPromotionCode) {
return new JsonResponse(status: Response::HTTP_NOT_FOUND);
}

try {
$this->catalogPromotionRemovalProcessor->removeCatalogPromotion($request->attributes->get('code'));
$this->catalogPromotionRemovalProcessor->removeCatalogPromotion($catalogPromotionCode);

return new Response(status: Response::HTTP_ACCEPTED);
} catch (CatalogPromotionNotFoundException) {
Expand Down
Expand Up @@ -32,13 +32,19 @@ public function __construct(
public function handleCheckoutRedirect(ResourceControllerEvent $resourceControllerEvent): void
{
$request = $this->requestStack->getCurrentRequest();
if (!$this->requestMatcher->matches($request) || isset($request->attributes->get('_sylius')['redirect'])) {
if (
null === $request ||
!$this->requestMatcher->matches($request) ||
isset($request->attributes->get('_sylius', [])['redirect'])
) {
return;
}

$order = $resourceControllerEvent->getSubject();
Assert::isInstanceOf($order, OrderInterface::class);

$resourceControllerEvent->setResponse(new RedirectResponse($this->checkoutStateUrlGenerator->generateForOrderCheckoutState($order)));
$resourceControllerEvent->setResponse(
new RedirectResponse($this->checkoutStateUrlGenerator->generateForOrderCheckoutState($order)),
);
}
}
Expand Up @@ -97,7 +97,7 @@ public function requestAction(Request $request): Response

private function getSyliusAttribute(Request $request, string $attributeName, ?string $default): ?string
{
$attributes = $request->attributes->get('_sylius');
$attributes = $request->attributes->get('_sylius', []);

return $attributes[$attributeName] ?? $default;
}
Expand Down

0 comments on commit d75e146

Please sign in to comment.