| 1 | <?php |
||
| 10 | class CreateSubscription extends AbstractPlugin |
||
| 11 | { |
||
| 12 | use VerifyHttpStatusResponseCode; |
||
| 13 | |||
| 14 | const CREATE_SUBSCRIPTION_ENDPOINT = "/subscription"; |
||
| 15 | |||
| 16 | protected $baseUrl; |
||
| 17 | |||
| 18 | 2 | public function __construct(string $baseUrl) |
|
| 22 | |||
| 23 | 2 | public function getPluginAccessor(): string |
|
| 24 | { |
||
| 25 | 2 | return "createSubscription"; |
|
| 26 | } |
||
| 27 | |||
| 28 | 2 | public function handle( |
|
| 29 | string $customerIdentifier, |
||
| 30 | string $planCode, |
||
| 31 | string $customerAuth = "" |
||
| 32 | ) { |
||
| 33 | |||
| 34 | 2 | $response = $this->adapter->getHttpClient() |
|
|
|
|||
| 35 | 2 | ->post($this->baseUrl . self::CREATE_SUBSCRIPTION_ENDPOINT, [ |
|
| 36 | 2 | "body" => json_encode($this->makeBodyParams($customerIdentifier, $planCode, $customerAuth)) |
|
| 37 | ]); |
||
| 38 | |||
| 39 | 2 | $this->verifyResponse($response); |
|
| 40 | |||
| 41 | 1 | return json_decode($response->getBody(), true)["data"]; |
|
| 42 | } |
||
| 43 | |||
| 44 | 2 | protected function makeBodyParams( |
|
| 57 | } |
||
| 58 |
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: