Completed
Push — npm-shrinkwrap ( 52ca24 )
by Kamil
23:13
created

ProductTaxonPositionController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B updatePositionsAction() 0 31 5
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\ApiBundle\Controller;
13
14
use Doctrine\ORM\EntityManagerInterface;
15
use Sylius\Component\Core\Model\ProductTaxonInterface;
16
use Sylius\Component\Core\Repository\ProductTaxonRepositoryInterface;
17
use Sylius\Component\Resource\Repository\RepositoryInterface;
18
use Symfony\Component\HttpFoundation\JsonResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpKernel\Exception\HttpException;
22
23
/**
24
 * @author Anna Walasek <[email protected]>
25
 */
26
final class ProductTaxonPositionController
27
{
28
    /**
29
     * @var ProductTaxonRepositoryInterface
30
     */
31
    private $productTaxonRepository;
32
33
    /**
34
     * @var EntityManagerInterface
35
     */
36
    private $manager;
37
38
    /**
39
     * @param RepositoryInterface $productTaxonRepository
40
     * @param EntityManagerInterface $manager
41
     */
42
    public function __construct(
43
        RepositoryInterface $productTaxonRepository,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productTaxonRepository 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...
44
        EntityManagerInterface $manager
45
    ) {
46
        $this->productTaxonRepository = $productTaxonRepository;
0 ignored issues
show
Documentation Bug introduced by
$productTaxonRepository is of type object<Sylius\Component\...ry\RepositoryInterface>, but the property $productTaxonRepository was declared to be of type object<Sylius\Component\...xonRepositoryInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
47
        $this->manager = $manager;
48
    }
49
50
    /**
51
     * @param Request $request
52
     *
53
     * @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...
54
     */
55
    public function updatePositionsAction(Request $request)
56
    {
57
        $productsPositions = $request->request->get('productsPositions');
58
59
        if (!in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && null === $productsPositions) {
60
            return new JsonResponse(null, Response::HTTP_NO_CONTENT);
61
        }
62
63
        foreach ($productsPositions as $positionData) {
64
            if (!is_numeric($positionData['position'])) {
65
                throw new HttpException(
66
                    Response::HTTP_BAD_REQUEST,
67
                    sprintf('The productTaxon position "%s" is invalid.', $positionData['position'])
68
                );
69
            }
70
71
            /** @var ProductTaxonInterface $productTaxonFromBase */
72
            $productTaxonFromBase = $this->productTaxonRepository->findOneByProductCodeAndTaxonCode(
73
                $positionData['productCode'],
74
                $request->attributes->get('taxonCode')
75
            );
76
77
            $productTaxonFromBase->setPosition($positionData['position']);
78
79
            $this->manager->persist($productTaxonFromBase);
80
        }
81
82
        $this->manager->flush();
83
84
        return new JsonResponse(null, Response::HTTP_NO_CONTENT);
85
    }
86
}
87