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; |
|
|
|
|
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
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.