| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of Laravel Love. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Anton Komarev <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Cog\Laravel\Love\Reacter\Models; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Cog\Contracts\Love\Reactant\Exceptions\ReactantInvalid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Cog\Contracts\Love\Reacter\Exceptions\NotAssignedToReacterable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Cog\Contracts\Love\Reacter\Models\Reacter as ReacterContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Cog\Contracts\Love\Reacterable\Models\Reacterable as ReacterableContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Cog\Contracts\Love\Reaction\Exceptions\ReactionAlreadyExists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Cog\Contracts\Love\Reaction\Exceptions\ReactionNotExists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Cog\Contracts\Love\Reaction\Models\Reaction as ReactionContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Cog\Laravel\Love\Reaction\Models\Reaction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Cog\Laravel\Love\Support\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Illuminate\Database\Eloquent\Relations\HasMany; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Illuminate\Database\Eloquent\Relations\MorphTo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | final class Reacter extends Model implements | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     ReacterContract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected $table = 'love_reacters'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $fillable = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         'type', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $casts = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         'id' => 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     ]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     public function reacterable(): MorphTo | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         return $this->morphTo('reacterable', 'type', 'id', 'love_reacter_id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function reactions(): HasMany | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return $this->hasMany(Reaction::class, 'reacter_id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function getId(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return $this->getAttributeValue('id'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function getReacterable(): ReacterableContract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $reacterable = $this->getAttribute('reacterable'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (is_null($reacterable)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             throw new NotAssignedToReacterable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return $reacterable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function getReactions(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $this->getAttribute('reactions'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function reactTo( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         ReactantContract $reactant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         ReactionTypeContract $reactionType, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ?float $rate = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     ): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if ($reactant->isNull()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             throw ReactantInvalid::notExists(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $reaction = $this->findReaction($reactant, $reactionType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if (is_null($reaction)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $this->createReaction($reactant, $reactionType, $rate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         // TODO: Get or compare rate value using reaction contract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         if (is_null($rate) || $reaction->getAttributeValue('rate') === $rate) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             throw new ReactionAlreadyExists( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 sprintf('Reaction of type `%s` with `%s` rate already exists.', $reactionType->getName(), $rate) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $reaction->update([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             'rate' => $rate, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     public function unreactTo( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         ReactantContract $reactant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         ReactionTypeContract $reactionType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     ): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if ($reactant->isNull()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             throw ReactantInvalid::notExists(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $reaction = $this->findReaction($reactant, $reactionType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         if (is_null($reaction)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             throw new ReactionNotExists( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 sprintf('Reaction of type `%s` not exists.', $reactionType->getName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $reaction->delete(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function hasReactedTo( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         ReactantContract $reactant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         ?ReactionTypeContract $reactionType = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     ): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if ($reactant->isNull()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return $reactant->isReactedBy($this, $reactionType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public function hasNotReactedTo( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         ReactantContract $reactant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         ?ReactionTypeContract $reactionType = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     ): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         return $reactant->isNotReactedBy($this, $reactionType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     public function isEqualTo( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         ReacterContract $that | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     ): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         return $that->isNotNull() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             && $this->getId() === $that->getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     public function isNotEqualTo( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         ReacterContract $that | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     ): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         return !$this->isEqualTo($that); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     public function isNull(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return !$this->exists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     public function isNotNull(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         return $this->exists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     private function createReaction( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         ReactantContract $reactant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         ReactionTypeContract $reactionType, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         ?float $rate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     ): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $this->reactions()->create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             'reaction_type_id' => $reactionType->getId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             'reactant_id' => $reactant->getId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             'rate' => $rate, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * @param \Cog\Contracts\Love\Reactant\Models\Reactant $reactant | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * @param \Cog\Contracts\Love\ReactionType\Models\ReactionType $reactionType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @return null|\Cog\Contracts\Love\Reaction\Models\Reaction|\Illuminate\Database\Eloquent\Model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     private function findReaction( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         ReactantContract $reactant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         ReactionTypeContract $reactionType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     ): ?ReactionContract { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             ->reactions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             ->where('reactant_id', $reactant->getId()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             ->where('reaction_type_id', $reactionType->getId()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             ->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 190 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 191 |  |  |  |