| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Copyright © 2017 Toan Nguyen. All rights reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Gojira\Jira\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Gojira\Api\Response\BaseResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Gojira\Api\Response\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Helper\TableCell; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Render result for JIRA REST /worklog | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @package Gojira\Jira\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author  Toan Nguyen <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 21 |  | View Code Duplication | class WorklogResponse extends IssueResponse implements ResponseInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function render($type = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         switch ($type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             case 'worklog:show': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 $result = $this->renderListWorklogs(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             case 'worklog:add': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             case 'worklog:update': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 $result = $this->renderAddUpdateWorklog(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 $result = $this->renderNothing(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * Returns list of worklogs in a JIRA ticket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * - Command: worklog:show (['worklog', 'wlog:s']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     protected function renderListWorklogs() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $rows = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if ($this->response[self::TOTAL] === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $rows[] = [new TableCell('No work yet logged.', ['colspan' => 5])]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $worklogs = $this->response[self::WORKLOGS]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $totalWorklogs = count($worklogs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         for ($counter = 0; $counter < $totalWorklogs; $counter++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $worklogId = $worklogs[$counter][self::ID]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $startDate = $worklogs[$counter][self::CREATED]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $author = $worklogs[$counter][self::AUTHOR][self::DISPLAY_NAME]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $timeSpent = $worklogs[$counter][self::TIME_SPENT]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $comment = $worklogs[$counter][self::COMMENT]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             if (strlen($comment) > 50) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 $comment = substr($comment, 0, 47) . '...'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $rows[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $worklogId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 $startDate, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 $author, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $timeSpent, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $comment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return $rows; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Returns worklog data after added/updated in a JIRA ticket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * - Command: worklog:add (['worklogadd', 'wlog:a']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * - Command: worklog:update (['worklogupdate', 'wlog:u']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |     protected function renderAddUpdateWorklog() | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         $rows = []; | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         $worklogId = $this->response[self::ID]; | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         $startDate = $this->response[self::CREATED]; | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |         $author = $this->response[self::AUTHOR][self::DISPLAY_NAME]; | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         $timeSpent = $this->response[self::TIME_SPENT]; | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         $comment = $this->response[self::COMMENT]; | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         if (strlen($comment) > 50) { | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |             $comment = substr($comment, 0, 47) . '...'; | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         $rows[] = [ | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |             $worklogId, | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |             $startDate, | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |             $author, | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |             $timeSpent, | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |             $comment | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |         return $rows; | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  | 
            
                        
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.