Complex classes like ShortListController 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 ShortListController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class ShortListController extends Page_Controller |
||
4 | { |
||
5 | private static $allowed_actions = array( |
||
|
|||
6 | 'renderList', |
||
7 | 'performAction' |
||
8 | ); |
||
9 | |||
10 | private static $url_handlers = array( |
||
11 | 'add' => 'performAction', |
||
12 | 'remove' => 'performAction', |
||
13 | '$URL!' => 'renderList', |
||
14 | ); |
||
15 | |||
16 | private static $extensions = array( |
||
17 | 'ShortListPaginationExtension' |
||
18 | ); |
||
19 | |||
20 | public function init() |
||
30 | |||
31 | /** |
||
32 | * When landing on the homepage, if there is a shortlist for the current |
||
33 | * user, redirect to the correct URL. Otherwise, 404. |
||
34 | * */ |
||
35 | public function index($request) |
||
36 | { |
||
37 | $shortlist = $this->getSessionShortList(); |
||
38 | |||
39 | if (!empty($shortlist)) { |
||
40 | return $this->redirect(Config::inst()->get('ShortList', 'URLSegment') . $shortlist->URL); |
||
41 | } else { |
||
42 | if (!$shortlist || !$shortlist->exists()) { |
||
43 | $shortlist = new ShortList(); |
||
44 | $shortlist->write(); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | // render with empty template. |
||
49 | return $this->renderWith(array('Page', 'ShortList_empty')); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Get the absolute URL of this controller. |
||
54 | * */ |
||
55 | public function Link($action = null) |
||
56 | { |
||
57 | $shortlist = $this->getSessionShortList(); |
||
58 | $url = Config::inst()->get('ShortList', 'URLSegment'); |
||
59 | |||
60 | if ($shortlist) { |
||
61 | $url .= $shortlist->URL; |
||
62 | } |
||
63 | |||
64 | return $url; |
||
65 | } |
||
66 | |||
67 | public function renderList($request) |
||
68 | { |
||
69 | $shortlist = DataObject::get_one('ShortList', $filter = array('URL' => $request->param('URL'))); |
||
70 | $link = false; |
||
71 | $count = 0; |
||
72 | |||
73 | if ($this->dontRender($shortlist, $request)) { |
||
74 | return $this->httpError(404); |
||
75 | } |
||
76 | |||
77 | if ($shortlist && $shortlist->exists()) { |
||
78 | $link = $shortlist->Link(); |
||
79 | $count = $shortlist->ShortListItems()->Count(); |
||
80 | } |
||
81 | |||
82 | return $this->customise(array( |
||
83 | 'ShortlistURL' => $link, |
||
84 | 'ShortlistCount' => $count |
||
85 | ))->renderWith( |
||
86 | array('ShortList', 'Page') |
||
87 | ); |
||
88 | } |
||
89 | |||
90 | public function performAction($request) |
||
91 | { |
||
92 | if ($this->dontPerformAction($request)) { |
||
93 | return $this->httpError(404); |
||
94 | } |
||
95 | |||
96 | $action = $this->determineAction($request->getURL()); |
||
97 | |||
98 | $status = $action->performAction( |
||
99 | $shortlist = $this->getSessionShortList(), |
||
100 | $ID = $request->getVar('id'), |
||
101 | $type = $request->getVar('type'), |
||
102 | $session = $request->getVar('s') |
||
103 | ); |
||
104 | |||
105 | if ($request->isAjax()) { |
||
106 | return $this->renderAjax($session); |
||
107 | } |
||
108 | |||
109 | if (array_key_exists('output', $request->getVars())) { |
||
110 | return $status; |
||
111 | } |
||
112 | |||
113 | return $this->redirectBack(); |
||
114 | } |
||
115 | |||
116 | |||
117 | /** |
||
118 | * Get the number of items in the current short list. |
||
119 | * |
||
120 | * @param session The session to check & find a shortlist for. |
||
121 | * @return mixed false if no session exists - else the number of items in the shortlist. |
||
122 | * */ |
||
123 | public function shortListCount($session = false) |
||
124 | { |
||
125 | if ($this->isSessionValid($session)) { |
||
126 | return false; |
||
127 | } |
||
128 | |||
129 | $shortlist = $this->getSessionShortList(); |
||
130 | |||
131 | if (!$shortlist || !$shortlist->exists()) { |
||
132 | return 0; |
||
133 | } |
||
134 | |||
135 | return $shortlist->Items()->count(); |
||
136 | } |
||
137 | |||
138 | public static function getShortListSession() |
||
139 | { |
||
140 | return DataObject::get_one('ShortList', $filter = array('SessionID' => self::getSecurityToken())); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get the token to use to add/remove from shortlist. |
||
145 | * */ |
||
146 | public static function getSecurityToken() |
||
147 | { |
||
148 | return Utilities::getSecurityToken(); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Determine the action based upon the url requested. |
||
153 | * */ |
||
154 | private function determineAction($url) |
||
155 | { |
||
156 | $matches = array(); |
||
157 | preg_match('/remove|add/', $url, $matches); |
||
158 | |||
159 | switch ($matches[0]) { |
||
160 | case 'remove': |
||
161 | return new RemoveFromshortlistAction(); |
||
162 | case 'add': |
||
163 | return new AddToshortlistAction(); |
||
164 | default: |
||
165 | return null; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Return a valid shortlist - or null. |
||
171 | * */ |
||
172 | private function getSessionShortList() |
||
173 | { |
||
174 | return DataObject::get_one('ShortList', |
||
175 | $filter = array('SessionID' => self::getSecurityToken()), |
||
176 | $cache = false |
||
177 | ); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Return the json encoded count & url for the current session |
||
182 | * */ |
||
183 | private function renderAjax($session) |
||
184 | { |
||
185 | $shortlist = $this->getSessionShortList(); |
||
186 | $url = false; |
||
187 | |||
188 | if ($shortlist && $shortlist->exists()) { |
||
189 | $url = $shortlist->Link(); |
||
190 | } |
||
191 | |||
192 | return json_encode(array( |
||
193 | 'count' => $this->shortListCount($session), |
||
194 | 'url' => $url |
||
195 | )); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Don't render the template! |
||
200 | * */ |
||
201 | private function dontRender($shortlist, $request) |
||
202 | { |
||
203 | return is_null(self::getSecurityToken()) || !$request->param('URL') || !$shortlist || !$shortlist->exists(); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Is this session valid? |
||
208 | * */ |
||
209 | private function isSessionValid($session) |
||
210 | { |
||
211 | return is_null(self::getSecurityToken()) || !$session || $session != self::getSecurityToken(); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Don't perform an action. |
||
216 | * */ |
||
217 | private function dontPerformAction($request) |
||
222 | } |
||
223 |