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
<?php
namespace Laminas\Mail\Transport;
use Laminas\Stdlib\ArrayUtils;
use Traversable;
abstract class Factory
{
/**
* @var array Known transport types
*/
protected static $classMap = [
'file' => File::class,
'inmemory' => InMemory::class,
'memory' => InMemory::class,
'null' => InMemory::class,
'sendmail' => Sendmail::class,
'smtp' => Smtp::class,
];
/**
* @param array $spec
* @return TransportInterface
* @throws Exception\InvalidArgumentException
* @throws Exception\DomainException
*/
public static function create($spec = [])
{
if ($spec instanceof Traversable) {
$spec = ArrayUtils::iteratorToArray($spec);
}
if (! is_array($spec)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an array or Traversable argument; received "%s"',
__METHOD__,
(is_object($spec) ? get_class($spec) : gettype($spec))
));
}
$type = $spec['type'] ?? 'sendmail';
$normalizedType = strtolower($type);
if (isset(static::$classMap[$normalizedType])) {
$type = static::$classMap[$normalizedType];
}
if (! class_exists($type)) {
throw new Exception\DomainException(sprintf(
'%s expects the "type" attribute to resolve to an existing class; received "%s"',
__METHOD__,
$type
));
}
$transport = new $type();
if (! $transport instanceof TransportInterface) {
throw new Exception\DomainException(sprintf(
'%s expects the "type" attribute to resolve to a valid %s instance; received "%s"',
__METHOD__,
TransportInterface::class,
$type
));
}
if ($transport instanceof Smtp && isset($spec['options'])) {
$transport->setOptions(new SmtpOptions($spec['options']));
}
if ($transport instanceof File && isset($spec['options'])) {
$transport->setOptions(new FileOptions($spec['options']));
}
return $transport;
}
}