1
|
|
|
<?php |
2
|
|
|
namespace CultureKings\Afterpay\Service\InStore; |
3
|
|
|
|
4
|
|
|
use CultureKings\Afterpay\Model; |
5
|
|
|
use CultureKings\Afterpay\Traits; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use GuzzleHttp\HandlerStack; |
8
|
|
|
use JMS\Serializer\SerializerInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AbstractService |
12
|
|
|
* @package CultureKings\Afterpay\Service\InStore |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractService |
15
|
|
|
{ |
16
|
|
|
use Traits\ClientTrait; |
17
|
|
|
use Traits\AuthorizationTrait; |
18
|
|
|
use Traits\SerializerTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Device constructor. |
22
|
|
|
* |
23
|
|
|
* @param Model\InStore\Authorization $auth |
24
|
|
|
* @param Client $client |
25
|
|
|
* @param SerializerInterface $serializer |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Model\InStore\Authorization $auth, Client $client, SerializerInterface $serializer) |
28
|
|
|
{ |
29
|
|
|
$this->setAuthorization($auth); |
30
|
|
|
$this->setClient($client); |
31
|
|
|
$this->setSerializer($serializer); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param object|array $model |
36
|
|
|
* @param HandlerStack|null $stack |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
protected function generateParams($model, HandlerStack $stack = null) |
41
|
|
|
{ |
42
|
|
|
$params = [ |
43
|
|
|
'headers' => [ |
44
|
|
|
'Accept' => 'application/json', |
45
|
|
|
'Content-Type' => 'application/json', |
46
|
|
|
'Authorization' => sprintf('Bearer %s', $this->getAuthorization()->getDeviceToken()), |
|
|
|
|
47
|
|
|
'Operator' => $this->getAuthorization()->getOperator(), |
|
|
|
|
48
|
|
|
'User-Agent' => $this->getAuthorization()->getUserAgent(), |
49
|
|
|
], |
50
|
|
|
'body' => $this->getSerializer()->serialize( |
51
|
|
|
$model, |
52
|
|
|
'json' |
53
|
|
|
), |
54
|
|
|
'timeout' => Model\InStore\Authorization::REQUEST_TIMEOUT_SECONDS, |
55
|
|
|
]; |
56
|
|
|
if ($stack !== null) { |
57
|
|
|
$params['handler'] = $stack; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $params; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
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: