| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ConferenceTools\Checkin\Domain\ProcessManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Carnage\Cqrs\MessageHandler\AbstractMethodNameMessageHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Carnage\Cqrs\Persistence\EventStore\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Carnage\Cqrs\Persistence\Repository\RepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use ConferenceTools\Checkin\Domain\Event\Delegate\DelegateRegistered; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use ConferenceTools\Checkin\Domain\Event\Purchase\TicketAssigned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use ConferenceTools\Checkin\Domain\Event\Purchase\TicketCreated; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use ConferenceTools\Checkin\Domain\Event\Purchase\TicketPurchasePaid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use ConferenceTools\Checkin\Domain\Process\ImportPurchase as ImportPurchaseProcess; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | class ImportPurchase extends AbstractMethodNameMessageHandler | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     private $repository; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 7 |  |     public function __construct(RepositoryInterface $repository) | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 7 |  |         $this->repository = $repository; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 7 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 3 |  |     protected function handleTicketCreated(TicketCreated $event) | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 3 |  |         $process = $this->loadProcess($event->getTicket()->getPurchaseId()); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 3 |  |         $process->ticketCreated($event); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 3 |  |         $this->repository->save($process); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 4 |  |     protected function handleTicketAssigned(TicketAssigned $event) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 4 |  |         $process = $this->loadProcess($event->getTicket()->getPurchaseId()); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 4 |  |         $process->ticketAssigned($event); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 4 |  |         $this->repository->save($process); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 4 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 5 |  |     protected function handleTicketPurchasePaid(TicketPurchasePaid $event) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 5 |  |         $process = $this->loadProcess($event->getPurchaseId()); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 5 |  |         $process->ticketPurchasePaid($event); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 5 |  |         $this->repository->save($process); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 5 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |     protected function handleDelegateRegistered(DelegateRegistered $event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |         $process = $this->loadProcess($event->getTicket()->getPurchaseId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         $process->delegateRegistered($event); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 | 2 |  |         $this->repository->save($process); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 7 |  |     private function loadProcess($id): ImportPurchaseProcess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 7 |  |             $process = $this->repository->load($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 7 |  |         } catch (NotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 7 |  |             $process = ImportPurchaseProcess::withId($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 | 7 |  |         return $process; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                                                                
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 61 |  |  | } |