-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathDebugKitTransport.php
More file actions
145 lines (126 loc) · 3.82 KB
/
DebugKitTransport.php
File metadata and controls
145 lines (126 loc) · 3.82 KB
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
<?php
declare(strict_types=1);
namespace DebugKit\Mailer\Transport;
use ArrayObject;
use Cake\Core\App;
use Cake\Mailer\AbstractTransport;
use Cake\Mailer\Message;
/**
* Debug Transport class, useful for emulating the email sending process and inspecting
* the resulting email message before actually sending it during development
*/
class DebugKitTransport extends AbstractTransport
{
/**
* The transport object this class is decorating
*
* @var \Cake\Mailer\AbstractTransport|null
*/
protected ?AbstractTransport $originalTransport = null;
/**
* A reference to the object were emails will be pushed to
* for logging.
*
* @var \ArrayObject
*/
protected ArrayObject $emailLog;
/**
* Constructor
*
* @param array $config Configuration options.
* @param \Cake\Mailer\AbstractTransport|null $originalTransport The transport that is to be decorated
*/
public function __construct(array $config = [], ?AbstractTransport $originalTransport = null)
{
$this->emailLog = $config['debugKitLog'];
if ($originalTransport !== null) {
$this->originalTransport = $originalTransport;
return;
}
$className = false;
if (!empty($config['originalClassName'])) {
/** @var class-string<\Cake\Mailer\AbstractTransport> $className */
$className = App::className(
$config['originalClassName'],
'Mailer/Transport',
'Transport',
);
}
if ($className) {
unset($config['originalClassName'], $config['debugKitLog']);
$this->originalTransport = new $className($config);
}
}
/**
* @inheritDoc
*/
public function send(Message $message): array
{
$headers = $message->getHeaders(['from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc']);
$parts = [
'text' => $message->getBodyText(),
'html' => $message->getBodyHtml(),
];
$headers['Subject'] = $message->getOriginalSubject();
$result = ['headers' => $headers, 'message' => $parts];
$this->emailLog[] = $result;
if ($this->originalTransport !== null) {
return $this->originalTransport->send($message);
}
return $result;
}
/**
* Proxy unknown methods to the wrapped object
*
* @param string $method The method to call
* @param array $args The args to call $method with.
* @return mixed
*/
public function __call(string $method, array $args): mixed
{
/** @var callable $callable */
$callable = [$this->originalTransport, $method];
return call_user_func_array($callable, $args);
}
/**
* Proxy property reads to the wrapped object
*
* @param string $name The property to read.
* @return mixed
*/
public function __get(string $name): mixed
{
return $this->originalTransport->{$name};
}
/**
* Proxy property changes to the wrapped object
*
* @param string $name The property to read.
* @param mixed $value The property value.
* @return void
*/
public function __set(string $name, mixed $value): void
{
$this->originalTransport->{$name} = $value;
}
/**
* Proxy property changes to the wrapped object
*
* @param string $name The property to read.
* @return bool
*/
public function __isset(string $name): bool
{
return isset($this->originalTransport->{$name});
}
/**
* Proxy property changes to the wrapped object
*
* @param string $name The property to delete.
* @return void
*/
public function __unset(string $name): void
{
unset($this->originalTransport->{$name});
}
}