| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PRReviewWatcher\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Silex\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PRReviewWatcher\Entity\Credential; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PRReviewWatcher\Form\Type\CredentialType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use GuzzleHttp\Exception\ClientException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class CredentialController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function indexAction(Application $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $credentials = $app['credential_repository']->findAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         return $app['twig']->render('credList.html.twig', array('credentials' => $credentials)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 21 |  | View Code Duplication |     public function addCredentialAction(Request $request, Application $app) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $credential     = new Credential(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $credentialForm = $app['form.factory']->create(new CredentialType(), $credential, ['mapped' => true, 'disable' => false]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $credentialForm->handleRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         if ($credentialForm->isSubmitted() && $credentialForm->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $token      = $credential->getToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             $name       = $credential->getNameCred(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 $client   = new Client(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 $res      = $client->get('https://api.github.com/user', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                     'auth' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                         'token', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                         $token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 $status   = $res->getStatusCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 $testName = json_decode($res->getBody()->getContents(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             } catch (ClientException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 $status = $e->getResponse()->getStatusCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             if ($status == 200) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 if ($name == $testName['login']) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     $app['credential_repository']->save($credential); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                     $app['session']->getFlashBag()->add('success', 'The credential was successfully created.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     $app['session']->getFlashBag()->add('danger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                         'This is not the user assigned at this token.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $app['session']->getFlashBag()->add('danger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     'The token is incorect. (GitHub API error ' . $status . ')'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return $app['twig']->render('credList_form.html.twig', array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'title'          => 'New credential', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'legend'         => 'New credential', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'credentialForm' => $credentialForm->createView(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 66 |  | View Code Duplication |     public function editCredentialAction($id, Request $request, Application $app) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $credential     = $app['credential_repository']->find($id); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         $credentialForm = $app['form.factory']->create(new CredentialType(), $credential, ['mapped' => false, 'disable' => true]); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $credentialForm->handleRequest($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         if ($credentialForm->isSubmitted() && $credentialForm->isValid()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             $token      = $credential->getToken(); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             $name       = $credential->getNameCred(); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             try { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                 $client   = new Client(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 $res      = $client->get('https://api.github.com/user', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                     'auth' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                         'token', | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                         $token | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                     ] | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                 ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                 $status   = $res->getStatusCode(); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |                 $testName = json_decode($res->getBody()->getContents(), true); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             } catch (ClientException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |                 $status = $e->getResponse()->getStatusCode(); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             if ($status == 200) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                 if ($name == $testName['login']) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |                     $app['credential_repository']->save($credential); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                     $app['session']->getFlashBag()->add('success', 'The credential was successfully updated.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                 } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |                     $app['session']->getFlashBag()->add('danger', | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                         'This is not the user assigned at this token.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |                 $app['session']->getFlashBag()->add('danger', | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |                     'The token is incorect. (GitHub API error ' . $status . ')'); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         return $app['twig']->render('credList_form.html.twig', array( | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             'title'          => 'Edit credential', | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             'legend'         => 'Edit credential', | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             'credentialForm' => $credentialForm->createView(), | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     public function deleteCredentialAction($id, Application $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $app['credential_repository']->delete($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $app['session']->getFlashBag()->add('success', 'The credential was successfully removed.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return $app->redirect('/admin/credential'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.