| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Fenos\Notifynder\Models; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Eloquent\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Fenos\Notifynder\Parsers\NotificationParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Fenos\Notifynder\Builder\Notification as BuilderNotification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Class Notification. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @property int $to_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @property string $to_type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @property int $from_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @property string $from_type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @property int $category_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @property bool $read | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @property string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @property array $extra | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @property string $expires_at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @property int $stack_id | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | class Notification extends Model | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     protected $fillable = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         'to_id', | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         'to_type', | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         'from_id', | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         'from_type', | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         'category_id', | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         'read', | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         'url', | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         'extra', | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         'expires_at', | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         'stack_id', | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     protected $appends = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         'text', | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     protected $casts = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         'extra' => 'array', | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * Notification constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * @param array|\Fenos\Notifynder\Builder\Notification $attributes | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function __construct($attributes = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $table = app('notifynder.resolver.model')->getTable(get_class($this)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if (! empty($table)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $this->setTable($table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->fillable($this->mergeFillables()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if ($attributes instanceof BuilderNotification) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $attributes = $attributes->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         parent::__construct($attributes); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function category() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $this->belongsTo(NotificationCategory::class, 'category_id'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     public function from() | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         if (notifynder_config()->isPolymorphic()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             return $this->morphTo('from'); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'from_id'); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function to() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         if (notifynder_config()->isPolymorphic()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             return $this->morphTo('to'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'to_id'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     public function getCustomFillableFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return notifynder_config()->getAdditionalFields(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     protected function mergeFillables() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $fillables = array_unique(array_merge($this->getFillable(), $this->getCustomFillableFields())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return $fillables; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * @throws \Fenos\Notifynder\Exceptions\ExtraParamsException | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     public function getTextAttribute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         if (! array_key_exists('text', $this->attributes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $notifynderParse = new NotificationParser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $this->attributes['text'] = $notifynderParse->parse($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return $this->attributes['text']; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      * @return bool|int | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     public function read() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return $this->update(['read' => 1]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |      * @return bool|int | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     public function unread() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         return $this->update(['read' => 0]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     public function resend() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $this->updateTimestamps(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $this->read = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         return $this->save(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     public function isAnonymous() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         return is_null($this->from_id); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 174 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |      * @param Builder $query | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |      * @param $category | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |      * @return Builder | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     public function scopeByCategory(Builder $query, $category) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         $categoryId = NotificationCategory::getIdByCategory($category); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         return $query->where('category_id', $categoryId); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |      * @param Builder $query | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |      * @param int $read | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |      * @return Builder | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     public function scopeByRead(Builder $query, $read = 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         return $query->where('read', $read); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 196 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 197 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 198 |  |  |  |