Completed
Push — master ( 1c9f17...2589c5 )
by Kamil
21:28
created

ProductVariantController::updatePositionsAction()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 16
nc 4
nop 1
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');
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productVariantsToUpdate exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
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) {
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productVariantToUpdate exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
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