Completed
Push — master ( 7c0075...645752 )
by Kamil
18:52
created

ProductTaxonController::updatePositionsAction()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 14
nc 3
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
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

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