| 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\Services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Illuminate\Mail\Message as MailMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Illuminate\Contracts\Mail\Mailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Tinyissue\Http\Requests\FormRequest\Note; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Tinyissue\Model\Message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Tinyissue\Model\Project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Tinyissue\Model\Project\Issue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Tinyissue\Model\Role; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Tinyissue\Model\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * SendMessagesAbstract is an abstract class with for objects that requires sending messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @author Mohamed Alsharaf <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | abstract class SendMessagesAbstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Instance of issue that this message belong to. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var Issue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected $issue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * Instance of project that this message belong to. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var Issue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     protected $project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * The latest message queued. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @var Message\Queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     protected $latestMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Collection of all of the queued messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @var Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     protected $allMessages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Instance of a queued message that is for adding a record (ie. adding issue). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @var Message\Queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     protected $addMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Collection of users that must not receive messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @var Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     protected $excludeUsers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * Collection of all of the project users that should receive messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @var Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     protected $projectUsers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * Collection of full subscribers that will always receive messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @var Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     protected $fullSubscribers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Name of message template. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     protected $template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @var Mailer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     protected $mailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * Collection of all messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @var Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     protected $messages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * Set instance of Mailer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param Mailer $mailer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     public function setMailer(Mailer $mailer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->mailer = $mailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * The main method to process the massages queue and send them. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @param Message\Queue $latestMessage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param Collection    $changes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     public function process(Message\Queue $latestMessage, Collection $changes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $this->setup($latestMessage, $changes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         // Is model deleted? or in valid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         if (!$this->getModel() || !$this->validateData()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $this->processDirectMessages(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         // Skip if no users found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         if ($this->getProjectUsers()->isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $this->populateData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         // Send the latest message if it is about status (ie. closed issue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         if ($this->isStatusMessage()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             return $this->sendMessageToAll($this->latestMessage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         // Get message data for all of the messages combined & for add message queue if exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $addMessageData = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         if ($this->addMessage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             $addMessageData = $this->getMessageData($this->addMessage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $everythingMessageData = $this->getCombineMessageData($this->allMessages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         // Send messages to project users | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $this->sendMessages($this->getProjectUsers(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             'addMessage' => $addMessageData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             'everything' => $everythingMessageData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * Setup properties needed for the process. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param Message\Queue $latestMessage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @param Collection    $allMessages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     protected function setup(Message\Queue $latestMessage, Collection $allMessages) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         // Set queue messages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         $this->latestMessage = $latestMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         $this->allMessages   = $allMessages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         // Exclude the user who made the change from receiving messages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         $this->addToExcludeUsers($this->latestMessage->changeBy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         // Extract add model message if exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         if ($this->getModel()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             $addMessageIdentifier = Message\Queue::getAddEventNameFromModel($this->getModel()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             $this->addMessage     = $this->allMessages->where('event', $addMessageIdentifier)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         // Make sure to load issue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         $this->getIssue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      * Whether or not we have all the needed properties. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     abstract protected function validateData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * Process any messages queue that is to send messages to specific users. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      * For example, assign issue to user to message the user about the issue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     protected function processDirectMessages() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |      * Populate any data or properties. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |     protected function populateData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |      * Whether or not the latest message is about status change such as closed issue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |     abstract public function isStatusMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |      * Returns the message subject. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |     protected function getSubject() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |         return '#' . $this->issue->id . ' / ' . $this->issue->title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |      * Returns an array of data needed for the message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |      * @param Message\Queue $queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |      * @param array         $extraData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |     protected function getMessageData(Message\Queue $queue, array $extraData = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |         // Generic info for all messages emails | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |         $messageData                         = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |         $messageData['issue']                = $this->getIssue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |         $messageData['project']              = $this->getProject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |         $messageData['changes']              = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |         $messageData['changes']['change_by'] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |             'now' => $queue->changeBy->fullname, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         if ($this->getIssue()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |             $messageData['changes']['change_by']['url'] = $this->getIssue()->to(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |             $messageData['changes']['change_by']['url'] = $this->getProject()->to(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         $messageData['changeByImage']   = $queue->changeBy->image; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |         $messageData['changeByHeading'] = $this->getMessageHeading($queue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         $messageData['event']           = $queue->event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         // Info specific to a message type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |         $method = 'getMessageDataFor' . ucfirst(camel_case($queue->event)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         if (method_exists($this, $method)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |             $messageData = array_replace_recursive($messageData, $this->{$method}($queue, $extraData)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |         return $messageData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |      * Loop through all of the messages and combine its message data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |      * @param Collection $changes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |     protected function getCombineMessageData(Collection $changes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |         $everything = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |         $changes->reverse()->each(function (Message\Queue $queue) use (&$everything) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |             if (!$everything) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |                 $everything = $this->getMessageData($queue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |                 $messageData = $this->getMessageData($queue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |                 $everything['changes'] = array_merge($everything['changes'], $messageData['changes']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |         $latestMessage                 = $changes->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |         $everything['changeByHeading'] = $this->getMessageHeading($latestMessage, $changes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |         $everything['event']           = $latestMessage->event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |         $messageData                   = $this->getMessageData($latestMessage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |         $everything                    = array_replace_recursive($everything, $messageData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |         return $everything; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |      * Return text to be used for the message heading. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |      * @param Message\Queue   $queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |      * @param Collection|null $changes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |     protected function getMessageHeading(Message\Queue $queue, Collection $changes = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |         $heading = $queue->changeBy->fullname . ' '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |         // If other users have made changes too | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |         if (!is_null($changes) && $changes->unique('change_by_id')->count() > 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |             $heading .= '& others '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |         return $heading; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |      * Returns collection of all users in a project that should receive the messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |      * @return Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |     protected function getProjectUsers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |         if (null === $this->projectUsers) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |             $this->projectUsers = (new Project\User()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |                 ->with('message', 'user', 'user.role') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |                 ->whereNotIn('user_id', $this->getExcludeUsers()->lists('id')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |                 ->where('project_id', '=', $this->getProjectId()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |                 ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |         return $this->projectUsers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |      * Returns the model that is belong to the queue message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |      * @return Issue|Issue\Comment|Note | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |     protected function getModel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |         return $this->latestMessage->model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |      * Returns an instance of project issue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |      * @return Issue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |     abstract protected function getIssue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |      * Returns an instance of project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |      * @return Project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |     abstract protected function getProject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |      * Returns the id of a project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |      * @return int | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 360 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 361 |  |  |     abstract protected function getProjectId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |      * Returns collection of all of the users that must not receive messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |      * @return Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |     protected function getExcludeUsers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |         if (null === $this->excludeUsers) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |             $this->excludeUsers = collect([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |         return $this->excludeUsers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |      * Exclude a user from receiving messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |      * @param User $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |     protected function addToExcludeUsers(User $user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |         $this->getExcludeUsers()->push($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |      * Find user by id. This search the project users and fallback to excluded list of users. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |      * @param int $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |      * @return User | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |     protected function getUserById($userId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |         $projectUser = $this->getProjectUsers()->where('user_id', $userId, false)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |         if (!$projectUser) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  |             return $this->getExcludeUsers()->where('id', $userId, false)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |         return $projectUser->user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |      * Returns collection of all messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  |      * @return Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 |  |  |     protected function getMessages() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 |  |  |         if (null === $this->messages) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  |             $this->messages = (new Message())->orderBy('id', 'ASC')->get(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 |  |  |         return $this->messages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 |  |  |      * Send a message to a user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 |  |  |      * @param User  $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 428 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 |  |  |     private function sendMessage(User $user, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 432 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 433 |  |  |         // Make sure the data contains changes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 434 |  |  |         if (!array_key_exists('changes', $data) && count($data['changes']) > 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 435 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 436 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 437 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 438 |  |  |         return $this->mailer->send('email.' . $this->template, $data, function (MailMessage $message) use ($user) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 439 |  |  |             $message->to($user->email, $user->fullname)->subject($this->getSubject()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 440 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 441 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 442 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 443 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 444 |  |  |      * Send a message to a collection of users, or send customised message per use logic. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 445 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 446 |  |  |      * @param Collection $users | 
            
                                                                                                            
                            
            
                                    
            
            
                | 447 |  |  |      * @param array      $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 448 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 449 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 450 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 451 |  |  |     protected function sendMessages(Collection $users, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  |         foreach ($users as $user) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  |             $userMessageData = $this->getUserMessageData($user->user_id, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  |             if (!$this->wantToReceiveMessage($user, $userMessageData)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  |             $this->sendMessage($user->user, $userMessageData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 461 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 462 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 463 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 464 |  |  |      * Get customised message per user logic. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 465 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 466 |  |  |      * @param int   $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 467 |  |  |      * @param array $messagesData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 468 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 469 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 470 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 471 |  |  |     protected function getUserMessageData($userId, array $messagesData) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 472 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 473 |  |  |         if (array_key_exists('event', $messagesData)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 474 |  |  |             return $messagesData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 475 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 476 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 477 |  |  |         // Possible message data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 478 |  |  |         $addMessageData        = $messagesData['addMessage']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 479 |  |  |         $everythingMessageData = $messagesData['everything']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 480 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 481 |  |  |         // Check if the user has seen the model data and made a change | 
            
                                                                                                            
                            
            
                                    
            
            
                | 482 |  |  |         $changeMadeByUser = $this->allMessages->where('change_by_id', $userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 483 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 484 |  |  |         // This user has never seen this model data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 485 |  |  |         if (!$changeMadeByUser->count()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 486 |  |  |             if ($this->addMessage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 487 |  |  |                 return $addMessageData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 488 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 489 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 490 |  |  |             return $everythingMessageData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 491 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 492 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 493 |  |  |         // This user has seen this model data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 494 |  |  |         // Get all of the changes that may happened later. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 495 |  |  |         // Combine them and send message to the user about these changes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 496 |  |  |         $everythingMessageData = $this->getCombineMessageData( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 497 |  |  |             $this->allMessages->forget($changeMadeByUser->keys()->toArray()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 498 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 499 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 500 |  |  |         return $everythingMessageData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 501 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 502 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 503 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 504 |  |  |      * Whether or not the user wants to receive the message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 505 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 |  |  |      * @param Project\User $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  |      * @param array        $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 510 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 511 |  |  |     protected function wantToReceiveMessage(Project\User $user, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 512 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 513 |  |  |         /** @var Message $message */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 514 |  |  |         $message = $user->message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 515 |  |  |         if (!$message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 516 |  |  |             $roleName = $user->user->role->role; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 517 |  |  |             $message  = $this->getMessages()->where('name', Message::$defaultMessageToRole[$roleName])->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 518 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 519 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 520 |  |  |         // No message to send, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 521 |  |  |         // - if we can't find message object or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 522 |  |  |         // - messages are disabled or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 523 |  |  |         // - event is inactive for the user message setting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 524 |  |  |         if (!$message || $message->isDisabled() || !$message->isActiveEvent($data['event'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 525 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 526 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 527 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 528 |  |  |         // Wants to see all updates in project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 529 |  |  |         if ((bool) $message->in_all_issues === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 530 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 531 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 532 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 533 |  |  |         if (!$this->getIssue()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 534 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 535 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 536 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 537 |  |  |         // For issue only send messages if user is assignee or creator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 538 |  |  |         $creator  = $this->getIssue()->user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 539 |  |  |         $assignee = $this->getIssue()->assigned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 540 |  |  |         if ($user->user_id === $creator->id || ($assignee && $user->user_id === $assignee->id)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 541 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 542 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 543 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 544 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 545 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 546 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 547 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 548 |  |  |      * Send a message to al users in project and full subscribes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 549 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 550 |  |  |      * @param Message\Queue $queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 551 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 552 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 553 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 554 |  |  |     protected function sendMessageToAll(Message\Queue $queue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 555 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 556 |  |  |         $messageData = $this->getMessageData($queue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 557 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 558 |  |  |         $this->sendMessages($this->getProjectUsers(), $messageData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 559 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 560 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 561 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 562 |  |  |      * Load the creator of an issue to the collection of project users. So we can send message to creator if needed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 563 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 564 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 565 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 566 |  |  |     protected function loadIssueCreatorToProjectUsers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 567 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 568 |  |  |         // Stop if we can't get the issue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 569 |  |  |         if (!$this->getIssue()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 570 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 571 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 572 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 573 |  |  |         // Get issue creator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 574 |  |  |         $creator = $this->getIssue()->user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 575 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 576 |  |  |         // Stop if creator excluded from messages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 577 |  |  |         $excluded = $this->getExcludeUsers()->where('id', $creator->id, false)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 578 |  |  |         if ($excluded) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 579 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 580 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 581 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 582 |  |  |         // Stop if the creator already part of the project users | 
            
                                                                                                            
                            
            
                                    
            
            
                | 583 |  |  |         $existInProject = $this->getProjectUsers()->where('user_id', $creator->id, false)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 584 |  |  |         if ($existInProject) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 585 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 586 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 587 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 588 |  |  |         // Create virtual project user object & add to collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 589 |  |  |         $userProject = new Project\User([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 590 |  |  |             'user_id'    => $creator->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 591 |  |  |             'project_id' => $this->getProjectId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 592 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 593 |  |  |         $userProject->setRelation('user', $creator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 594 |  |  |         $userProject->setRelation('project', $this->getProject()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 595 |  |  |         $this->getProjectUsers()->push($userProject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 596 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 597 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 598 |  |  |  | 
            
                        
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.