| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace NotificationChannels\Pushbullet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use NotificationChannels\Pushbullet\Targets\Targetable; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  | class PushbulletMessage | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |     const TYPE_NOTE = 'note'; | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     const TYPE_LINK = 'link'; | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      * Type of message (currently: note or link). | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     public $type = 'note'; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     /** @var \NotificationChannels\Pushbullet\Targets\Targetable */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     protected $target; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * Notification title. | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     public $title; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * Notification message. | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     public $message; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * Url if notification is of link type. | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     public $url; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param string $message | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @return static | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public static function create($message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return new static($message); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @param string $message | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 7 |  |     public function __construct($message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 7 |  |         $this->message = $message; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 | 7 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @param  \NotificationChannels\Pushbullet\Targets\Targetable  $targetable | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     public function target(Targetable $targetable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->target = $targetable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * Specify that notification is of `note` type. | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |     public function note() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         $this->type = static::TYPE_NOTE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * Specify that notification is of `link` type. | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |     public function link() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |         $this->type = static::TYPE_LINK; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 2 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      * Set notification title. | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * @param  string  $title | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |     public function title($title) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |         $this->title = $title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      * Set notification message. | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * @param  string  $message | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |     public function message($message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |         $this->message = $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |      * Set notification url (if notification is of `link` type). | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * @param  string  $url | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |     public function url($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |         $this->url = $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * Get array representation of message for Pushbullet client. | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     public function toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $payload = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             'type' => $this->type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             'title' => $this->title, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             'body' => $this->message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         return array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             $payload, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             $this->target->getTarget(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             $this->getUrlParameter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     private function isLink() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         return $this->type === static::TYPE_LINK; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     private function getUrlParameter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         return $this->isLink() ? ['url' => $this->url] : []; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 174 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  |