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

ProductTaxonController::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\RedirectHandlerInterface;
15
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
16
use Sylius\Component\Core\Model\ProductTaxonInterface;
17
use Sylius\Component\Resource\ResourceActions;
18
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\RedirectResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Exception\HttpException;
24
25
/**
26
 * @author Anna Walasek <[email protected]>
27
 */
28
class ProductTaxonController extends ResourceController
29
{
30
    /**
31
     * @param Request $request
32
     *
33
     * @return Response
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use JsonResponse.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
34
     */
35
    public function updatePositionsAction(Request $request)
36
    {
37
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
38
        $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
39
        $productTaxons = $request->get('productTaxons');
40
41
        if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid('update-product-taxon-position', $request->request->get('_csrf_token'))) {
42
            throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
43
        }
44
45
        if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && null !== $productTaxons) {
46
            foreach ($productTaxons as $productTaxon) {
47
                if (!is_numeric($productTaxon['position'])) {
48
                    throw new HttpException(
49
                        Response::HTTP_NOT_ACCEPTABLE,
50
                        sprintf('The productTaxon position "%s" is invalid.', $productTaxon['position'])
51
                    );
52
                }
53
54
                /** @var ProductTaxonInterface $productTaxon */
55
                $productTaxonFromBase = $this->repository->findOneBy(['id' => $productTaxon['id']]);
56
                $productTaxonFromBase->setPosition($productTaxon['position']);
57
                $this->manager->flush();
58
            }
59
        }
60
61
        return new JsonResponse();
62
    }
63
}
64