|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Controller\Product; |
|
4
|
|
|
|
|
5
|
|
|
use FOS\RestBundle\View\View; |
|
6
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
|
7
|
|
|
use Pagerfanta\Adapter\ArrayAdapter; |
|
8
|
|
|
use Pagerfanta\Pagerfanta; |
|
9
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
|
10
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
|
11
|
|
|
use Sylius\Component\Core\Repository\ProductReviewRepositoryInterface; |
|
12
|
|
|
use Sylius\ShopApiPlugin\Factory\ProductReviewViewFactoryInterface; |
|
13
|
|
|
use Sylius\ShopApiPlugin\Factory\ProductViewFactoryInterface; |
|
14
|
|
|
use Sylius\ShopApiPlugin\View\PageLinksView; |
|
15
|
|
|
use Sylius\ShopApiPlugin\View\PageView; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
18
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
19
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
20
|
|
|
|
|
21
|
|
|
final class ShowReviewsAction |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ChannelRepositoryInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $channelRepository; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ProductReviewRepositoryInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $productReviewRepository; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var ViewHandlerInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $viewHandler; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var ProductReviewViewFactoryInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $productReviewViewFactory; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var RouterInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $router; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct( |
|
49
|
|
|
ChannelRepositoryInterface $channelRepository, |
|
50
|
|
|
ProductReviewRepositoryInterface $productReviewRepository, |
|
51
|
|
|
ViewHandlerInterface $viewHandler, |
|
52
|
|
|
ProductReviewViewFactoryInterface $productReviewViewFactory, |
|
53
|
|
|
RouterInterface $router |
|
54
|
|
|
) { |
|
55
|
|
|
$this->channelRepository = $channelRepository; |
|
56
|
|
|
$this->productReviewRepository = $productReviewRepository; |
|
57
|
|
|
$this->viewHandler = $viewHandler; |
|
58
|
|
|
$this->productReviewViewFactory = $productReviewViewFactory; |
|
59
|
|
|
$this->router = $router; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function __invoke(Request $request): Response |
|
63
|
|
|
{ |
|
64
|
|
|
if (!$request->query->has('channel')) { |
|
65
|
|
|
throw new NotFoundHttpException('Cannot find product without channel provided'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$channelCode = $request->query->get('channel'); |
|
69
|
|
|
/** @var ChannelInterface $channel */ |
|
70
|
|
|
$channel = $this->channelRepository->findOneByCode($channelCode); |
|
71
|
|
|
|
|
72
|
|
|
if (null === $channel) { |
|
73
|
|
|
throw new NotFoundHttpException(sprintf('Channel with code %s has not been found', $channelCode)); |
|
74
|
|
|
} |
|
75
|
|
|
$slug = $request->attributes->get('slug'); |
|
76
|
|
|
|
|
77
|
|
|
$locale = $request->query->has('locale') ? $request->query->get('locale') : $channel->getDefaultLocale()->getCode(); |
|
78
|
|
|
|
|
79
|
|
|
$reviews = $this->productReviewRepository->findAcceptedByProductSlugAndChannel($request->attributes->get('slug'), $locale, $channel); |
|
80
|
|
|
|
|
81
|
|
|
$adapter = new ArrayAdapter($reviews); |
|
82
|
|
|
$pagerfanta = new Pagerfanta($adapter); |
|
83
|
|
|
|
|
84
|
|
|
$pagerfanta->setMaxPerPage($request->query->get('limit', 10)); |
|
85
|
|
|
$pagerfanta->setCurrentPage($request->query->get('page', 1)); |
|
86
|
|
|
|
|
87
|
|
|
$page = new PageView(); |
|
88
|
|
|
$page->page = $pagerfanta->getCurrentPage(); |
|
89
|
|
|
$page->limit = $pagerfanta->getMaxPerPage(); |
|
90
|
|
|
$page->pages = $pagerfanta->getNbPages(); |
|
91
|
|
|
$page->total = $pagerfanta->getNbResults(); |
|
92
|
|
|
|
|
93
|
|
|
$page->links = new PageLinksView(); |
|
94
|
|
|
|
|
95
|
|
|
$page->links->self = $this->router->generate('shop_api_product_show_reviews', [ |
|
96
|
|
|
'slug' => $slug, |
|
97
|
|
|
'channel' => $channelCode, |
|
98
|
|
|
'page' => $pagerfanta->getCurrentPage(), |
|
99
|
|
|
'limit' => $pagerfanta->getMaxPerPage(), |
|
100
|
|
|
]); |
|
101
|
|
|
$page->links->first = $this->router->generate('shop_api_product_show_reviews', [ |
|
102
|
|
|
'slug' => $slug, |
|
103
|
|
|
'channel' => $channelCode, |
|
104
|
|
|
'page' => 1, |
|
105
|
|
|
'limit' => $pagerfanta->getMaxPerPage(), |
|
106
|
|
|
]); |
|
107
|
|
|
$page->links->last = $this->router->generate('shop_api_product_show_reviews', [ |
|
108
|
|
|
'slug' => $slug, |
|
109
|
|
|
'channel' => $channelCode, |
|
110
|
|
|
'page' => $pagerfanta->getNbPages(), |
|
111
|
|
|
'limit' => $pagerfanta->getMaxPerPage(), |
|
112
|
|
|
]); |
|
113
|
|
|
$page->links->next = $this->router->generate('shop_api_product_show_reviews', [ |
|
114
|
|
|
'slug' => $slug, |
|
115
|
|
|
'channel' => $channelCode, |
|
116
|
|
|
'page' => ($pagerfanta->getCurrentPage() < $pagerfanta->getNbPages()) ? $pagerfanta->getCurrentPage() + 1 : $pagerfanta->getCurrentPage(), |
|
117
|
|
|
'limit' => $pagerfanta->getMaxPerPage(), |
|
118
|
|
|
]); |
|
119
|
|
|
|
|
120
|
|
|
foreach ($pagerfanta->getCurrentPageResults() as $currentPageResult) { |
|
121
|
|
|
$page->items[] = $this->productReviewViewFactory->create($currentPageResult); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $this->viewHandler->handle(View::create($page, Response::HTTP_OK)); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|