Completed
Push — master ( 421643...165cff )
by Paweł
34:09 queued 24:31
created

ProductTaxonPositionController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
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,
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
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