interface SenderInterface
{
public function sendAnswer(string $message): void;
public function supports(string $type): bool;
}
class EmailSender implements SenderInterface
{
public function sendAnswer(string $message): void
{
// тут отправляете сообщение
}
public function supports(string $type): bool
{
return 'email' === $type;
}
}
class SmsSender implements SenderInterface
{
public function sendAnswer(string $message): void
{
// тут отправляете сообщение
}
public function supports(string $type): bool
{
return 'sms' === $type;
}
}
class SenderAggregate
{
private $providers;
public function __construct(SenderInterface ...$providers)
{
$this->providers = $providers
}
public function send(string $type, string $message)
{
foreach ($this->providers as $provider) {
if ($provider->supports($type) {
return $provider->sendAnswer($message);
}
}
throw Exception;
}
}