Completed
Push — core-test-reorganisation ( b9cfa9...ce9ad9 )
by Kamil
43:54 queued 22:56
created

ProductTaxonController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 37
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C updatePositionsAction() 0 29 7
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\ProductTaxonInterface;
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 Anna Walasek <[email protected]>
24
 */
25
class ProductTaxonController extends ResourceController
26
{
27
    /**
28
     * @param Request $request
29
     *
30
     * @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...
31
     */
32
    public function updatePositionsAction(Request $request)
33
    {
34
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
35
        $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
36
        $productTaxons = $request->get('productTaxons');
37
38
        if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid('update-product-taxon-position', $request->request->get('_csrf_token'))) {
39
            throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
40
        }
41
42
        if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && null !== $productTaxons) {
43
            /** @var ProductTaxonInterface $productTaxon */
44
            foreach ($productTaxons as $productTaxon) {
45
                if (!is_numeric($productTaxon['position'])) {
46
                    throw new HttpException(
47
                        Response::HTTP_NOT_ACCEPTABLE,
48
                        sprintf('The productTaxon position "%s" is invalid.', $productTaxon['position'])
49
                    );
50
                }
51
52
                $productTaxonFromBase = $this->repository->findOneBy(['id' => $productTaxon['id']]);
53
                $productTaxonFromBase->setPosition($productTaxon['position']);
54
55
                $this->manager->flush();
56
            }
57
        }
58
59
        return new JsonResponse();
60
    }
61
}
62