1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Setono\SyliusLagersystemPlugin\Controller\Product; |
6
|
|
|
|
7
|
|
|
use FOS\RestBundle\View\View; |
8
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
9
|
|
|
use function Safe\sprintf; |
10
|
|
|
use Setono\SyliusLagersystemPlugin\Factory\Product\ProductVariantViewFactoryInterface; |
11
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
12
|
|
|
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
16
|
|
|
use Webmozart\Assert\Assert; |
17
|
|
|
|
18
|
|
|
final class ProductVariantShowAction |
19
|
|
|
{ |
20
|
|
|
/** @var ViewHandlerInterface */ |
21
|
|
|
private $viewHandler; |
22
|
|
|
|
23
|
|
|
/** @var ProductVariantRepositoryInterface */ |
24
|
|
|
private $productVariantRepository; |
25
|
|
|
|
26
|
|
|
/** @var ProductVariantViewFactoryInterface */ |
27
|
|
|
private $productVariantViewFactory; |
28
|
|
|
|
29
|
|
|
public function __construct( |
30
|
|
|
ViewHandlerInterface $viewHandler, |
31
|
|
|
ProductVariantViewFactoryInterface $productVariantViewFactory, |
32
|
|
|
ProductVariantRepositoryInterface $productVariantRepository |
33
|
|
|
) { |
34
|
|
|
$this->viewHandler = $viewHandler; |
35
|
|
|
$this->productVariantViewFactory = $productVariantViewFactory; |
36
|
|
|
$this->productVariantRepository = $productVariantRepository; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function __invoke(Request $request): Response |
40
|
|
|
{ |
41
|
|
|
$code = $request->attributes->get('code'); |
42
|
|
|
/** @var ProductVariantInterface|null $variant */ |
43
|
|
|
$variant = $this->productVariantRepository->findOneBy(['code' => $code]); |
44
|
|
|
if (null === $variant) { |
45
|
|
|
throw new NotFoundHttpException(sprintf('Variant with code %s has not been found.', $code)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$locale = $request->query->get('locale'); |
49
|
|
|
Assert::notNull($locale); |
50
|
|
|
|
51
|
|
|
return $this->viewHandler->handle( |
52
|
|
|
View::create( |
53
|
|
|
$this->productVariantViewFactory->create( |
54
|
|
|
$variant, |
55
|
|
|
$locale |
56
|
|
|
), |
57
|
|
|
Response::HTTP_OK |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|