1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultureKings\Afterpay\Service\InStore; |
4
|
|
|
|
5
|
|
|
use CultureKings\Afterpay\Exception\ApiException; |
6
|
|
|
use CultureKings\Afterpay\Model; |
7
|
|
|
use CultureKings\Afterpay\Traits; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
10
|
|
|
use GuzzleHttp\HandlerStack; |
11
|
|
|
use JMS\Serializer\SerializerInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PreApproval |
15
|
|
|
* @package CultureKings\Afterpay\Service\InStore |
16
|
|
|
*/ |
17
|
|
|
class PreApproval |
18
|
|
|
{ |
19
|
|
|
use Traits\ClientTrait; |
20
|
|
|
use Traits\AuthorizationTrait; |
21
|
|
|
use Traits\SerializerTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Device constructor. |
25
|
|
|
* |
26
|
|
|
* @param Model\InStore\Authorization $auth |
27
|
|
|
* @param Client $client |
28
|
|
|
* @param SerializerInterface $serializer |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Model\InStore\Authorization $auth, Client $client, SerializerInterface $serializer) |
31
|
|
|
{ |
32
|
|
|
$this->setAuthorization($auth); |
33
|
|
|
$this->setClient($client); |
34
|
|
|
$this->setSerializer($serializer); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $preApprovalCode |
39
|
|
|
* @param HandlerStack|null $stack |
40
|
|
|
* |
41
|
|
|
* @return array|\JMS\Serializer\scalar|object |
42
|
|
|
*/ |
43
|
|
|
public function enquiry($preApprovalCode, HandlerStack $stack = null) |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
|
|
$params = [ |
47
|
|
|
'headers' => [ |
48
|
|
|
'Accept' => 'application/json', |
49
|
|
|
'Content-Type' => 'application/json', |
50
|
|
|
'Authorization' => sprintf('Bearer %s', $this->getAuthorization()->getDeviceToken()), |
|
|
|
|
51
|
|
|
'Operator' => $this->getAuthorization()->getOperator(), |
|
|
|
|
52
|
|
|
'User-Agent' => $this->getAuthorization()->getUserAgent() |
|
|
|
|
53
|
|
|
], |
54
|
|
|
'body' => $this->getSerializer()->serialize( |
55
|
|
|
['preApprovalCode' => $preApprovalCode], |
56
|
|
|
'json' |
57
|
|
|
), |
58
|
|
|
]; |
59
|
|
|
if ($stack !== null) { |
60
|
|
|
$params['handler'] = $stack; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$result = $this->getClient()->post('preapprovals/enquire', $params); |
64
|
|
|
|
65
|
|
|
return $this->getSerializer()->deserialize( |
66
|
|
|
(string) $result->getBody(), |
67
|
|
|
Model\InStore\PreApproval::class, |
68
|
|
|
'json' |
69
|
|
|
); |
70
|
|
|
} catch (BadResponseException $e) { |
71
|
|
|
throw new ApiException( |
72
|
|
|
$this->getSerializer()->deserialize( |
73
|
|
|
(string) $e->getResponse()->getBody(), |
74
|
|
|
Model\ErrorResponse::class, |
75
|
|
|
'json' |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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: