AbstractTransportFactory.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\Notifier\Transport;
  11. use Symfony\Component\EventDispatcher\Event;
  12. use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
  13. use Symfony\Component\Notifier\Exception\IncompleteDsnException;
  14. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Contracts\HttpClient\HttpClientInterface;
  16. /**
  17. * @author Konstantin Myakshin <molodchick@gmail.com>
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. *
  20. * @experimental in 5.1
  21. */
  22. abstract class AbstractTransportFactory implements TransportFactoryInterface
  23. {
  24. protected $dispatcher;
  25. protected $client;
  26. public function __construct(EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null)
  27. {
  28. $this->dispatcher = class_exists(Event::class) ? LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher;
  29. $this->client = $client;
  30. }
  31. public function supports(Dsn $dsn): bool
  32. {
  33. return \in_array($dsn->getScheme(), $this->getSupportedSchemes());
  34. }
  35. /**
  36. * @return string[]
  37. */
  38. abstract protected function getSupportedSchemes(): array;
  39. protected function getUser(Dsn $dsn): string
  40. {
  41. $user = $dsn->getUser();
  42. if (null === $user) {
  43. throw new IncompleteDsnException('User is not set.', $dsn->getOriginalDsn());
  44. }
  45. return $user;
  46. }
  47. protected function getPassword(Dsn $dsn): string
  48. {
  49. $password = $dsn->getPassword();
  50. if (null === $password) {
  51. throw new IncompleteDsnException('Password is not set.', $dsn->getOriginalDsn());
  52. }
  53. return $password;
  54. }
  55. }