| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace CultureKings\Afterpay\Service\Merchant; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use CultureKings\Afterpay\Exception\ApiException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use CultureKings\Afterpay\Model\ErrorResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use CultureKings\Afterpay\Model\Merchant\Authorization; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use CultureKings\Afterpay\Model\Merchant\OrderDetails; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use CultureKings\Afterpay\Model\Merchant\OrderToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use CultureKings\Afterpay\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use GuzzleHttp\Exception\BadResponseException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use JMS\Serializer\SerializerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class Orders | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package CultureKings\Afterpay\Service\Merchant | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class Orders | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     use Traits\ClientTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     use Traits\AuthorizationTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     use Traits\SerializerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Orders constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param Client              $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param Authorization       $authorization | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param SerializerInterface $serializer | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         Client $client, | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         Authorization $authorization, | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         SerializerInterface $serializer | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $this->setClient($client); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $this->setAuthorization($authorization); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $this->setSerializer($serializer); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param OrderDetails $order | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return OrderToken|object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function create(OrderDetails $order) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $result = $this->getClient()->post( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 'orders', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     'auth' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                         $this->getAuthorization()->getMerchantId(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                         $this->getAuthorization()->getSecret(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     'headers' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                         'Accept' => 'application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                         'Content-Type' => 'application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     'body' => $this->getSerializer()->serialize($order, 'json'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } catch (BadResponseException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             throw new ApiException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 $this->getSerializer()->deserialize( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     (string) $e->getResponse()->getBody(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     ErrorResponse::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     'json' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return $this->getSerializer()->deserialize( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             (string) $result->getBody(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             OrderToken::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             'json' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param string $token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return OrderDetails|object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function get($token) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $result = $this->getClient()->get( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 sprintf('orders/%s', $token), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                     'auth' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                         $this->getAuthorization()->getMerchantId(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                         $this->getAuthorization()->getSecret(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } catch (BadResponseException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             throw new ApiException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $this->getSerializer()->deserialize( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     (string) $e->getResponse()->getBody(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                     ErrorResponse::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                     'json' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         return $this->getSerializer()->deserialize( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             (string) $result->getBody(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             OrderDetails::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             'json' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                        
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: