| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Xetaravel\Listeners\Subscribers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Xetaravel\Events\Experiences\ConversationWasCreatedEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Xetaravel\Events\Experiences\PostWasCreatedEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Xetaravel\Events\Experiences\PostWasSolvedEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Xetaravel\Models\Experience; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Xetaravel\Models\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class ExperienceSubscriber | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * The experience earned for the related event. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $experiences = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         PostWasSolvedEvent::class => 120, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         ConversationWasCreatedEvent::class => 90, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         PostWasCreatedEvent::class => 75 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * The events mapping to the listener function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $events = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         PostWasCreatedEvent::class => 'postWasCreated', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         PostWasSolvedEvent::class => 'postWasSolved', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         ConversationWasCreatedEvent::class => 'conversationWasCreated' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Register the listeners for the subscriber. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param Illuminate\Events\Dispatcher $events | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function subscribe($events) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         foreach ($this->events as $event => $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $events->listen($event, ExperienceSubscriber::class . '@' . $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Handle a PostWasCreated event. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param \Xetaravel\Events\Experiences\PostWasCreatedEvent $event The event that was fired. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function postWasCreated(PostWasCreatedEvent $event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             'amount' => $this->experiences[PostWasCreatedEvent::class], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'obtainable_id' => $event->post->getKey(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'obtainable_type' => get_class($event->post), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'event_type' => PostWasCreatedEvent::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return $this->create($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Handle a PostWasSolved event. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param \Xetaravel\Events\Experiences\PostWasSolvedEvent $event The event that was fired. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 75 |  | View Code Duplication |     public function postWasSolved(PostWasSolvedEvent $event) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             'user_id' => $event->post->user_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             'amount' => $this->experiences[PostWasSolvedEvent::class], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             'obtainable_id' => $event->post->getKey(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'obtainable_type' => get_class($event->post), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'event_type' => PostWasSolvedEvent::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $this->create($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Handle a ConversationWasCreated event. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param \Xetaravel\Events\Experiences\ConversationWasCreatedEvent $event The event that was fired. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 95 |  | View Code Duplication |     public function conversationWasCreated(ConversationWasCreatedEvent $event) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         $data = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             'amount' => $this->experiences[ConversationWasCreatedEvent::class], | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             'obtainable_id' => $event->conversation->getKey(), | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             'obtainable_type' => get_class($event->conversation), | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             'event_type' => ConversationWasCreatedEvent::class | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         return $this->create($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * Create the experience. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param array $data The data used to create the experience. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     protected function create(array $data) : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         if (!isset($data['data'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             $data['data'] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $experience = Experience::create($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         return !(is_null($experience)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.