Conditions | 6 |
Paths | 6 |
Total Lines | 64 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 |