Conditions | 46 |
Paths | > 20000 |
Total Lines | 425 |
Lines | 6 |
Ratio | 1.41 % |
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 |
||
99 | public function editGame($templatePath, $formId) |
||
100 | { |
||
101 | // We try to get FB pages from the logged in user |
||
102 | $session = new Container('facebook'); |
||
103 | $config = $this->getServiceLocator()->get('config'); |
||
104 | $appsArray = []; |
||
105 | $platformFbAppId = ''; |
||
106 | |||
107 | if (isset($config['facebook'])) { |
||
108 | $platformFbAppId = $config['facebook']['fb_appid']; |
||
109 | $platformFbAppSecret = $config['facebook']['fb_secret']; |
||
110 | } |
||
111 | $fb = new \Facebook\Facebook([ |
||
112 | 'app_id' => $platformFbAppId, |
||
113 | 'app_secret' => $platformFbAppSecret, |
||
114 | 'default_graph_version' => 'v3.1', |
||
115 | ]); |
||
116 | |||
117 | $helper = $fb->getRedirectLoginHelper(); |
||
118 | $fb_args_param = array('req_perms' => 'manage_pages,publish_pages'); |
||
119 | $fb_login_url = $helper->getLoginUrl($this->adminUrl()->fromRoute( |
||
120 | 'playgroundgame/list', |
||
121 | array(), |
||
122 | array('force_canonical' => true) |
||
123 | ), $fb_args_param); |
||
124 | $accessToken = $helper->getAccessToken(); |
||
125 | |||
126 | if (isset($accessToken) || $session->offsetExists('fb_token')) { |
||
127 | if (isset($accessToken)) { |
||
128 | $session->offsetSet('fb_token', $accessToken); |
||
129 | } |
||
130 | |||
131 | // checking if user access token is not valid then ask user to login again |
||
132 | $debugToken = $fb->get('/debug_token?input_token='. $session->offsetGet('fb_token'), $platformFbAppId . '|' . $platformFbAppSecret) |
||
133 | ->getGraphNode() |
||
134 | ->asArray(); |
||
135 | if (isset($debugToken['error']['code'])) { |
||
136 | $session->offsetUnset('fb_token'); |
||
137 | } else { |
||
138 | // setting default user access token for future requests |
||
139 | $fb->setDefaultAccessToken($session->offsetGet('fb_token')); |
||
140 | $pages = $fb->get('/me/accounts') |
||
141 | ->getGraphEdge() |
||
142 | ->asArray(); |
||
143 | |||
144 | foreach ($pages as $key) { |
||
145 | $app_label = ''; |
||
146 | if (isset($key['name'])) { |
||
147 | $app_label .= $key['name']; |
||
148 | } |
||
149 | if (isset($key['id'])) { |
||
150 | $app_label .= ' ('.$key['id'].')'; |
||
151 | } |
||
152 | $appsArray[$key['id']] = $app_label; |
||
153 | } |
||
154 | $fb_login_url = ''; |
||
155 | |||
156 | if ($this->getRequest()->isPost()) { |
||
157 | $data = array_replace_recursive( |
||
158 | $this->getRequest()->getPost()->toArray(), |
||
159 | $this->getRequest()->getFiles()->toArray() |
||
160 | ); |
||
161 | // Removing a previously page tab set on this game |
||
162 | if ($this->game && |
||
163 | !empty($this->game->getFbPageId()) && |
||
164 | !empty($this->game->getFbAppId()) && |
||
165 | ( |
||
166 | ( |
||
167 | $this->game->getFbPageId() !== $data['fbPageId'] || |
||
168 | $this->game->getFbAppId() !== $data['fbAppId'] |
||
169 | ) || |
||
170 | $data['broadcastFacebook'] == 0 |
||
171 | ) |
||
172 | ) { |
||
173 | $oldPage = $fb->get('/' . $this->game->getFbPageId() . '?fields=access_token,name,id') |
||
174 | ->getGraphNode() |
||
175 | ->asArray(); |
||
176 | $removeTab = $fb->delete( |
||
177 | '/' . $this->game->getFbPageId() . '/tabs', |
||
178 | [ |
||
179 | 'tab' => 'app_'.$this->game->getFbAppId(), |
||
180 | ], |
||
181 | $oldPage['access_token'] |
||
182 | ) |
||
183 | ->getGraphNode() |
||
184 | ->asArray(); |
||
185 | } |
||
186 | |||
187 | // Removing a previously post set on this game |
||
188 | if ($this->game && |
||
189 | !empty($this->game->getFbPostId()) && |
||
190 | $data['broadcastPostFacebook'] == 0 |
||
191 | ) { |
||
192 | $oldPage = $fb->get('/' . $this->game->getFbPageId() . '?fields=access_token,name,id') |
||
193 | ->getGraphNode() |
||
194 | ->asArray(); |
||
195 | $removePost = $fb->delete( |
||
196 | '/' . $this->game->getFbPostId(), |
||
197 | [], |
||
198 | $oldPage['access_token'] |
||
199 | ) |
||
200 | ->getGraphNode() |
||
201 | ->asArray(); |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | |||
207 | $viewModel = new ViewModel(); |
||
208 | $viewModel->setTemplate($templatePath); |
||
209 | |||
210 | $gameForm = new ViewModel(); |
||
211 | $gameForm->setTemplate('playground-game/game/game-form'); |
||
212 | |||
213 | $form = $this->getServiceLocator()->get($formId); |
||
214 | $form->setAttribute( |
||
215 | 'action', |
||
216 | $this->adminUrl()->fromRoute( |
||
217 | 'playgroundgame/edit-' . $this->game->getClassType(), |
||
218 | array('gameId' => $this->game->getId()) |
||
219 | ) |
||
220 | ); |
||
221 | $form->setAttribute('method', 'post'); |
||
222 | |||
223 | $pageIds = $form->get('fbPageId')->getOption('value_options'); |
||
224 | foreach ($appsArray as $k => $v) { |
||
225 | $pageIds[$k] = $v; |
||
226 | } |
||
227 | $form->get('fbPageId')->setAttribute('options', $pageIds); |
||
228 | |||
229 | //if($form->get('fbAppId')->getValue() == '') { |
||
230 | $form->get('fbAppId')->setValue($platformFbAppId); |
||
231 | //} |
||
232 | |||
233 | // if ($this->game->getFbAppId()) { |
||
234 | // $data['fbAppId'] = $form->get('fbAppId')->getOption('value_options'); |
||
235 | // $appIds[$this->game->getFbAppId()] = $this->game->getFbAppId(); |
||
236 | // $form->get('fbAppId')->setAttribute('options', $appIds); |
||
237 | // } |
||
238 | |||
239 | $gameOptions = $this->getAdminGameService()->getOptions(); |
||
240 | $gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $this->game->getId(). '.css'; |
||
241 | View Code Duplication | if (is_file($gameStylesheet)) { |
|
242 | $values = $form->get('stylesheet')->getValueOptions(); |
||
243 | $values[$gameStylesheet] = 'Style personnalisé de ce jeu'; |
||
244 | |||
245 | $form->get('stylesheet')->setAttribute('options', $values); |
||
246 | } |
||
247 | |||
248 | $form->bind($this->game); |
||
249 | |||
250 | if ($this->getRequest()->isPost()) { |
||
251 | $data = array_replace_recursive( |
||
252 | $this->getRequest()->getPost()->toArray(), |
||
253 | $this->getRequest()->getFiles()->toArray() |
||
254 | ); |
||
255 | if (empty($data['prizes'])) { |
||
256 | $data['prizes'] = array(); |
||
257 | } |
||
258 | if (isset($data['drawDate']) && $data['drawDate']) { |
||
259 | $data['drawDate'] = \DateTime::createFromFormat('d/m/Y', $data['drawDate']); |
||
260 | } |
||
261 | |||
262 | $game = $this->getAdminGameService()->createOrUpdate($data, $this->game, $formId); |
||
263 | |||
264 | if ($game) { |
||
265 | if ($session->offsetExists('fb_token')) { |
||
266 | if (!empty($data['fbPageId']) && !empty($data['fbAppId'])) { |
||
267 | $page = $fb->get('/' . $data['fbPageId'] . '?fields=access_token,name,id') |
||
268 | ->getGraphNode() |
||
269 | ->asArray(); |
||
270 | |||
271 | // let's create a post on FB |
||
272 | if ($data['broadcastPostFacebook'] && $game->getWelcomeBlock() != '' && $game->getMainImage() != '') { |
||
273 | $imgPath = $this->url()->fromRoute('frontend', [], ['force_canonical' => true], false).$game->getMainImage(); |
||
274 | // emoticons : $emoji = html_entity_decode('😈'); |
||
275 | |||
276 | $message = str_replace('<p>', "", $game->getWelcomeBlock()); |
||
277 | $message = str_replace('</p>', "\n", $message); |
||
278 | $message = strip_tags($message); |
||
279 | |||
280 | // Create the post |
||
281 | try { |
||
282 | // Associate the fbAppId to the page so that we can receive the webhooks |
||
283 | $linkAppToPage = $fb->post( |
||
284 | '/' . $page['id'] . '/subscribed_apps', |
||
285 | array(), |
||
286 | $page['access_token'] |
||
287 | ); |
||
288 | |||
289 | /** |
||
290 | * post text and save the post_id to be able to get the likes and comments on the post |
||
291 | */ |
||
292 | // $post = $fb->post( |
||
293 | // '/' . $page['id'] . '/feed', |
||
294 | // array( |
||
295 | // 'message' => 'message', |
||
296 | // ), |
||
297 | // $page['access_token'] |
||
298 | // ); |
||
299 | |||
300 | /** |
||
301 | * Post a photo |
||
302 | */ |
||
303 | // $post = $fb->post( |
||
304 | // '/' . $page['id'] . '/photos', |
||
305 | // array( |
||
306 | // 'url' => 'https://images.unsplash.com/photo-1538239010247-383da61e35db?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=22e9de10cd7e4d8e32d698099dc6d23c&auto=format&fit=crop&w=3289&q=80', |
||
307 | // 'published' => true, |
||
308 | // ), |
||
309 | // $page['access_token'] |
||
310 | // ); |
||
311 | |||
312 | /** |
||
313 | * Upload an unpublished photo and include it in a post |
||
314 | */ |
||
315 | $img = $fb->post( |
||
316 | '/' . $page['id'] . '/photos', |
||
317 | array( |
||
318 | 'url' => $imgPath, |
||
319 | 'published' => false, |
||
320 | ), |
||
321 | $page['access_token'] |
||
322 | ); |
||
323 | $img = $img->getGraphNode()->asArray(); |
||
324 | |||
325 | if ($game->getFbPostId() != '') { |
||
326 | $post = $fb->post( |
||
327 | '/' . $game->getFbPostId(), |
||
328 | array( |
||
329 | 'message' => $message, |
||
330 | 'attached_media[0]' => '{"media_fbid":"'.$img['id'].'"}', |
||
331 | ), |
||
332 | $page['access_token'] |
||
333 | ); |
||
334 | } else { |
||
335 | $post = $fb->post( |
||
336 | '/' . $page['id'] . '/feed', |
||
337 | array( |
||
338 | 'message' => $message, |
||
339 | 'attached_media[0]' => '{"media_fbid":"'.$img['id'].'"}', |
||
340 | ), |
||
341 | $page['access_token'] |
||
342 | ); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Upload an unpublished photo and include it in a scheduled post |
||
347 | */ |
||
348 | // $img = $fb->post( |
||
349 | // '/' . $page['id'] . '/photos', |
||
350 | // array( |
||
351 | // 'url' => 'https://images.unsplash.com/photo-1538239010247-383da61e35db?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=22e9de10cd7e4d8e32d698099dc6d23c&auto=format&fit=crop&w=3289&q=80', |
||
352 | // 'published' => false, |
||
353 | // 'temporary' => true |
||
354 | // ), |
||
355 | // $page['access_token'] |
||
356 | // ); |
||
357 | // $img = $img->getGraphNode()->asArray(); |
||
358 | // |
||
359 | // $post = $fb->post( |
||
360 | // '/' . $page['id'] . '/feed', |
||
361 | // array( |
||
362 | // 'message' => 'message avec image', |
||
363 | // 'attached_media[0]' => '{"media_fbid":"'.$img['id'].'"}', |
||
364 | // 'published' => false, |
||
365 | // 'scheduled_publish_time' => '1512068400', |
||
366 | // 'unpublished_content_type' => 'SCHEDULED', |
||
367 | // ), |
||
368 | // $page['access_token'] |
||
369 | // ); |
||
370 | |||
371 | /** |
||
372 | * publish multiple photos then associate these photos to a post |
||
373 | */ |
||
374 | // $endpoint = "/".$page['id']."/photos"; |
||
375 | // $multiple_photos = [ |
||
376 | // 'https://images.unsplash.com/photo-1538239010247-383da61e35db?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=22e9de10cd7e4d8e32d698099dc6d23c&auto=format&fit=crop&w=3289&q=80', |
||
377 | // 'https://images.unsplash.com/photo-1538218952949-2f5dda4a9156?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=b79a9c7314dd5ca8eac2f187902ceca2&auto=format&fit=crop&w=2704&q=80', |
||
378 | // 'https://images.unsplash.com/photo-1538157245064-badfdabb7142?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=dfa50d5dd51b85f25ca03f2b2667752a&auto=format&fit=crop&w=2700&q=80', |
||
379 | // ]; |
||
380 | // $photos = []; |
||
381 | // $data_post = ['attached_media' => [], 'message' => 'message', 'published' => true]; |
||
382 | // foreach ($multiple_photos as $file_url): |
||
383 | // array_push($photos, $fb->request('POST',$endpoint,['url' =>$file_url,'published' => false,'temporary' => true], $page['access_token'])); |
||
384 | // endforeach; |
||
385 | // $uploaded_photos = $fb->sendBatchRequest($photos, $page['access_token']); |
||
386 | // $uploaded_photos = $uploaded_photos->getGraphNode()->asArray(); |
||
387 | |||
388 | // foreach ($uploaded_photos as $photo): |
||
389 | // $photo = json_decode($photo['body']); |
||
390 | // array_push($data_post['attached_media'], '{"media_fbid":"'.$photo->id.'"}'); |
||
391 | // endforeach; |
||
392 | // $post = $fb->sendRequest('POST', "/".$page['id']."/feed", $data_post, $page['access_token']); |
||
393 | |||
394 | /** |
||
395 | * publish a carrousel to a post |
||
396 | */ |
||
397 | // $data_post = [ |
||
398 | // 'child_attachments' => [], |
||
399 | // 'message' => 'message', |
||
400 | // 'link' => 'https://www.playground.gg', |
||
401 | // 'multi_share_end_card' => false, |
||
402 | // 'published' => true, |
||
403 | // ]; |
||
404 | |||
405 | // $multiple_photos = [ |
||
406 | // 'https://images.unsplash.com/photo-1538239010247-383da61e35db?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=22e9de10cd7e4d8e32d698099dc6d23c&auto=format&fit=crop&w=3289&q=80', |
||
407 | // 'https://images.unsplash.com/photo-1538218952949-2f5dda4a9156?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=b79a9c7314dd5ca8eac2f187902ceca2&auto=format&fit=crop&w=2704&q=80', |
||
408 | // 'https://images.unsplash.com/photo-1538157245064-badfdabb7142?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=dfa50d5dd51b85f25ca03f2b2667752a&auto=format&fit=crop&w=2700&q=80', |
||
409 | // ]; |
||
410 | // foreach ($multiple_photos as $k => $photo): |
||
411 | // array_push($data_post['child_attachments'], '{"link":"'.$photo.'", "name": "message_'.$k.'"}'); |
||
412 | // endforeach; |
||
413 | // $post = $fb->sendRequest('POST', "/".$page['id']."/feed", $data_post, $page['access_token']); |
||
414 | |||
415 | /** Texte avec lien vers une page |
||
416 | * |
||
417 | */ |
||
418 | // $post = $fb->post( |
||
419 | // '/' . $page['id'] . '/feed', |
||
420 | // array( |
||
421 | // 'message' => 'message', |
||
422 | // 'link' => 'https://images.unsplash.com/photo-1538239010247-383da61e35db?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=22e9de10cd7e4d8e32d698099dc6d23c&auto=format&fit=crop&w=3289&q=80', |
||
423 | // //'picture' => 'https://images.unsplash.com/photo-1538239010247-383da61e35db?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=22e9de10cd7e4d8e32d698099dc6d23c&auto=format&fit=crop&w=3289&q=80', |
||
424 | // 'call_to_action' => '{"type":"BOOK_TRAVEL","value":{"link":"https://images.unsplash.com/photo-1538239010247-383da61e35db?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=22e9de10cd7e4d8e32d698099dc6d23c&auto=format&fit=crop&w=3289&q=80"}}', |
||
425 | // ), |
||
426 | // $page['access_token'] |
||
427 | // ); |
||
428 | } catch (\Exception $e) { |
||
429 | if ($e->getMessage() == 'Missing or invalid image file') { |
||
430 | if ($game->getFbPostId() != '') { |
||
431 | $post = $fb->post( |
||
432 | '/' . $game->getFbPostId(), |
||
433 | array( |
||
434 | 'message' => $message, |
||
435 | ), |
||
436 | $page['access_token'] |
||
437 | ); |
||
438 | } else { |
||
439 | $post = $fb->post( |
||
440 | '/' . $page['id'] . '/feed', |
||
441 | array( |
||
442 | 'message' => $message, |
||
443 | ), |
||
444 | $page['access_token'] |
||
445 | ); |
||
446 | } |
||
447 | } else { |
||
448 | throw $e; |
||
449 | } |
||
450 | } |
||
451 | $post = $post->getGraphNode()->asArray(); |
||
452 | if (isset($post['id'])) { |
||
453 | $game->setFbPostId($post['id']); |
||
454 | $game = $this->getAdminGameService()->getGameMapper()->update($game); |
||
455 | } |
||
456 | } |
||
457 | |||
458 | // Let's record the FB page tab if it is configured |
||
459 | // adding page tab to selected page using page access token |
||
460 | if ($data['broadcastFacebook']) { |
||
461 | try { |
||
462 | $addTab = $fb->post( |
||
463 | '/' . $page['id'] . '/tabs', |
||
464 | [ |
||
465 | 'app_id' => $data['fbAppId'], |
||
466 | 'custom_name' => (!empty($data['fbPageTabTitle'])) ? $data['fbPageTabTitle'] : $data['title'], |
||
467 | 'custom_image_url' => ($game->getFbPageTabImage() !== '') ? |
||
468 | $this->getAdminGameService()->getServiceManager()->get('ViewRenderer')->url( |
||
469 | 'frontend', |
||
470 | array(), |
||
471 | array('force_canonical' => true) |
||
472 | ).$game->getFbPageTabImage() : |
||
473 | null, |
||
474 | 'position' => (!empty($data['fbPageTabPosition'])) ? $data['fbPageTabPosition'] : 99 |
||
475 | ], |
||
476 | $page['access_token'] |
||
477 | ) |
||
478 | ->getGraphNode() |
||
479 | ->asArray(); |
||
480 | } catch (\Exception $e) { |
||
481 | // (#324) Missing or invalid image file |
||
482 | if ($e->getCode() == '324') { |
||
483 | try { |
||
484 | $addTab = $fb->post( |
||
485 | '/' . $page['id'] . '/tabs', |
||
486 | [ |
||
487 | 'app_id' => $data['fbAppId'], |
||
488 | 'custom_name' => (!empty($data['fbPageTabTitle'])) ? $data['fbPageTabTitle'] : $data['title'], |
||
489 | 'position' => (!empty($data['fbPageTabPosition'])) ? $data['fbPageTabPosition'] : 99 |
||
490 | ], |
||
491 | $page['access_token'] |
||
492 | ) |
||
493 | ->getGraphNode() |
||
494 | ->asArray(); |
||
495 | } catch (\Exception $e) { |
||
496 | throw $e; |
||
497 | } |
||
498 | } |
||
499 | } |
||
500 | } |
||
501 | } |
||
502 | } |
||
503 | return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/list')); |
||
504 | } |
||
505 | } |
||
506 | |||
507 | $gameForm->setVariables( |
||
508 | array( |
||
509 | 'platform_fb_app_id' => $platformFbAppId, |
||
510 | 'fb_login_url' => $fb_login_url, |
||
511 | 'form' => $form, |
||
512 | 'game' => $this->game |
||
513 | ) |
||
514 | ); |
||
515 | $viewModel->addChild($gameForm, 'game_form'); |
||
516 | |||
517 | return $viewModel->setVariables( |
||
518 | array( |
||
519 | 'form' => $form, |
||
520 | 'title' => 'Edit this game', |
||
521 | ) |
||
522 | ); |
||
523 | } |
||
524 | |||
904 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..