Conditions | 7 |
Paths | 8 |
Total Lines | 77 |
Code Lines | 47 |
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 |
||
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 |