I'm creating a client to a third-party soap web service in my application. Here is the way I'm thinking of doing it:
- Create a
ClientFactory
that makes a client based on thewsdl
file, and sets needed options on it, then returns it. - Have several
Request
objects, preferably implementing theRequest
interface, that take theClient
as their constructor argument, and then make requests to the web service through that client.
Here is a short example to illustrate my point.
class ClientFactory
{
public function create()
{
return new \SoapClient('http://www.example.com/example.wsdl', [
'trace' => true
]);
}
}
class LockSeatRequest implements RequestInterface
{
public function __construct(Client $client)
{
$this->client = $client;
}
public function execute($id, $count, $date)
{
$result = $this->client->lockSeat($id, $count, $date);
// Make some changes to $result here...
return $result;
}
}
interface Request
{
// what should I put in here?
}
Client code example:
$client = $clientFactory->create();
$lockSeatRequest = new lockSeatRequest($client);
$lockSeatRequest->execute(1, 5, '2015-04-04');
My problem here is that the number of arguments, and the type of arguments, are different from request to request, and this makes it impossible to define an interface. The way I have seen it done in other PHP libraries is to accept an array of parameters, but my issue with that approach is that it makes the user have to look up the parameters for each function call instead of making it explicit. Also, I would then have to add cruft such as checking whether the right keys are defined in the array, to each Request
, instead of having the language ensure each parameter with the right type is passed to the function.
How can I have multiple methods, each of them requiring a different number of arguments, but still have them implement a particular interface?
Or, am I seeing this totally wrong?