|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\CoreBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
|
15
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
|
16
|
|
|
use Sylius\Component\Resource\ResourceActions; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Grzegorz Sadowski <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class ProductVariantController extends ResourceController |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @param Request $request |
|
29
|
|
|
* |
|
30
|
|
|
* @return JsonResponse |
|
31
|
|
|
* |
|
32
|
|
|
* @throws HttpException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function updatePositionsAction(Request $request) |
|
35
|
|
|
{ |
|
36
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
|
37
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::UPDATE); |
|
38
|
|
|
$productVariantsToUpdate = $request->get('productVariants'); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid('update-product-variant-position', $request->request->get('_csrf_token'))) { |
|
41
|
|
|
throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && null !== $productVariantsToUpdate) { |
|
45
|
|
|
foreach ($productVariantsToUpdate as $productVariantToUpdate) { |
|
|
|
|
|
|
46
|
|
|
if (!is_numeric($productVariantToUpdate['position'])) { |
|
47
|
|
|
throw new HttpException( |
|
48
|
|
|
Response::HTTP_NOT_ACCEPTABLE, |
|
49
|
|
|
sprintf('The product variant position "%s" is invalid.', $productVariantToUpdate['position']) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** @var ProductVariantInterface $productVariant */ |
|
54
|
|
|
$productVariant = $this->repository->findOneBy(['id' => $productVariantToUpdate['id']]); |
|
55
|
|
|
$productVariant->setPosition($productVariantToUpdate['position']); |
|
56
|
|
|
$this->manager->flush(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return new JsonResponse(); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.