1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusVendorPlugin\Controller; |
6
|
|
|
|
7
|
|
|
use Odiseo\SyliusVendorPlugin\Entity\VendorInterface; |
8
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
9
|
|
|
use Sylius\Component\Resource\ResourceActions; |
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
14
|
|
|
|
15
|
|
|
class VendorController extends ResourceController |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param Request $request |
19
|
|
|
* @return Response |
20
|
|
|
*/ |
21
|
|
|
public function updatePositionsAction(Request $request): Response |
22
|
|
|
{ |
23
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
24
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::UPDATE); |
25
|
|
|
$vendorsToUpdate = $request->get('vendors'); |
26
|
|
|
|
27
|
|
|
if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid('update-vendor-position', $request->request->get('_csrf_token'))) { |
28
|
|
|
throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && null !== $vendorsToUpdate) { |
32
|
|
|
/** @var array $vendorToUpdate */ |
33
|
|
|
foreach ($vendorsToUpdate as $vendorToUpdate) { |
34
|
|
|
if (!is_numeric($vendorToUpdate['position'])) { |
35
|
|
|
throw new HttpException( |
36
|
|
|
Response::HTTP_NOT_ACCEPTABLE, |
37
|
|
|
sprintf('The vendor position "%s" is invalid.', $vendorToUpdate['position']) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** @var VendorInterface $vendor */ |
42
|
|
|
$vendor = $this->repository->findOneBy(['id' => $vendorToUpdate['id']]); |
43
|
|
|
$vendor->setPosition((int) $vendorToUpdate['position']); |
44
|
|
|
$this->manager->flush(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return new JsonResponse(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|