| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace AlfredTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use AlfredTime\ServiceApiCall; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Harvest extends Service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $methods = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         'start'             => 'post', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         'stop'              => 'get', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         'delete'            => 'delete', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         'timer_running'     => 'get', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         'get_projects'      => 'get', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         'get_tags'          => 'get', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         'get_recent_timers' => 'get', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param $domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param null      $apiToken | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct($domain = null, $apiToken = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         parent::__construct('https://' . $domain . '.harvestapp.com/', $apiToken); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param $timerId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function apiDeleteUrl($timerId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return 'daily/delete/' . $timerId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function apiStartUrl() | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         return 'daily/add/'; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param $timerId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function apiStopUrl($timerId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return 'daily/timer/' . $timerId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param  $description | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param  $projectId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param  $taskId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function generateTimer($description, $projectId, $taskId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'notes'      => $description, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'project_id' => $projectId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'task_id'    => $taskId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function getOnlineData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $data['projects'] = $this->timerAction('get_projects', 'projects'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $data['tasks'] = $this->timerAction('get_tags', 'tasks'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param  $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function getProjects($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $this->getItems('projects', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 91 |  | View Code Duplication |     public function getRecentTimers() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $timers = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         foreach ($this->timerAction('get_recent_timers', 'daily')['day_entries'] as $dayEntry) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $timers[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 'id'           => $dayEntry['id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 'description'  => $dayEntry['notes'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 'project_id'   => $dayEntry['project_id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 'project_name' => $dayEntry['project'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 'tags'         => $dayEntry['task'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 'duration'     => $dayEntry['hours'] * 60 * 60, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return array_reverse($timers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param  $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function getTags($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return $this->getItems('tasks', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @param  string  $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param  string  $apiUri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 123 |  | View Code Duplication |     protected function timerAction($action, $apiUri, array $options = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $returnDataFor = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             'start', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             'timer_running', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             'get_projects', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             'get_tags', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             'get_recent_timers', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $method = $this->methodForAction($action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return $this->serviceApiCall->send( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             $method, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             $apiUri, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             $options, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             in_array($action, $returnDataFor) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @param  $needle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @param  array     $haystack | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     private function getItems($needle, array $haystack = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $items = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         if (isset($haystack[$needle]) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         foreach ($haystack[$needle] as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $items[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 'name' => $item[key($item)]['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                 'id'   => $item[key($item)]['id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         return $items; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * @param  $timerId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     private function isTimerRunning($timerId) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         $data = $this->timerAction('timer_running', 'daily/show/' . $timerId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         return isset($data['timer_started_at']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |  | 
            
                        
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.