| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Tinyissue package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Mohamed Alsharaf <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Tinyissue\Model\Project\Issue\Comment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Tinyissue\Model\Message\Queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Tinyissue\Model\Project\Issue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Tinyissue\Model\Project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Tinyissue\Model\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Tinyissue\Services\SendMessagesAbstract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * SendMessages is a class to manage & process of sending messages about comment changes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @author Mohamed Alsharaf <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class SendMessages extends SendMessagesAbstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $template = 'update_comment'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Returns an instance of Issue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @return Issue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 35 |  | View Code Duplication |     protected function getIssue() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         if (!$this->issue instanceof Issue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             if ($this->getModel()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 $this->issue = $this->getModel()->issue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 // Possible deleted comment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 $issueId     = $this->latestMessage->getDataFromPayload('origin.issue.id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 $this->issue = (new Issue())->find($issueId); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         return $this->issue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Returns an instance of Project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return Project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     protected function getProject() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return $this->getIssue()->project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * Returns the project id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected function getProjectId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return $this->getIssue()->project_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * Returns message data belongs to adding a comment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param Queue $queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     protected function getMessageDataForAddComment(Queue $queue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $messageData                    = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $messageData['changeByHeading'] = $queue->changeBy->fullname . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             ' commented on ' . link_to($this->getIssue()->to(), '#' . $this->getIssue()->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $messageData['changes']['comment'] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'noLabel' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'date'    => $this->getModel()->created_at, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'now'     => \Html::format($this->getModel()->comment), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $messageData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * Returns message data belongs to updating a comment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param Queue $queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     protected function getMessageDataForUpdateComment(Queue $queue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $messageData                    = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $messageData['changeByHeading'] = $queue->changeBy->fullname . ' updated a comment in ' . link_to($this->getIssue()->to(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 '#' . $this->getIssue()->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $messageData['changes']['comment'] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             'noLabel' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             'date'    => $queue->getDataFromPayload('origin.updated_at'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             'was'     => \Html::format($queue->getDataFromPayload('origin.comment')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             'now'     => \Html::format($queue->getDataFromPayload('dirty.comment')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return $messageData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * Returns message data belongs to deleting a note. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param Queue $queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 120 |  | View Code Duplication |     protected function getMessageDataForDeleteComment(Queue $queue) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $messageData                    = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $messageData['changeByHeading'] = $queue->changeBy->fullname . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             ' deleted a comment from ' . link_to($this->getIssue()->to(), '#' . $this->getIssue()->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $messageData['changes']['comment'] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             'noLabel' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             'date'    => $queue->created_at, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             'was'     => \Html::format($queue->getDataFromPayload('origin.comment')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             'now'     => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         return $messageData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * Return text to be used for the message heading. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @param Queue           $queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @param Collection|null $changes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     protected function getMessageHeading(Queue $queue, Collection $changes = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $heading = $queue->changeBy->fullname . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             ' commented on ' . link_to($this->getIssue()->to(), '#' . $this->getIssue()->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         return $heading; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * Check that the issue comment is loaded and the comment is belongs to the issue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     protected function validateData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         return $this->getIssue() && $this->getModel() && $this->getModel()->issue_id === $this->getIssue()->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * Populate assigned relation in the current issue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |     protected function populateData() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |         // Set the relation of assigned in issue object from the fetch data | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |         $this->issue->setRelation('assigned', $this->getUserById($this->issue->assigned_to)); | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |         $creator = $this->getUserById($this->issue->created_by); | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |         if ($creator) { | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |             $this->issue->setRelation('user', $creator); | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |         $this->loadIssueCreatorToProjectUsers(); | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * Check if the latest message is about deleting a comment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     public function isStatusMessage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         return $this->latestMessage->event === Queue::DELETE_COMMENT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      * Whether or not the user wants to receive the message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * @param Project\User $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * @param array        $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     protected function wantToReceiveMessage(Project\User $user, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         $status = parent::wantToReceiveMessage($user, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         if (!$status) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         // Check if user allowed to receive the message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         $tags = $this->getIssue()->tags; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         foreach ($tags as $tag) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |             if (!$tag->allowMessagesToUser($user->user)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 214 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 215 |  |  |  | 
            
                        
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.