Skip to content

Commit

Permalink
remove usage of symfony internal method
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed Nov 29, 2021
1 parent 28a02a6 commit d2c39e4
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public function __construct(
public function __invoke(Request $request): JsonResponse
{
/** @var PaymentInterface|null $payment */
$payment = $this->paymentRepository->findOneByOrderToken($request->get('paymentId'), $request->get('id'));
$payment = $this->paymentRepository->findOneByOrderToken(
$request->attributes->get('paymentId'),
$request->attributes->get('id')
);

Assert::notNull($payment);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __invoke(Request $request): ImageInterface
$image->setFile($file);

/** @var string $ownerIri */
$ownerIri = $request->get('owner');
$ownerIri = $request->request->get('owner');
Assert::notEmpty($ownerIri);

/** @var ResourceInterface|AdminUserInterface $owner */
Expand Down
4 changes: 2 additions & 2 deletions src/Sylius/Bundle/CoreBundle/Command/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function ($value): string {
/** @var ConstraintViolationListInterface $errors */
$errors = $this->getContainer()->get('validator')->validate((string) $value, [new Email(), new NotBlank()]);
foreach ($errors as $error) {
throw new \DomainException($error->getMessage());
throw new \DomainException((string) $error->getMessage());
}

return $value;
Expand Down Expand Up @@ -191,7 +191,7 @@ function ($value): string {
/** @var ConstraintViolationListInterface $errors */
$errors = $this->getContainer()->get('validator')->validate($value, [new NotBlank()]);
foreach ($errors as $error) {
throw new \DomainException($error->getMessage());
throw new \DomainException((string) $error->getMessage());
}

return $value;
Expand Down
33 changes: 30 additions & 3 deletions src/Sylius/Bundle/CoreBundle/Controller/ProductTaxonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ class ProductTaxonController extends ResourceController
* @throws HttpException
*
* @deprecated This ajax action is deprecated and will be removed in Sylius 2.0 - use ProductTaxonController::updateProductTaxonsPositionsAction instead.
*
* @psalm-suppress DeprecatedMethod
*/
public function updatePositionsAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$this->isGrantedOr403($configuration, ResourceActions::UPDATE);
$productTaxons = $request->get('productTaxons');

$productTaxons = $this->getParameterFromRequest($request,'productTaxons');
$this->validateCsrfProtection($request, $configuration);

if ($this->shouldProductsPositionsBeUpdated($request, $productTaxons)) {
Expand All @@ -55,11 +56,14 @@ public function updatePositionsAction(Request $request): Response
return new JsonResponse();
}

/**
* @psalm-suppress DeprecatedMethod
*/
public function updateProductTaxonsPositionsAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$this->isGrantedOr403($configuration, ResourceActions::UPDATE);
$productTaxons = $request->get('productTaxons');
$productTaxons = $this->getParameterFromRequest($request,'productTaxons');

$this->validateCsrfProtection($request, $configuration);

Expand Down Expand Up @@ -103,4 +107,27 @@ private function updatePositions(string $position, int $id): void
$productTaxonFromBase = $this->repository->findOneBy(['id' => $id]);
$productTaxonFromBase->setPosition((int) $position);
}

/**
* @return mixed
*
* @deprecated This function will be removed in Sylius 2.0, since Symfony 5.4, use explicit input sources instead
* based on Symfony\Component\HttpFoundation\Request::get
*/
public function getParameterFromRequest(Request $request, string $key)
{
if ($request !== $result = $request->attributes->get($key, $request)) {
return $result;
}

if ($request->query->has($key)) {
return $request->query->all()[$key];
}

if ($request->request->has('key')) {
return $request->request->all()[$key];
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ class ProductVariantController extends ResourceController
{
/**
* @throws HttpException
* @psalm-suppress DeprecatedMethod
*/
public function updatePositionsAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$this->isGrantedOr403($configuration, ResourceActions::UPDATE);
$productVariantsToUpdate = $request->get('productVariants');
$productVariantsToUpdate = $this->getParameterFromRequest($request, 'productVariants');

if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid('update-product-variant-position', (string) $request->request->get('_csrf_token'))) {
throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
Expand All @@ -54,4 +55,27 @@ public function updatePositionsAction(Request $request): Response

return new JsonResponse();
}

/**
* @return mixed
*
* @deprecated This function will be removed in Sylius 2.0, since Symfony 5.4, use explicit input sources instead
* based on Symfony\Component\HttpFoundation\Request::get
*/
public function getParameterFromRequest(Request $request, string $key)
{
if ($request !== $result = $request->attributes->get($key, $request)) {
return $result;
}

if ($request->query->has($key)) {
return $request->query->all()[$key];
}

if ($request->request->has('key')) {
return $request->request->all()[$key];
}

return null;
}
}
28 changes: 27 additions & 1 deletion src/Sylius/Bundle/OrderBundle/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,17 @@ public function saveAction(Request $request): Response
);
}

/**
* @psalm-suppress DeprecatedMethod
*/
public function clearAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

$this->isGrantedOr403($configuration, ResourceActions::DELETE);
$resource = $this->getCurrentCart();

if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid((string) $resource->getId(), $request->get('_csrf_token'))) {
if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid((string) $resource->getId(), $this->getParameterFromRequest($request, '_csrf_token'))) {
throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
}

Expand Down Expand Up @@ -194,4 +197,27 @@ protected function getEventDispatcher(): EventDispatcherInterface
{
return $this->container->get('event_dispatcher');
}

/**
* @return mixed
*
* @deprecated This function will be removed in Sylius 2.0, since Symfony 5.4, use explicit input sources instead
* based on Symfony\Component\HttpFoundation\Request::get
*/
public function getParameterFromRequest(Request $request, string $key)
{
if ($request !== $result = $request->attributes->get($key, $request)) {
return $result;
}

if ($request->query->has($key)) {
return $request->query->all()[$key];
}

if ($request->request->has('key')) {
return $request->request->all()[$key];
}

return null;
}
}

0 comments on commit d2c39e4

Please sign in to comment.