| 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\Repository\Project\Note; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Tinyissue\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Tinyissue\Model\Message\Queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Tinyissue\Model\Project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Tinyissue\Model\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Tinyissue\Repository\RepositoryUpdater; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | class Updater extends RepositoryUpdater | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @var Project\Note | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     protected $model; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function __construct(Project\Note $model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->model = $model; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * Create a new note. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @param array $input | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @return Project\Note | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function create(array $input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->model->body       = $input['note_body']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->model->project_id = $this->model->project->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->model->created_by = $this->model->createdBy->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         // Add event on successful save | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         Project\Note::saved(function (Project\Note $note) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $this->queueAdd($note, $this->user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->model->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         // Add to user's activity log | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->saveToActivity([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             'type_id'   => Model\Activity::TYPE_NOTE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             'parent_id' => $this->model->project->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             'user_id'   => $this->model->createdBy->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return $this->model; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * Update the note body. | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @param string $body | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @return Project\Note | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function updateBody($body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->model->body = $body; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         // Add event on successful save | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         Project\Note::saved(function (Project\Note $note) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $this->queueUpdate($note, $this->user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return $this->model->save(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * Delete a note. | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * @return bool|null | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function delete() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->model->activity()->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         // Add event on successful delete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         Project\Note::deleted(function (Project\Note $note) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $this->queueDelete($note, $this->user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return $this->model->delete(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * Insert add note to message queue. | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * @param Project\Note $note | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @param User         $changeBy | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function queueAdd(Project\Note $note, User $changeBy) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return (new Queue())->updater($changeBy)->queue(Queue::ADD_NOTE, $note, $changeBy); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * Insert update note to message queue. | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * @param Project\Note $note | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      * @param User         $changeBy | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function queueUpdate(Project\Note $note, User $changeBy) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         // Skip message if nothing changed in note | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         if (!$note->isDirty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         return (new Queue())->updater($changeBy)->queue(Queue::UPDATE_NOTE, $note, $changeBy); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      * Insert delete note to message queue. | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |      * @param Project\Note $note | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |      * @param User         $changeBy | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     public function queueDelete(Project\Note $note, User $changeBy) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         return (new Queue())->updater($changeBy)->queueDelete(Queue::DELETE_NOTE, $note, $changeBy); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  |