Complex classes like RewriteUrlAdminController 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 RewriteUrlAdminController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class RewriteUrlAdminController extends BaseAdminController |
||
46 | { |
||
47 | /** @var array */ |
||
48 | private $correspondence = array( |
||
49 | 'brand' => 'brand', |
||
50 | 'category' => 'categories', |
||
51 | 'content' => 'content', |
||
52 | 'folder' => 'folders', |
||
53 | 'product' => 'products' |
||
54 | ); |
||
55 | |||
56 | /** |
||
57 | * @return mixed |
||
58 | */ |
||
59 | public function deleteAction() |
||
60 | { |
||
61 | if (null !== $response = $this->checkAuth(array(AdminResources::MODULE), 'RewriteUrl', AccessManager::DELETE)) { |
||
62 | return $response; |
||
63 | } |
||
64 | |||
65 | $id_url = $this->getRequest()->request->get('id_url'); |
||
66 | $rewritingUrl = RewritingUrlQuery::create()->findOneById($id_url); |
||
67 | |||
68 | if ($rewritingUrl !== null) { |
||
69 | $event = new RewriteUrlEvent($rewritingUrl); |
||
70 | $this->getDispatcher()->dispatch(RewriteUrlEvents::REWRITEURL_DELETE, $event); |
||
71 | } |
||
72 | |||
73 | if (method_exists($this, 'generateSuccessRedirect')) { |
||
74 | //for 2.1 |
||
75 | return $this->generateRedirectFromRoute( |
||
76 | 'admin.'.$this->correspondence[$rewritingUrl->getView()].'.update', |
||
77 | [ |
||
78 | $rewritingUrl->getView().'_id'=>$rewritingUrl->getViewId(), |
||
79 | 'current_tab' => 'modules' |
||
80 | ], |
||
81 | [ |
||
82 | $rewritingUrl->getView().'_id'=>$rewritingUrl->getViewId() |
||
83 | ] |
||
84 | ); |
||
85 | } else { |
||
86 | //for 2.0 |
||
87 | $this->redirectToRoute( |
||
88 | 'admin.'.$this->correspondence[$rewritingUrl->getView()].'.update', |
||
89 | [ |
||
90 | $rewritingUrl->getView().'_id'=>$rewritingUrl->getViewId(), |
||
91 | 'current_tab' => 'modules' |
||
92 | ], |
||
93 | [ |
||
94 | $rewritingUrl->getView().'_id'=>$rewritingUrl->getViewId() |
||
95 | ] |
||
96 | ); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return mixed |
||
102 | */ |
||
103 | public function addAction() |
||
104 | { |
||
105 | $message = null; |
||
106 | $exception = null; |
||
107 | |||
108 | if (null !== $response = $this->checkAuth(array(AdminResources::MODULE), 'RewriteUrl', AccessManager::CREATE)) { |
||
109 | return $response; |
||
110 | } |
||
111 | |||
112 | $addForm = new AddUrlForm($this->getRequest()); |
||
113 | |||
114 | try { |
||
115 | $form = $this->validateForm($addForm); |
||
116 | $data = $form->getData($form); |
||
117 | |||
118 | $findExist = RewritingUrlQuery::create()->findOneByUrl(($data['url'])); |
||
119 | |||
120 | if ($findExist !== null && !in_array($findExist->getView(), RewriteUrl::getUnknownSources()) && $findExist->getView() !== '') { |
||
121 | $url = $this->generateUrlByRewritingUrl($findExist); |
||
122 | |||
123 | throw new \Exception( |
||
124 | Translator::getInstance()->trans( |
||
125 | "This url is already used here %url.", |
||
126 | ['%url' => '<a href="' . $url . '">' . $url . '</a>'], |
||
127 | RewriteUrl::MODULE_DOMAIN |
||
128 | ) |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | $rewriting = $findExist !== null ? $findExist : new RewritingUrlOverride(); |
||
133 | $rewriting->setUrl($data['url']) |
||
134 | ->setView($data['view']) |
||
135 | ->setViewId($data['view-id']) |
||
136 | ->setViewLocale($data['locale']); |
||
137 | |||
138 | $rewriteDefault = RewritingUrlQuery::create() |
||
139 | ->filterByView($rewriting->getView()) |
||
140 | ->filterByViewId($rewriting->getViewId()) |
||
141 | ->filterByViewLocale($rewriting->getViewLocale()) |
||
142 | ->findOneByRedirected(null); |
||
143 | |||
144 | if ($data['default'] == 1) { |
||
145 | $rewriting->setRedirected(null); |
||
146 | } else { |
||
147 | if ($rewriteDefault !== null) { |
||
148 | $rewriting->setRedirected($rewriteDefault->getId()); |
||
149 | } else { |
||
150 | $rewriting->setRedirected(null); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | $this->getDispatcher()->dispatch( |
||
155 | RewriteUrlEvents::REWRITEURL_ADD, |
||
156 | new RewriteUrlEvent($rewriting) |
||
157 | ); |
||
158 | |||
159 | if (method_exists($this, 'generateSuccessRedirect')) { |
||
160 | //for 2.1 |
||
161 | return $this->generateSuccessRedirect($addForm); |
||
162 | } else { |
||
163 | //for 2.0 |
||
164 | $this->redirectSuccess($addForm); |
||
165 | } |
||
166 | |||
167 | } catch (FormValidationException $exception) { |
||
168 | $message = $this->createStandardFormValidationErrorMessage($exception); |
||
169 | } catch (\Exception $exception) { |
||
170 | $message = $exception->getMessage(); |
||
171 | } |
||
172 | |||
173 | if ($message !== null && $exception !== null) { |
||
174 | Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $exception->getMessage())); |
||
175 | |||
176 | $addForm->setErrorMessage($message); |
||
177 | |||
178 | $this->getParserContext() |
||
179 | ->addForm($addForm) |
||
180 | ->setGeneralError($message) |
||
181 | ; |
||
182 | } |
||
183 | |||
184 | if (method_exists($this, 'generateSuccessRedirect')) { |
||
185 | //for 2.1 |
||
186 | return $this->generateSuccessRedirect($addForm); |
||
187 | } else { |
||
188 | //for 2.0 |
||
189 | $this->redirectSuccess($addForm); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @return mixed |
||
195 | */ |
||
196 | public function setDefaultAction() |
||
236 | |||
237 | /** |
||
238 | * @return mixed |
||
239 | */ |
||
240 | public function reassignAction() |
||
287 | |||
288 | /** |
||
289 | * @param int $rewriteId |
||
290 | * @param string $newView |
||
291 | * @param int $newViewId |
||
292 | */ |
||
293 | protected function allReassign($rewriteId, $newView, $newViewId) |
||
322 | |||
323 | /** |
||
324 | * @param int $rewriteId |
||
325 | * @param string $newView |
||
326 | * @param int $newViewId |
||
327 | */ |
||
328 | protected function simpleReassign($rewriteId, $newView, $newViewId) |
||
376 | |||
377 | /** |
||
378 | * @return mixed|\Thelia\Core\HttpFoundation\Response |
||
379 | */ |
||
380 | public function existAction() |
||
381 | { |
||
382 | if (null !== $response = $this->checkAuth(array(AdminResources::MODULE), 'RewriteUrl', AccessManager::VIEW)) { |
||
383 | return $response; |
||
384 | } |
||
385 | |||
386 | $search = $this->getRequest()->query->get('q'); |
||
387 | |||
388 | $rewritingUrl = RewritingUrlQuery::create() |
||
389 | ->filterByView(RewriteUrl::getUnknownSources(), Criteria::NOT_IN) |
||
390 | ->findOneByUrl($search); |
||
391 | |||
392 | if ($rewritingUrl !== null) { |
||
393 | if (!in_array($rewritingUrl->getView(), $this->correspondence)) { |
||
394 | return $this->jsonResponse(json_encode(false)); |
||
395 | } |
||
396 | |||
397 | $rewritingUrlArray = ["reassignUrl" => $this->generateUrlByRewritingUrl($rewritingUrl)]; |
||
398 | |||
399 | return $this->jsonResponse(json_encode($rewritingUrlArray)); |
||
400 | } else { |
||
401 | return $this->jsonResponse(json_encode(false)); |
||
402 | } |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @return mixed|\Thelia\Core\HttpFoundation\Response |
||
407 | * @throws \Propel\Runtime\Exception\PropelException |
||
408 | */ |
||
409 | public function searchAction() |
||
465 | |||
466 | /** |
||
467 | * @param RewritingUrl $rewritingUrl |
||
468 | * @return null|string url |
||
469 | */ |
||
470 | protected function generateUrlByRewritingUrl(RewritingUrl $rewritingUrl) |
||
485 | } |
||
486 |