-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transport.php
150 lines (129 loc) · 4.42 KB
/
Transport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <francois.pluchino@klipper.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\SmsSender;
use Klipper\Bridge\SmsSender\Amazon\Transport\SnsTransportFactory;
use Klipper\Bridge\SmsSender\Twilio\Transport\TwilioTransportFactory;
use Klipper\Component\SmsSender\Exception\UnsupportedHostException;
use Klipper\Component\SmsSender\Transport\Dsn;
use Klipper\Component\SmsSender\Transport\FailoverTransport;
use Klipper\Component\SmsSender\Transport\NullTransportFactory;
use Klipper\Component\SmsSender\Transport\RoundRobinTransport;
use Klipper\Component\SmsSender\Transport\TransportFactoryInterface;
use Klipper\Component\SmsSender\Transport\TransportInterface;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* @author François Pluchino <francois.pluchino@klipper.dev>
*/
class Transport
{
private const FACTORY_CLASSES = [
SnsTransportFactory::class,
TwilioTransportFactory::class,
NullTransportFactory::class,
];
/**
* @var TransportFactoryInterface[]
*/
private $factories;
/**
* @param TransportFactoryInterface[] $factories The sms sender transport factories
*/
public function __construct(iterable $factories)
{
$this->factories = $factories;
}
/**
* Create the transport form the DSN and include the failover or round robin logic if necessary.
*
* @param string $dsn The DSN to build the transport
* @param null|EventDispatcherInterface $dispatcher The event dispatcher
* @param null|HttpClientInterface $client The custom http client
* @param null|LoggerInterface $logger The logger
*/
public static function fromDsn(
string $dsn,
?EventDispatcherInterface $dispatcher = null,
?HttpClientInterface $client = null,
?LoggerInterface $logger = null
): TransportInterface {
$factory = new self(self::getDefaultFactories($dispatcher, $client, $logger));
return $factory->fromString($dsn);
}
/**
* Create the transport from the dsn string.
*
* @param string $dsn The dsn
*/
public function fromString(string $dsn): TransportInterface
{
// failover?
$dsns = preg_split('/\s++\|\|\s++/', $dsn);
if (\count($dsns) > 1) {
return new FailoverTransport($this->createFromDsns($dsns));
}
// round robin?
$dsns = preg_split('/\s++&&\s++/', $dsn);
if (\count($dsns) > 1) {
return new RoundRobinTransport($this->createFromDsns($dsns));
}
return $this->fromDsnObject(Dsn::fromString($dsn));
}
/**
* Create the transport from the dsn instance.
*
* @param Dsn $dsn The dsn instance
*/
public function fromDsnObject(Dsn $dsn): TransportInterface
{
foreach ($this->factories as $factory) {
if ($factory->supports($dsn)) {
return $factory->create($dsn);
}
}
throw new UnsupportedHostException($dsn);
}
/**
* Create the transports from the dsn strings.
*
* @param string[] $dsns The dsn strings
*
* @return TransportInterface[]
*/
private function createFromDsns(array $dsns): array
{
$transports = [];
foreach ($dsns as $dsn) {
$transports[] = $this->fromDsnObject(Dsn::fromString($dsn));
}
return $transports;
}
/**
* Get the default factories.
*
* @param null|EventDispatcherInterface $dispatcher The event dispatcher
* @param null|HttpClientInterface $client The http client
* @param null|LoggerInterface $logger The logger
*/
private static function getDefaultFactories(
?EventDispatcherInterface $dispatcher = null,
?HttpClientInterface $client = null,
?LoggerInterface $logger = null
): iterable {
$factories = [];
foreach (self::FACTORY_CLASSES as $factoryClass) {
if (class_exists($factoryClass)) {
$factories[] = new $factoryClass($dispatcher, $client, $logger);
}
}
return $factories;
}
}