| Total Complexity | 41 |
| Total Lines | 386 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like BusinessUnitController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BusinessUnitController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class BusinessUnitController extends AbstractCompanyController |
||
| 23 | { |
||
| 24 | protected const MESSAGE_FORM_CSRF_VALIDATION_ERROR = 'form.csrf.error.text'; |
||
| 25 | protected const BUSINESS_UNIT_LIST_SORT_FIELD = 'id_company_business_unit'; |
||
| 26 | protected const COMPANY_UNIT_ADDRESS_LIST_SORT_FIELD = 'id_company_unit_address'; |
||
| 27 | protected const REQUEST_PARAM_ID = 'id'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @return array|\Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 31 | */ |
||
| 32 | public function indexAction() |
||
| 33 | { |
||
| 34 | $viewData = $this->executeIndexAction(); |
||
| 35 | |||
| 36 | return $this->view($viewData, [], '@CompanyPage/views/business-unit/business-unit.twig'); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | protected function executeIndexAction(): array |
||
| 43 | { |
||
| 44 | return [ |
||
| 45 | 'businessUnitsTree' => $this->getFactory()->createCompanyBusinessUnitTreeBuilder()->getCustomerCompanyBusinessUnitTree(), |
||
| 46 | ]; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 51 | * |
||
| 52 | * @return array|\Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 53 | */ |
||
| 54 | public function detailsAction(Request $request) |
||
| 55 | { |
||
| 56 | $viewData = $this->getCompanyBusinessUnitDetailsResponseData($request); |
||
| 57 | |||
| 58 | return $this->view($viewData, [], '@CompanyPage/views/business-unit-detail/business-unit-detail.twig'); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 63 | * |
||
| 64 | * @return array|\Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 65 | */ |
||
| 66 | public function createAction(Request $request) |
||
| 67 | { |
||
| 68 | $response = $this->executeCreateAction($request); |
||
| 69 | |||
| 70 | if (!is_array($response)) { |
||
| 71 | return $response; |
||
| 72 | } |
||
| 73 | |||
| 74 | return $this->view($response, [], '@CompanyPage/views/business-unit-create/business-unit-create.twig'); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 79 | * |
||
| 80 | * @return array|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 81 | */ |
||
| 82 | protected function executeCreateAction(Request $request) |
||
| 83 | { |
||
| 84 | $dataProvider = $this->getFactory() |
||
| 85 | ->createCompanyPageFormFactory() |
||
| 86 | ->createBusinessUnitFormDataProvider(); |
||
| 87 | |||
| 88 | $companyUserTransfer = $this->findCurrentCompanyUserTransfer(); |
||
| 89 | $idCompanyBusinessUnit = $request->query->getInt(static::REQUEST_PARAM_ID); |
||
| 90 | $dataProviderOptions = $dataProvider->getOptions($companyUserTransfer, $idCompanyBusinessUnit); |
||
|
|
|||
| 91 | |||
| 92 | $companyBusinessUnitForm = $this->getFactory() |
||
| 93 | ->createCompanyPageFormFactory() |
||
| 94 | ->getBusinessUnitForm($dataProviderOptions) |
||
| 95 | ->handleRequest($request); |
||
| 96 | |||
| 97 | if ($companyBusinessUnitForm->isSubmitted() === false) { |
||
| 98 | $companyBusinessUnitForm->setData($dataProvider->getData($this->findCurrentCompanyUserTransfer())); |
||
| 99 | } |
||
| 100 | |||
| 101 | if ($companyBusinessUnitForm->isSubmitted() === true && $companyBusinessUnitForm->isValid() === true) { |
||
| 102 | $companyBusinessUnitResponseTransfer = $this->companyBusinessUnitSave($companyBusinessUnitForm->getData()); |
||
| 103 | |||
| 104 | if ($companyBusinessUnitResponseTransfer->getIsSuccessful()) { |
||
| 105 | $this->applySuccessMessage($companyBusinessUnitResponseTransfer); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!$companyBusinessUnitResponseTransfer->getIsSuccessful()) { |
||
| 109 | $this->applyErrorMessage($companyBusinessUnitResponseTransfer); |
||
| 110 | } |
||
| 111 | |||
| 112 | return $this->redirectResponseInternal(CompanyPageControllerProvider::ROUTE_COMPANY_BUSINESS_UNIT_UPDATE, [ |
||
| 113 | 'id' => $companyBusinessUnitResponseTransfer->getCompanyBusinessUnitTransfer()->getIdCompanyBusinessUnit(), |
||
| 114 | ]); |
||
| 115 | } |
||
| 116 | |||
| 117 | return [ |
||
| 118 | 'form' => $companyBusinessUnitForm->createView(), |
||
| 119 | ]; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 124 | * |
||
| 125 | * @return array|\Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 126 | */ |
||
| 127 | public function updateAction(Request $request) |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 140 | * |
||
| 141 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 142 | * |
||
| 143 | * @return array|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 144 | */ |
||
| 145 | protected function executeUpdateAction(Request $request) |
||
| 146 | { |
||
| 147 | $dataProvider = $this->getFactory() |
||
| 148 | ->createCompanyPageFormFactory() |
||
| 149 | ->createBusinessUnitFormDataProvider(); |
||
| 150 | |||
| 151 | $companyUserTransfer = $this->findCurrentCompanyUserTransfer(); |
||
| 152 | $idCompanyBusinessUnit = $request->query->getInt(static::REQUEST_PARAM_ID); |
||
| 153 | $dataProviderOptions = $dataProvider->getOptions($companyUserTransfer, $idCompanyBusinessUnit); |
||
| 154 | |||
| 155 | $companyBusinessUnitForm = $this->getFactory() |
||
| 156 | ->createCompanyPageFormFactory() |
||
| 157 | ->getBusinessUnitForm($dataProviderOptions) |
||
| 158 | ->handleRequest($request); |
||
| 159 | |||
| 160 | if ($companyBusinessUnitForm->isSubmitted() === false) { |
||
| 161 | $data = $dataProvider->getData($this->findCurrentCompanyUserTransfer(), $idCompanyBusinessUnit); |
||
| 162 | |||
| 163 | $companyBusinessUnitTransfer = $this->getFactory() |
||
| 164 | ->getCompanyBusinessUnitClient() |
||
| 165 | ->getCompanyBusinessUnitById((new CompanyBusinessUnitTransfer())->setIdCompanyBusinessUnit($idCompanyBusinessUnit)); |
||
| 166 | |||
| 167 | if (!$this->isCurrentCustomerRelatedToCompany($companyBusinessUnitTransfer->getFkCompany())) { |
||
| 168 | throw new NotFoundHttpException(); |
||
| 169 | } |
||
| 170 | |||
| 171 | $companyBusinessUnitForm->setData($data); |
||
| 172 | } |
||
| 173 | |||
| 174 | if ($companyBusinessUnitForm->isSubmitted() === true && $companyBusinessUnitForm->isValid() === true) { |
||
| 175 | $companyBusinessUnitResponseTransfer = $this->companyBusinessUnitSave($companyBusinessUnitForm->getData()); |
||
| 176 | |||
| 177 | if ($companyBusinessUnitResponseTransfer->getIsSuccessful()) { |
||
| 178 | $this->applySuccessMessage($companyBusinessUnitResponseTransfer); |
||
| 179 | } |
||
| 180 | |||
| 181 | if (!$companyBusinessUnitResponseTransfer->getIsSuccessful()) { |
||
| 182 | $this->applyErrorMessage($companyBusinessUnitResponseTransfer); |
||
| 183 | } |
||
| 184 | |||
| 185 | return $this->redirectResponseInternal(CompanyPageControllerProvider::ROUTE_COMPANY_BUSINESS_UNIT_UPDATE, [ |
||
| 186 | 'id' => $idCompanyBusinessUnit, |
||
| 187 | ]); |
||
| 188 | } |
||
| 189 | |||
| 190 | return [ |
||
| 191 | 'form' => $companyBusinessUnitForm->createView(), |
||
| 192 | 'addresses' => $dataProviderOptions[CompanyBusinessUnitForm::FIELD_COMPANY_UNIT_ADDRESSES], |
||
| 193 | ]; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 198 | * |
||
| 199 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 200 | * |
||
| 201 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 202 | */ |
||
| 203 | public function deleteAction(Request $request) |
||
| 204 | { |
||
| 205 | $companyBusinessUnitDeleteForm = $this->getFactory()->createCompanyPageFormFactory()->getCompanyBusinessUnitDeleteForm()->handleRequest($request); |
||
| 206 | |||
| 207 | if (!$companyBusinessUnitDeleteForm->isSubmitted() || !$companyBusinessUnitDeleteForm->isValid()) { |
||
| 208 | $this->addErrorMessage(static::MESSAGE_FORM_CSRF_VALIDATION_ERROR); |
||
| 209 | |||
| 210 | return $this->redirectResponseInternal(CompanyPageControllerProvider::ROUTE_COMPANY_BUSINESS_UNIT); |
||
| 211 | } |
||
| 212 | |||
| 213 | $companyBusinessUnitId = $request->query->getInt(static::REQUEST_PARAM_ID); |
||
| 214 | $companyBusinessUnitTransfer = new CompanyBusinessUnitTransfer(); |
||
| 215 | $companyBusinessUnitTransfer->setIdCompanyBusinessUnit($companyBusinessUnitId); |
||
| 216 | |||
| 217 | $companyBusinessUnitTransfer = $this->getFactory() |
||
| 218 | ->getCompanyBusinessUnitClient() |
||
| 219 | ->getCompanyBusinessUnitById($companyBusinessUnitTransfer); |
||
| 220 | |||
| 221 | if (!$this->isCurrentCustomerRelatedToCompany($companyBusinessUnitTransfer->getFkCompany())) { |
||
| 222 | throw new NotFoundHttpException(); |
||
| 223 | } |
||
| 224 | |||
| 225 | $companyBusinessUnitResponseTransfer = $this->getFactory() |
||
| 226 | ->getCompanyBusinessUnitClient() |
||
| 227 | ->deleteCompanyBusinessUnit($companyBusinessUnitTransfer); |
||
| 228 | |||
| 229 | if ($companyBusinessUnitResponseTransfer->getIsSuccessful()) { |
||
| 230 | $this->applySuccessMessage($companyBusinessUnitResponseTransfer); |
||
| 231 | } |
||
| 232 | |||
| 233 | if (!$companyBusinessUnitResponseTransfer->getIsSuccessful()) { |
||
| 234 | $this->applyErrorMessage($companyBusinessUnitResponseTransfer); |
||
| 235 | } |
||
| 236 | |||
| 237 | return $this->redirectResponseInternal(CompanyPageControllerProvider::ROUTE_COMPANY_BUSINESS_UNIT); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 242 | * |
||
| 243 | * @return array|\Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 244 | */ |
||
| 245 | public function confirmDeleteAction(Request $request) |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 258 | * |
||
| 259 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 260 | * |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | protected function executeConfirmDeleteAction(Request $request): array |
||
| 280 | ]; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 285 | * |
||
| 286 | * @return \Generated\Shared\Transfer\CompanyBusinessUnitCriteriaFilterTransfer |
||
| 287 | */ |
||
| 288 | protected function createBusinessUnitCriteriaFilterTransfer(Request $request): CompanyBusinessUnitCriteriaFilterTransfer |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 305 | * |
||
| 306 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | protected function getCompanyBusinessUnitDetailsResponseData(Request $request): array |
||
| 342 | ]; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 347 | * |
||
| 348 | * @return \Generated\Shared\Transfer\CompanyUnitAddressCriteriaFilterTransfer |
||
| 349 | */ |
||
| 350 | protected function createCompanyUnitAddressCriteriaFilterTransfer( |
||
| 351 | Request $request |
||
| 352 | ): CompanyUnitAddressCriteriaFilterTransfer { |
||
| 353 | $criteriaFilterTransfer = new CompanyUnitAddressCriteriaFilterTransfer(); |
||
| 354 | $criteriaFilterTransfer->setIdCompany($this->findCurrentCompanyUserTransfer()->getFkCompany()); |
||
| 355 | |||
| 356 | $filterTransfer = $this->createFilterTransfer(self::COMPANY_UNIT_ADDRESS_LIST_SORT_FIELD); |
||
| 357 | $criteriaFilterTransfer->setFilter($filterTransfer); |
||
| 358 | |||
| 359 | $paginationTransfer = $this->createPaginationTransfer($request); |
||
| 360 | $criteriaFilterTransfer->setPagination($paginationTransfer); |
||
| 361 | |||
| 362 | return $criteriaFilterTransfer; |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param array $data |
||
| 367 | * |
||
| 368 | * @return \Generated\Shared\Transfer\CompanyBusinessUnitResponseTransfer |
||
| 369 | */ |
||
| 370 | protected function companyBusinessUnitSave(array $data): CompanyBusinessUnitResponseTransfer |
||
| 371 | { |
||
| 372 | $companyBusinessUnitTransfer = new CompanyBusinessUnitTransfer(); |
||
| 373 | $companyBusinessUnitTransfer->fromArray($data, true); |
||
| 374 | |||
| 375 | $companyBusinessUnitClient = $this->getFactory()->getCompanyBusinessUnitClient(); |
||
| 376 | |||
| 377 | if ($companyBusinessUnitTransfer->getIdCompanyBusinessUnit()) { |
||
| 378 | return $companyBusinessUnitClient->updateCompanyBusinessUnit($companyBusinessUnitTransfer); |
||
| 379 | } |
||
| 380 | |||
| 381 | return $companyBusinessUnitClient->createCompanyBusinessUnit($companyBusinessUnitTransfer); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param \Generated\Shared\Transfer\CompanyBusinessUnitResponseTransfer $companyBusinessUnitResponseTransfer |
||
| 386 | * |
||
| 387 | * @return void |
||
| 388 | */ |
||
| 389 | protected function applyErrorMessage(CompanyBusinessUnitResponseTransfer $companyBusinessUnitResponseTransfer): void |
||
| 394 | ]); |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param \Generated\Shared\Transfer\CompanyBusinessUnitResponseTransfer $companyBusinessUnitResponseTransfer |
||
| 400 | * |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | protected function applySuccessMessage(CompanyBusinessUnitResponseTransfer $companyBusinessUnitResponseTransfer): void |
||
| 408 | ]); |
||
| 409 | } |
||
| 412 |