It basically works the same as you did before, just that instead of a Zend_Db_Table_Gateway you use a My_UserGateway_Whatever, e.g. create an interface first:
interface UserGateway
{
/**
* @return array
* @throws UserGatewayException
*/
public function findAll();
}
We dont want Exceptions from the concrete Gateways to appear in the consuming code, so we add the UserGatewayException as a catch all:
class UserGatewayException extends RuntimeException
{}
Then add a class implementing that interface:
class My_UserGateway_Webservice implements UserGateway
{
public function findAll()
{
try {
// insert logic to fetch from the webservice
return $userData;
} catch (Exception $e) {
throw new UserGatewayException($e->getMessage(), $e->getCode, $e);
}
}
// … more code
}
Likewise, if you want to use a Database source, you can write an adapter for the Zend_Db_* classes, e.g.
class My_UserGateway_Database implements UserGateway
{
private $tableDataGateway;
public function __construct(Zend_Db_Table_Abstract $tableDataGateway)
{
$this->tableDataGateway = $tableDataGateway;
}
public function findAll()
{
try {
return $this->tableDataGateway->select()->blah();
} catch (Exception $e) {
throw new UserGatewayException($e->getMessage(), $e->getCode, $e);
}
}
// … more code
}
If you need another Data Provider, make sure they implement that interface, so you can rely on the findAll method to be there. Make your consuming class depend on the interface, e.g.
class SomethingUsingUsers
{
private $userGateway;
public function __construct(UserGateway $userGateway)
{
$this->userGateway = $userGateway;
}
public function something()
{
try {
$users = $this->userGateway->findAll();
// do something with array of user data from gateway
} catch (UserGatewayException $e) {
// handle Exception
}
}
// … more code
}
Now, when you create SomethingUsingUsers you can easily inject one or the other Gateway into the constructor and your code will work regardless of which Gateway you used:
$foo = SomethingUsingUsers(
new My_UserGateway_Database(
new Zend_Db_Table('User')
)
)
or, for the Webservice:
$foo = SomethingUsingUsers(
new My_UserGateway_Webservice(
// any additional dependencies
)
)