1 | <?php |
||
22 | class DropletService |
||
23 | { |
||
24 | /** |
||
25 | * Droplet Factory |
||
26 | * |
||
27 | * @var \Billow\Droplets\DropletFactoryInterface |
||
28 | */ |
||
29 | private $factory; |
||
30 | |||
31 | /** |
||
32 | * Client to make http requests |
||
33 | * |
||
34 | * @var \Billow\ClientInterface |
||
35 | */ |
||
36 | private $client; |
||
37 | |||
38 | /** |
||
39 | * Method to set factory |
||
40 | * |
||
41 | * @param \Billow\Droplets\DropletFactoryInterface $factory |
||
42 | */ |
||
43 | public function setFactory(DropletFactoryInterface $factory) |
||
47 | |||
48 | /** |
||
49 | * Method to retrieve or instantiate a Droplet Factory |
||
50 | * |
||
51 | * @return \Billow\Droplets\DropletFactoryInterface |
||
52 | */ |
||
53 | public function getFactory() |
||
60 | |||
61 | /** |
||
62 | * Method to set client |
||
63 | * |
||
64 | * @param \Billow\ClientInterface $client |
||
65 | */ |
||
66 | public function setClient(ClientInterface $client) |
||
70 | |||
71 | /** |
||
72 | * Method to retrieve set client or generate |
||
73 | * a new client if one is not set |
||
74 | * |
||
75 | * @return \Billow\ClientInterface |
||
76 | */ |
||
77 | public function getClient() |
||
84 | |||
85 | /** |
||
86 | * Method to create a new Digital Ocean Droplet |
||
87 | * |
||
88 | * @param Array $dropletRequest |
||
89 | * @param Array $headers |
||
90 | * @return \GuzzleHttp\Message\ResponseInterface |
||
91 | * @throws \Billow\Exceptions\ProvisionException |
||
92 | */ |
||
93 | public function create(Array $dropletRequest, Array $headers =[]) |
||
115 | |||
116 | /** |
||
117 | * Method to retrieve a droplet by id |
||
118 | * |
||
119 | * @param int $dropletId |
||
120 | * @param Array $headers |
||
121 | * @return \Billow\Droplets\Droplet |
||
122 | * @throws \Billow\Exceptions\DropletException |
||
123 | */ |
||
124 | public function retrieve($dropletId, Array $headers = []) |
||
145 | |||
146 | /** |
||
147 | * Method to retrieve a paginated list of droplets |
||
148 | * |
||
149 | * @param Array headers |
||
150 | * @param int per_page |
||
151 | * @param int page |
||
152 | * @return Array |
||
153 | */ |
||
154 | public function retrieveAll(Array $headers = [], $per_page = 25, $page = 1) |
||
185 | |||
186 | /** |
||
187 | * Method to perform an action on a Digital Ocean Droplet |
||
188 | * |
||
189 | * @param \Billow\Droplets\DropletInterface |
||
190 | * @param \Billow\Actions\ActionInterface $action |
||
191 | * @param Array $headers |
||
192 | * @return \GuzzleHttp\Message\ResponseInterface |
||
193 | */ |
||
194 | public function performAction(DropletInterface $droplet, ActionInterface $action, Array $headers = []) |
||
205 | |||
206 | /** |
||
207 | * Method to set the default Content-type if one is not sent |
||
208 | * |
||
209 | * @param Array Headers |
||
210 | * @return Array |
||
211 | */ |
||
212 | private function prepareHeaders(array $headers) |
||
219 | } |
||
220 |
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: