| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace LauLamanApps\IzettleApi\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use LauLamanApps\IzettleApi\API\Purchase\Purchase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use LauLamanApps\IzettleApi\API\Purchase\PurchaseHistory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use LauLamanApps\IzettleApi\Client\Exceptions\PurchaseNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use LauLamanApps\IzettleApi\Client\Purchase\PurchaseBuilderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use LauLamanApps\IzettleApi\Client\Purchase\PurchaseHistoryBuilderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use LauLamanApps\IzettleApi\Exception\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use LauLamanApps\IzettleApi\IzettleClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Ramsey\Uuid\UuidInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | final class PurchaseClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const BASE_URL = 'https://purchase.izettle.com'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     const GET_PURCHASE = self::BASE_URL . '/purchase/v2/%s'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     const GET_PURCHASES = self::BASE_URL . '/purchases/v2'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $purchaseHistoryBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $purchaseBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 5 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         IzettleClientInterface $client, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         PurchaseHistoryBuilderInterface $purchaseHistoryBuilder, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         PurchaseBuilderInterface $purchaseBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 5 |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 5 |  |         $this->purchaseHistoryBuilder = $purchaseHistoryBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 5 |  |         $this->purchaseBuilder = $purchaseBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |     public function getPurchaseHistory(): PurchaseHistory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 2 |  |         $json = $this->client->getJson($this->client->get(self::GET_PURCHASES)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  |         return $this->purchaseHistoryBuilder->buildFromJson($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 44 | 3 |  |     public function getPurchase(UuidInterface $uuid): Purchase | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         try { | 
            
                                                        
            
                                    
            
            
                | 47 | 3 |  |             $response = $this->client->get(sprintf(self::GET_PURCHASE, (string) $uuid)); | 
            
                                                        
            
                                    
            
            
                | 48 | 1 |  |         } catch (NotFoundException $e) { | 
            
                                                        
            
                                    
            
            
                | 49 | 1 |  |             throw new PurchaseNotFoundException($e->getMessage()); | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 52 | 2 |  |         $json = $this->client->getJson($response); | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 54 | 2 |  |         return $this->purchaseBuilder->buildFromJson($json); | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 56 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  |