Total Complexity | 52 |
Total Lines | 440 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ApplyController 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 ApplyController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class ApplyController extends AbstractActionController implements ContainerAwareInterface |
||
47 | { |
||
48 | |||
49 | protected $formContainer; |
||
50 | |||
51 | protected $config; |
||
52 | |||
53 | protected $imageCacheManager; |
||
54 | |||
55 | protected $validator; |
||
56 | |||
57 | protected $repositories; |
||
58 | |||
59 | protected $appEvents; |
||
60 | |||
61 | protected $viewHelper; |
||
62 | |||
63 | /** |
||
64 | * @param ContainerInterface $container |
||
65 | * |
||
66 | * @return ApplyController |
||
67 | */ |
||
68 | static public function factory(ContainerInterface $container) |
||
69 | { |
||
70 | $ob = new self(); |
||
71 | $ob->setContainer($container); |
||
72 | return $ob; |
||
73 | } |
||
74 | |||
75 | public function setContainer( ContainerInterface $container ) |
||
76 | { |
||
77 | $this->config = $container->get('Config'); |
||
78 | $this->imageCacheManager = $container->get('Organizations\ImageFileCache\Manager'); |
||
79 | $this->validator = $container->get('ValidatorManager'); |
||
80 | $this->repositories = $container->get('repositories'); |
||
81 | $this->appEvents = $container->get('Applications/Events'); |
||
82 | $this->viewHelper = $container->get('ViewHelperManager'); |
||
83 | } |
||
84 | |||
85 | |||
86 | public function attachDefaultListeners() |
||
87 | { |
||
88 | parent::attachDefaultListeners(); |
||
89 | $events = $this->getEventManager(); |
||
90 | $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 10); |
||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | public function preDispatch(MvcEvent $e) |
||
95 | { |
||
96 | /* @var $application \Applications\Entity\Application */ |
||
97 | if ($this->params()->fromQuery('do')) { |
||
98 | $e->getRouteMatch()->setParam('action', 'do'); |
||
99 | return; |
||
100 | } |
||
101 | |||
102 | /* @var $request \Zend\Http\Request */ |
||
103 | /* @var $repository \Applications\Repository\Application */ |
||
104 | /* @var $container \Applications\Form\Apply */ |
||
105 | $request = $this->getRequest(); |
||
106 | $services = $e->getApplication()->getServiceManager(); |
||
107 | $repositories = $services->get('repositories'); |
||
108 | $repository = $repositories->get('Applications/Application'); |
||
109 | $container = $services->get('forms')->get('Applications/Apply'); |
||
110 | |||
111 | if ($request->isPost()) { |
||
|
|||
112 | $appId = $this->params()->fromPost('applicationId'); |
||
113 | if (!$appId) { |
||
114 | throw new \RuntimeException('Missing application id.'); |
||
115 | } |
||
116 | $routeMatch = $e->getRouteMatch(); |
||
117 | |||
118 | if ('recruiter-preview' == $appId) { |
||
119 | $routeMatch->setParam('action', 'process-preview'); |
||
120 | return; |
||
121 | } |
||
122 | |||
123 | $application = $repository->find($appId); |
||
124 | if (!$application) { |
||
125 | throw new \RuntimeException('Invalid application id.'); |
||
126 | } |
||
127 | |||
128 | $action = 'process'; |
||
129 | |||
130 | $routeMatch->setParam('action', $action); |
||
131 | } else { |
||
132 | $user = $this->auth()->getUser(); |
||
133 | $appId = $this->params('applyId'); |
||
134 | if (!$appId) { |
||
135 | throw new \RuntimeException('Missing apply id'); |
||
136 | } |
||
137 | |||
138 | /* @var \Jobs\Entity\Job $job */ |
||
139 | $job = $repositories->get('Jobs/Job')->findOneByApplyId($appId); |
||
140 | |||
141 | if (!$job) { |
||
142 | $e->getRouteMatch()->setParam('action', 'job-not-found'); |
||
143 | return; |
||
144 | } |
||
145 | |||
146 | switch ($job->getStatus()) { |
||
147 | case \Jobs\Entity\Status::ACTIVE: |
||
148 | break; |
||
149 | default: |
||
150 | $e->getRouteMatch()->setParam('action', 'job-not-found'); |
||
151 | return; |
||
152 | break; |
||
153 | } |
||
154 | |||
155 | if ($user === $job->getUser()) { |
||
156 | $application = new \Applications\Entity\Application(); |
||
157 | $application->setContact(new Contact()); |
||
158 | $application->setJob($job); |
||
159 | $application->setId('recruiter-preview'); |
||
160 | } else { |
||
161 | $subscriberUri = $this->params()->fromQuery('subscriber'); |
||
162 | $application = $repository->findDraft($user, $appId); |
||
163 | |||
164 | if ($application) { |
||
165 | /* @var $form \Auth\Form\UserInfo */ |
||
166 | $form = $container->getForm('contact.contact'); |
||
167 | $form->setDisplayMode('summary'); |
||
168 | |||
169 | if ($subscriberUri) { |
||
170 | $subscriber = $application->getSubscriber(); |
||
171 | if (!$subscriber || $subscriber->uri != $subscriberUri) { |
||
172 | $subscriber = $repositories->get('Applications/Subscriber')->findbyUri($subscriberUri, /*create*/ true); |
||
173 | $application->setSubscriber($subscriber); |
||
174 | $subscriber->getname(); |
||
175 | } |
||
176 | } |
||
177 | } else { |
||
178 | if (!$job) { |
||
179 | $e->getRouteMatch()->setParam('action', 'job-not-found'); |
||
180 | return; |
||
181 | } |
||
182 | if ($job->getUriApply()) { |
||
183 | return $this->redirect($job->getUriApply()); |
||
184 | } |
||
185 | |||
186 | /* @var $application \Applications\Entity\Application */ |
||
187 | $application = $repository->create(); |
||
188 | $application->setIsDraft(true) |
||
189 | ->setContact($user->getInfo()) |
||
190 | ->setUser($user) |
||
191 | ->setJob($job); |
||
192 | |||
193 | if ($subscriberUri) { |
||
194 | $subscriber = $repositories->get('Applications/Subscriber')->findbyUri($subscriberUri, /*create*/ true); |
||
195 | $application->setSubscriber($subscriber); |
||
196 | } |
||
197 | |||
198 | $repositories->store($application); |
||
199 | /* |
||
200 | * If we had copy an user image, we need to refresh its data |
||
201 | * to populate the length property. |
||
202 | */ |
||
203 | if ($image = $application->getContact()->getImage()) { |
||
204 | $repositories->refresh($image); |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | $container->setEntity($application); |
||
211 | $this->configureContainer($container); |
||
212 | $this->formContainer = $container; |
||
213 | } |
||
214 | |||
215 | public function jobNotFoundAction() |
||
216 | { |
||
217 | $this->response->setStatusCode(410); |
||
218 | $model = new ViewModel( |
||
219 | [ 'content' => /*@translate*/ 'Invalid apply id'] |
||
220 | ); |
||
221 | $model->setTemplate('applications/error/not-found'); |
||
222 | return $model; |
||
223 | } |
||
224 | |||
225 | public function indexAction() |
||
226 | { |
||
227 | /* @var \Applications\Form\Apply $form */ |
||
228 | $form = $this->formContainer; |
||
229 | $application = $form->getEntity(); /* @var \Applications\Entity\Application $application */ |
||
230 | |||
231 | $form->setParam('applicationId', $application->getId()); |
||
232 | |||
233 | $organizationImageCache = $this->imageCacheManager; |
||
234 | |||
235 | $model = new ViewModel( |
||
236 | [ |
||
237 | 'form' => $form, |
||
238 | 'isApplicationValid' => $this->checkApplication($application), |
||
239 | 'application' => $application, |
||
240 | 'organizationImageCache' => $organizationImageCache |
||
241 | ] |
||
242 | ); |
||
243 | $model->setTemplate('applications/apply/index'); |
||
244 | return $model; |
||
245 | } |
||
246 | |||
247 | public function oneClickApplyAction() |
||
248 | { |
||
249 | /* @var \Applications\Entity\Application $application */ |
||
250 | $application = $this->formContainer->getEntity(); |
||
251 | $job = $application->getJob(); |
||
252 | $atsMode = $job->getAtsMode(); |
||
253 | |||
254 | // check for one click apply |
||
255 | if (!($atsMode->isIntern() && $atsMode->getOneClickApply())) |
||
256 | { |
||
257 | // redirect to regular application |
||
258 | return $this->redirect() |
||
259 | ->toRoute('lang/apply', ['applyId' => $job->getApplyId()]); |
||
260 | } |
||
261 | |||
262 | $network = $this->params('network'); |
||
263 | |||
264 | $hybridAuth = $this->formContainer |
||
265 | ->get('HybridAuthAdapter') |
||
266 | ->getHybridAuth(); |
||
267 | /* @var $authProfile \Hybrid_User_Profile */ |
||
268 | $authProfile = $hybridAuth->authenticate($network) |
||
269 | ->getUserProfile(); |
||
270 | |||
271 | /* @var \Auth\Entity\SocialProfiles\AbstractProfile $profile */ |
||
272 | $profile = $this->plugin('Auth/SocialProfiles')->fetch($network); |
||
273 | |||
274 | $contact = $application->getContact(); |
||
275 | $contact->setEmail($authProfile->emailVerified ?: $authProfile->email); |
||
276 | $contact->setFirstName($authProfile->firstName); |
||
277 | $contact->setLastName($authProfile->lastName); |
||
278 | $contact->setBirthDay($authProfile->birthDay); |
||
279 | $contact->setBirthMonth($authProfile->birthMonth); |
||
280 | $contact->setBirthYear($authProfile->birthYear); |
||
281 | $contact->setPostalCode($authProfile->zip); |
||
282 | $contact->setCity($authProfile->city); |
||
283 | $contact->setStreet($authProfile->address); |
||
284 | $contact->setPhone($authProfile->phone); |
||
285 | $contact->setGender($authProfile->gender); |
||
286 | |||
287 | $profiles = $application->getProfiles(); |
||
288 | $profiles->add($profile); |
||
289 | |||
290 | $cv = $application->getCv(); |
||
291 | $cv->setEmployments($profile->getEmployments()); |
||
292 | $cv->setEducations($profile->getEducations()); |
||
293 | |||
294 | if ($authProfile->photoURL) |
||
295 | { |
||
296 | $response = (new \Zend\Http\Client($authProfile->photoURL, ['sslverifypeer' => false]))->send(); |
||
297 | $file = new \Doctrine\MongoDB\GridFSFile(); |
||
298 | $file->setBytes($response->getBody()); |
||
299 | |||
300 | $image = new \Applications\Entity\Attachment(); |
||
301 | $image->setName($contact->getLastName().$contact->getFirstName()); |
||
302 | $image->setType($response->getHeaders()->get('Content-Type')->getFieldValue()); |
||
303 | $image->setFile($file); |
||
304 | $image->setPermissions($application->getPermissions()); |
||
305 | |||
306 | $contact->setImage($image); |
||
307 | } |
||
308 | |||
309 | $urlOptions = []; |
||
310 | |||
311 | if ($this->params('immediately')) |
||
312 | { |
||
313 | $application->getAttributes()->setAcceptedPrivacyPolicy(true); |
||
314 | $urlOptions = [ |
||
315 | 'query' => [ |
||
316 | 'do' => 'send' |
||
317 | ] |
||
318 | ]; |
||
319 | } |
||
320 | |||
321 | return $this->redirect() |
||
322 | ->toRoute('lang/apply', ['applyId' => $job->getApplyId()], $urlOptions); |
||
323 | } |
||
324 | |||
325 | public function processPreviewAction() |
||
326 | { |
||
327 | return new JsonModel(array('valid' => false, 'errors' => array())); |
||
328 | } |
||
329 | |||
330 | public function processAction() |
||
331 | { |
||
332 | $params = $this->params(); |
||
333 | $formName = $params->fromQuery('form'); |
||
334 | $form = $this->formContainer->getForm($formName); |
||
335 | $postData = $form->getOption('use_post_array') ? $params->fromPost() : array(); |
||
336 | //@TODO: [ZF3] option use_files_array is false by default |
||
337 | //$filesData = $form->getOption('use_files_array') ? $params->fromFiles() : array(); |
||
338 | $form->setData(array_merge($postData,$_FILES)); |
||
339 | |||
340 | if (!$form->isValid()) { |
||
341 | return new JsonModel( |
||
342 | array( |
||
343 | 'valid' => false, |
||
344 | 'errors' => $form->getMessages(), |
||
345 | ) |
||
346 | ); |
||
347 | } |
||
348 | $application = $this->formContainer->getEntity(); |
||
349 | $this->repositories->store($application); |
||
350 | |||
351 | if ('file-uri' === $params->fromPost('return')) { |
||
352 | $basepath = $this->viewHelper->get('basepath'); |
||
353 | $content = $basepath($form->getHydrator()->getLastUploadedFile()->getUri()); |
||
354 | } else { |
||
355 | if ($form instanceof SummaryForm) { |
||
356 | $form->setRenderMode(SummaryForm::RENDER_SUMMARY); |
||
357 | $viewHelper = 'summaryForm'; |
||
358 | } else { |
||
359 | $viewHelper = 'form'; |
||
360 | } |
||
361 | $content = $this->viewHelper->get($viewHelper)->__invoke($form); |
||
362 | } |
||
363 | |||
364 | return new JsonModel( |
||
365 | array( |
||
366 | 'valid' => $form->isValid(), |
||
367 | 'content' => $content, |
||
368 | 'isApplicationValid' => $this->checkApplication($application) |
||
369 | ) |
||
370 | ); |
||
371 | } |
||
372 | |||
373 | public function doAction() |
||
450 | } |
||
451 | |||
452 | protected function checkApplication($application) |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * Configures the apply form container. |
||
460 | * |
||
461 | * Currently only disables elements. |
||
462 | * |
||
463 | * @param Container $container |
||
464 | */ |
||
465 | protected function configureContainer(Container $container) |
||
486 | } |
||
487 | } |
||
488 | } |
||
489 |