I've an AbstractMessage object (a hierarchy, actually). It represents a small text message and can be sent using different transport methods: HTTP, REST and a "mailer" transport. Each transport relies on an external library for executing the transport itself, injected using a DI container.
Message itself may have different representations (query string, resource string or an instance of\Swift_Message), based on the transport used. A transport should use the more appropriate representation, again injected using constructor injection.
interface TransportInterface
{
public function executeTransport(AbstractMessage $message, array &$fails = array());
}
class HttpTransport implements TransportInterface
{
/**
* @var \Guzzle\Service\Client
*/
private $client;
/**
* @var Converter\MessageConverterInterface
*/
private $converter;
public function __construct(Client $client, MessageConverterInterface $converter)
{
$this->client = $client;
$this->converter = $converter;
}
public function executeTransport(AbstractMessage $message, array &$fails = array())
{
$representation = $this->converter->convert($message);
/* ... */
}
}
class RestTransport implements TransportInterface
{
/**
* @var \Guzzle\Service\Client
*/
private $client;
/**
* @var Converter\MessageConverterInterface
*/
private $converter;
public function __construct(Client $client, MessageConverterInterface $converter)
{
$this->client = $client;
$this->converter = $converter;
}
public function executeTransport(AbstractMessage $message, array &$fails = array())
{
$representation = $this->converter->convert($message);
/* ... */
}
}
class MailerTransport implements TransportInterface
{
/**
* @var \Swift_Mailer
*/
private $mailer;
/**
* @var Converter\MessageConverterInterface
*/
private $converter;
public function __construct(Swift_Mailer $mailer,
MessageConverterInterface $converter)
{
$this->mailer = $mailer;
$this->converter = $converter;
}
public function executeTransport(AbstractMessage $message, array &$fails = array())
{
$representation = $this->converter->convert($message);
/* ... */
}
}
The MessageConverterInterface and the actual helper may be fairly simple:
interface MessageConverterInterface
{
/**
* @param AbstractMessage $message
* @return mixed
*/
public function convert(AbstractMessage $message);
}
class MessageHelper
{
/**
* @var Transport\TransportInterface
*/
private $mailer;
public function send(AbstractMessage $message, array &$fails = array())
{
$this->transport->executeTransport($message, $fails);
}
}
Question: is this a good OO pattern for the library design? Does the pattern have a name?