UnsupportedSchemeException.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mailer\Exception;
  11. use Symfony\Component\Mailer\Bridge;
  12. use Symfony\Component\Mailer\Transport\Dsn;
  13. /**
  14. * @author Konstantin Myakshin <molodchick@gmail.com>
  15. */
  16. class UnsupportedSchemeException extends LogicException
  17. {
  18. private const SCHEME_TO_PACKAGE_MAP = [
  19. 'gmail' => [
  20. 'class' => Bridge\Google\Transport\GmailTransportFactory::class,
  21. 'package' => 'symfony/google-mailer',
  22. ],
  23. 'mailgun' => [
  24. 'class' => Bridge\Mailgun\Transport\MailgunTransportFactory::class,
  25. 'package' => 'symfony/mailgun-mailer',
  26. ],
  27. 'postmark' => [
  28. 'class' => Bridge\Postmark\Transport\PostmarkTransportFactory::class,
  29. 'package' => 'symfony/postmark-mailer',
  30. ],
  31. 'sendgrid' => [
  32. 'class' => Bridge\Sendgrid\Transport\SendgridTransportFactory::class,
  33. 'package' => 'symfony/sendgrid-mailer',
  34. ],
  35. 'ses' => [
  36. 'class' => Bridge\Amazon\Transport\SesTransportFactory::class,
  37. 'package' => 'symfony/amazon-mailer',
  38. ],
  39. 'mandrill' => [
  40. 'class' => Bridge\Mailchimp\Transport\MandrillTransportFactory::class,
  41. 'package' => 'symfony/mailchimp-mailer',
  42. ],
  43. 'mailjet' => [
  44. 'class' => Bridge\Mailjet\Transport\MailjetTransportFactory::class,
  45. 'package' => 'symfony/mailjet-mailer',
  46. ],
  47. 'sendinblue' => [
  48. 'class' => Bridge\Sendinblue\Transport\SendinblueTransportFactory::class,
  49. 'package' => 'symfony/sendinblue-mailer',
  50. ],
  51. ];
  52. public function __construct(Dsn $dsn, string $name = null, array $supported = [])
  53. {
  54. $provider = $dsn->getScheme();
  55. if (false !== $pos = strpos($provider, '+')) {
  56. $provider = substr($provider, 0, $pos);
  57. }
  58. $package = self::SCHEME_TO_PACKAGE_MAP[$provider] ?? null;
  59. if ($package && !class_exists($package['class'])) {
  60. parent::__construct(sprintf('Unable to send emails via "%s" as the bridge is not installed; try running "composer require %s".', $provider, $package['package']));
  61. return;
  62. }
  63. $message = sprintf('The "%s" scheme is not supported', $dsn->getScheme());
  64. if ($name && $supported) {
  65. $message .= sprintf('; supported schemes for mailer "%s" are: "%s"', $name, implode('", "', $supported));
  66. }
  67. parent::__construct($message.'.');
  68. }
  69. }