Conditions | 12 |
Paths | 116 |
Total Lines | 87 |
Code Lines | 58 |
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 |
||
53 | public function indexAction(Request $request): Response |
||
54 | { |
||
55 | $form = $this->createForm(DateType::class); |
||
56 | $formPeriod = $this->createForm(PeriodType::class); |
||
57 | |||
58 | // Form not submitted yet |
||
59 | if (null === $request->request->get($form->getName()) && null === $request->request->get($formPeriod->getName())) { |
||
60 | return $this->render('@MonsieurBizSyliusSalesReportsPlugin/Admin/index.html.twig', [ |
||
61 | 'form' => $form->createView(), |
||
62 | 'form_period' => $formPeriod->createView(), |
||
63 | ]); |
||
64 | } |
||
65 | |||
66 | // Submit request data and return form if form is not valid |
||
67 | if ($request->request->get($form->getName())) { |
||
68 | $form->submit($request->request->get($form->getName())); |
||
69 | if (!$form->isSubmitted() || !$form->isValid()) { |
||
70 | return $this->render('@MonsieurBizSyliusSalesReportsPlugin/Admin/index.html.twig', [ |
||
71 | 'form' => $form->createView(), |
||
72 | 'form_period' => $formPeriod->createView(), |
||
73 | ]); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | // Submit request data and return form period if form period is not valid |
||
78 | if ($request->request->get($formPeriod->getName())) { |
||
79 | $formPeriod->submit($request->request->get($formPeriod->getName())); |
||
80 | if (!$formPeriod->isSubmitted() || !$formPeriod->isValid()) { |
||
81 | return $this->render('@MonsieurBizSyliusSalesReportsPlugin/Admin/index.html.twig', [ |
||
82 | 'form' => $form->createView(), |
||
83 | 'form_period' => $formPeriod->createView(), |
||
84 | ]); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | // Assert retrieved data |
||
89 | $data = $form->getData(); |
||
90 | $isPeriod = false; |
||
91 | if (!$data) { |
||
92 | $data = $formPeriod->getData(); |
||
93 | $isPeriod = true; |
||
94 | } |
||
95 | $channel = $data['channel']; |
||
96 | $from = $data['date'] ?? $data['from']; |
||
97 | $to = $data['date'] ?? $data['to']; |
||
98 | |||
99 | // Reverse date if from date greater than end date |
||
100 | if ($from > $to) { |
||
101 | $tmp = $to; $to = $from; $from = $tmp; $data['from'] = $from; $data['to'] = $to; |
||
102 | } |
||
103 | |||
104 | Assert::isInstanceOf($channel, ChannelInterface::class); |
||
105 | Assert::isInstanceOf($from, \DateTimeInterface::class); |
||
106 | Assert::isInstanceOf($to, \DateTimeInterface::class); |
||
107 | |||
108 | // Form is valid, we can generate the report |
||
109 | try { |
||
110 | $totalSalesResult = $this->reportRepository->getSalesForChannelForDates($channel, $from, $to); |
||
111 | $averageSalesResult = $this->reportRepository->getAverageSalesForChannelForDates($channel, $from, $to); |
||
112 | $productSalesResult = $this->reportRepository->getProductSalesForChannelForDates($channel, $from, $to); |
||
113 | $productVariantSalesResult = $this->reportRepository->getProductVariantSalesForChannelForDates($channel, $from, $to); |
||
114 | $productOptionSalesResult = $this->reportRepository->getProductOptionSalesForChannelForDates($channel, $from, $to); |
||
115 | $productOptionValueSalesResult = $this->reportRepository->getProductOptionValueSalesForChannelForDates($channel, $from, $to); |
||
116 | } catch (InvalidDateException $e) { |
||
117 | $form->addError(new FormError($e->getMessage())); |
||
118 | return $this->render('@MonsieurBizSyliusSalesReportsPlugin/Admin/index.html.twig', [ |
||
119 | 'form' => $form->createView(), |
||
120 | ]); |
||
121 | } |
||
122 | |||
123 | $event = new CustomReportEvent($channel, $from, $to); |
||
124 | $this->eventDispatcher->dispatch($event); |
||
125 | |||
126 | return $this->render('@MonsieurBizSyliusSalesReportsPlugin/Admin/view.html.twig', [ |
||
127 | 'form' => $form->createView(), |
||
128 | 'form_period' => $formPeriod->createView(), |
||
129 | 'from' => $from, |
||
130 | 'to' => $to, |
||
131 | 'channel' => $data['channel'], |
||
132 | 'total_sales_result' => $totalSalesResult, |
||
133 | 'average_sales_result' => $averageSalesResult, |
||
134 | 'product_sales_result' => $productSalesResult, |
||
135 | 'product_variant_sales_result' => $productVariantSalesResult, |
||
136 | 'product_option_sales_result' => $productOptionSalesResult, |
||
137 | 'product_option_value_sales_result' => $productOptionValueSalesResult, |
||
138 | 'is_period' => $isPeriod, |
||
139 | 'custom_reports' => $event->getCustomReports(), |
||
140 | ]); |
||
143 |