| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the LineMob package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Ishmael Doss <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace LineMob\Core; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use League\Tactician\CommandBus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use LineMob\Core\Command\FallbackCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use LineMob\Core\Exception\DerailException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use LineMob\Core\Sender\SenderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @author Ishmael Doss <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class Receiver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var SenderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $sender; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var CommandBus | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $commandBus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var RegistryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private $registry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var CommandHandlerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private $handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         SenderInterface $sender, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         RegistryInterface $registry, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         CommandBus $commandBus, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         CommandHandlerInterface $handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->sender = $sender; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->registry = $registry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->commandBus = $commandBus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->handler = $handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param $signature | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function validate($body, $signature) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return $this->sender->validateSignature($body, $signature); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param array $eventData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     private function captureReceiveMessageText(array $eventData) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $type = strtolower(@$eventData['message']['type']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         switch ($type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             case Constants::REVEIVE_TYPE_MESSAGE_TEXT: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 return @$eventData['message']['text']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 throw new \RuntimeException("Unknown message type `$type`."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param array $eventData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return Input | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     private function captureInput(array $eventData) | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         $inputData = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             'text' => FallbackCommand::CMD, | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         if (empty($eventData['replyToken'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             throw new \InvalidArgumentException("Require `replyToken` to run."); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         if (empty($eventData['source']['userId'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             throw new \InvalidArgumentException("Require `userId` to run."); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         $inputData['replyToken'] = $eventData['replyToken']; | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         $inputData['userId'] = $eventData['source']['userId']; | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         $type = strtolower(@$eventData['type']); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         switch ($type) { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             case Constants::REVEIVE_TYPE_FOLLOW: | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |                 $inputData['text'] = ':follow'; | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             case Constants::REVEIVE_TYPE_MESSAGE: | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                 $inputData['text'] = $this->captureReceiveMessageText($eventData); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |             default: | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |                 throw new \RuntimeException("Unsupported event type `$type`."); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         return new Input($inputData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param $payload | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     public function handle($payload) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $data = json_decode($payload, true) ?: []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $events = (array)@$data['events']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $results = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             foreach ($events as $event) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 $input = $this->captureInput($event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 $command = $this->registry->findCommand($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 $command->input = $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 $results[] = $this->commandBus->handle($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } catch (DerailException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $results[] = $this->handler->handle($e->command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             $results[] = sprintf('ERROR: %s', $e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         return $results; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 151 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |  |