|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use FOS\RestBundle\View\View; |
|
6
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
|
7
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
|
8
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter; |
|
9
|
|
|
use Pagerfanta\Pagerfanta; |
|
10
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
|
11
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
|
12
|
|
|
use Sylius\Component\Core\Model\ProductImage; |
|
13
|
|
|
use Sylius\Component\Core\Model\ProductImageInterface; |
|
14
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
|
15
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
|
16
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
|
17
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
|
18
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
|
19
|
|
|
use Sylius\ShopApiPlugin\Factory\ImageViewFactoryInterface; |
|
20
|
|
|
use Sylius\ShopApiPlugin\Factory\PriceViewFactoryInterface; |
|
21
|
|
|
use Sylius\ShopApiPlugin\Factory\ProductViewFactoryInterface; |
|
22
|
|
|
use Sylius\ShopApiPlugin\View\ImageView; |
|
23
|
|
|
use Sylius\ShopApiPlugin\View\PageLinksView; |
|
24
|
|
|
use Sylius\ShopApiPlugin\View\PageView; |
|
25
|
|
|
use Sylius\ShopApiPlugin\View\ProductVariantView; |
|
26
|
|
|
use Sylius\ShopApiPlugin\View\ProductView; |
|
27
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
28
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
30
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
31
|
|
|
|
|
32
|
|
|
final class ProductController extends Controller |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @param Request $request |
|
36
|
|
|
* |
|
37
|
|
|
* @return Response |
|
38
|
|
|
*/ |
|
39
|
|
|
public function showDetailsAction(Request $request) |
|
40
|
|
|
{ |
|
41
|
|
|
if (!$request->query->has('channel')) { |
|
42
|
|
|
throw new NotFoundHttpException('Cannot find product without channel provided'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** @var ChannelRepositoryInterface $channelRepository */ |
|
46
|
|
|
$channelRepository = $this->get('sylius.repository.channel'); |
|
47
|
|
|
/** @var ProductRepositoryInterface $productRepository */ |
|
48
|
|
|
$productRepository = $this->get('sylius.repository.product'); |
|
49
|
|
|
/** @var ViewHandlerInterface $viewHandler */ |
|
50
|
|
|
$viewHandler = $this->get('fos_rest.view_handler'); |
|
51
|
|
|
/** @var ProductViewFactoryInterface $productViewFactory */ |
|
52
|
|
|
$productViewFactory = $this->get('sylius.shop_api_plugin.factory.product_view_factory'); |
|
53
|
|
|
$channelCode = $request->query->get('channel'); |
|
54
|
|
|
/** @var ChannelInterface $channel */ |
|
55
|
|
|
$channel = $channelRepository->findOneByCode($channelCode); |
|
56
|
|
|
|
|
57
|
|
|
if (null === $channel) { |
|
58
|
|
|
throw new NotFoundHttpException(sprintf('Channel with code %s has not been found', $channelCode)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$locale = $request->query->has('locale') ? $request->query->get('locale') : $channel->getDefaultLocale()->getCode(); |
|
62
|
|
|
|
|
63
|
|
|
$productSlug = $request->attributes->get('slug'); |
|
64
|
|
|
$product = $productRepository->findOneByChannelAndSlug($channel, $locale, $productSlug); |
|
65
|
|
|
|
|
66
|
|
|
if (null === $product) { |
|
67
|
|
|
throw new NotFoundHttpException(sprintf('Product with slug %s has not been found in %s locale.', $productSlug, $locale)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $viewHandler->handle(View::create($productViewFactory->create($product, $channel, $locale), Response::HTTP_OK)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Request $request |
|
75
|
|
|
* |
|
76
|
|
|
* @return Response |
|
77
|
|
|
*/ |
|
78
|
|
|
public function showCatalogAction(Request $request) |
|
79
|
|
|
{ |
|
80
|
|
|
if (!$request->query->has('channel')) { |
|
81
|
|
|
throw new NotFoundHttpException('Cannot find product without channel provided'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** @var ChannelRepositoryInterface $channelRepository */ |
|
85
|
|
|
$channelRepository = $this->get('sylius.repository.channel'); |
|
86
|
|
|
/** @var ProductRepositoryInterface $productRepository */ |
|
87
|
|
|
$productRepository = $this->get('sylius.repository.product'); |
|
88
|
|
|
/** @var TaxonRepositoryInterface $taxonRepository */ |
|
89
|
|
|
$taxonRepository = $this->get('sylius.repository.taxon'); |
|
90
|
|
|
/** @var ViewHandlerInterface $viewHandler */ |
|
91
|
|
|
$viewHandler = $this->get('fos_rest.view_handler'); |
|
92
|
|
|
/** @var ProductViewFactoryInterface $productViewFactory */ |
|
93
|
|
|
$productViewFactory = $this->get('sylius.shop_api_plugin.factory.product_view_factory'); |
|
94
|
|
|
|
|
95
|
|
|
$channelCode = $request->query->get('channel'); |
|
96
|
|
|
/** @var ChannelInterface $channel */ |
|
97
|
|
|
$channel = $channelRepository->findOneByCode($channelCode); |
|
98
|
|
|
|
|
99
|
|
|
if (null === $channel) { |
|
100
|
|
|
throw new NotFoundHttpException(sprintf('Channel with code %s has not been found', $channelCode)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$locale = $request->query->has('locale') ? $request->query->get('locale') : $channel->getDefaultLocale()->getCode(); |
|
104
|
|
|
|
|
105
|
|
|
$taxonSlug = $request->attributes->get('taxonomy'); |
|
106
|
|
|
/** @var TaxonInterface $taxon */ |
|
107
|
|
|
$taxon = $taxonRepository->findOneBySlug($taxonSlug, $locale); |
|
108
|
|
|
|
|
109
|
|
|
if (null === $taxon) { |
|
110
|
|
|
throw new NotFoundHttpException(sprintf('Taxon with slug %s in locale %s has not been found', $taxonSlug, $locale)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$queryBuilder = $productRepository->createShopListQueryBuilder($channel, $taxon, $locale); |
|
114
|
|
|
$adapter = new DoctrineORMAdapter($queryBuilder); |
|
115
|
|
|
$pagerfanta = new Pagerfanta($adapter); |
|
116
|
|
|
|
|
117
|
|
|
$pagerfanta->setMaxPerPage($request->query->get('limit', 10)); |
|
118
|
|
|
$pagerfanta->setCurrentPage($request->query->get('page', 1)); |
|
119
|
|
|
|
|
120
|
|
|
$page = new PageView(); |
|
121
|
|
|
$page->page = $pagerfanta->getCurrentPage(); |
|
122
|
|
|
$page->limit = $pagerfanta->getMaxPerPage(); |
|
123
|
|
|
$page->pages = $pagerfanta->getNbPages(); |
|
124
|
|
|
$page->total = $pagerfanta->getNbResults(); |
|
125
|
|
|
|
|
126
|
|
|
$page->links = new PageLinksView(); |
|
127
|
|
|
|
|
128
|
|
|
$page->links->self = $this->generateUrl('shop_api_product_show_catalog', [ |
|
129
|
|
|
'taxonomy' => $taxonSlug, |
|
130
|
|
|
'page' => $pagerfanta->getCurrentPage(), |
|
131
|
|
|
'limit' => $pagerfanta->getMaxPerPage(), |
|
132
|
|
|
]); |
|
133
|
|
|
$page->links->first = $this->generateUrl('shop_api_product_show_catalog', [ |
|
134
|
|
|
'taxonomy' => $taxonSlug, |
|
135
|
|
|
'page' => 1, |
|
136
|
|
|
'limit' => $pagerfanta->getMaxPerPage(), |
|
137
|
|
|
]); |
|
138
|
|
|
$page->links->last = $this->generateUrl('shop_api_product_show_catalog', [ |
|
139
|
|
|
'taxonomy' => $taxonSlug, |
|
140
|
|
|
'page' => $pagerfanta->getNbPages(), |
|
141
|
|
|
'limit' => $pagerfanta->getMaxPerPage(), |
|
142
|
|
|
]); |
|
143
|
|
|
$page->links->next = $this->generateUrl('shop_api_product_show_catalog', [ |
|
144
|
|
|
'taxonomy' => $taxonSlug, |
|
145
|
|
|
'page' => ($pagerfanta->getCurrentPage() < $pagerfanta->getNbPages()) ? $pagerfanta->getCurrentPage() + 1 : $pagerfanta->getCurrentPage(), |
|
146
|
|
|
'limit' => $pagerfanta->getMaxPerPage(), |
|
147
|
|
|
]); |
|
148
|
|
|
|
|
149
|
|
|
foreach ($pagerfanta->getCurrentPageResults() as $currentPageResult) { |
|
150
|
|
|
$page->items[] = $productViewFactory->create($currentPageResult, $channel, $locale); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $viewHandler->handle(View::create($page, Response::HTTP_OK)); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|