Skip to content

Commit 0d61de5

Browse files
committed
Config handling moved into a new class to allow class serialization
1 parent 61e0f1a commit 0d61de5

File tree

13 files changed

+576
-310
lines changed

13 files changed

+576
-310
lines changed

src/Attachment.php

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,23 @@
5757
class Attachment {
5858

5959
/**
60-
* @var Message $oMessage
60+
* @var Message $message
6161
*/
62-
protected Message $oMessage;
62+
protected Message $message;
6363

6464
/**
6565
* Used config
6666
*
67-
* @var array $config
67+
* @var Config $config
6868
*/
69-
protected array $config = [];
69+
protected Config $config;
70+
71+
/**
72+
* Attachment options
73+
*
74+
* @var array $options
75+
*/
76+
protected array $options = [];
7077

7178
/** @var Part $part */
7279
protected Part $part;
@@ -100,23 +107,24 @@ class Attachment {
100107

101108
/**
102109
* Attachment constructor.
103-
* @param Message $oMessage
110+
* @param Message $message
104111
* @param Part $part
105112
*/
106-
public function __construct(Message $oMessage, Part $part) {
107-
$this->config = ClientManager::get('options');
113+
public function __construct(Message $message, Part $part) {
114+
$this->message = $message;
115+
$this->config = $this->message->getConfig();
116+
$this->options = $this->config->get('options');
108117

109-
$this->oMessage = $oMessage;
110118
$this->part = $part;
111119
$this->part_number = $part->part_number;
112120

113-
if ($this->oMessage->getClient()) {
114-
$default_mask = $this->oMessage->getClient()?->getDefaultAttachmentMask();
121+
if ($this->message->getClient()) {
122+
$default_mask = $this->message->getClient()?->getDefaultAttachmentMask();
115123
if ($default_mask != null) {
116124
$this->mask = $default_mask;
117125
}
118126
} else {
119-
$default_mask = ClientManager::getMask("attachment");
127+
$default_mask = $this->config->getMask("attachment");
120128
if ($default_mask != "") {
121129
$this->mask = $default_mask;
122130
}
@@ -205,7 +213,7 @@ protected function fetch(): void {
205213
$content = $this->part->content;
206214

207215
$this->content_type = $this->part->content_type;
208-
$this->content = $this->oMessage->decodeString($content, $this->part->encoding);
216+
$this->content = $this->message->decodeString($content, $this->part->encoding);
209217

210218
// Create a hash of the raw part - this can be used to identify the attachment in the message context. However,
211219
// it is not guaranteed to be unique and collisions are possible.
@@ -292,7 +300,7 @@ public function decodeName(?string $name): string {
292300
}
293301
}
294302

295-
$decoder = $this->config['decoder']['message'];
303+
$decoder = $this->options['decoder']['message'];
296304
if (preg_match('/=\?([^?]+)\?(Q|B)\?(.+)\?=/i', $name, $matches)) {
297305
$name = $this->part->getHeader()->decode($name);
298306
} elseif ($decoder === 'utf-8' && extension_loaded('imap')) {
@@ -364,7 +372,7 @@ public function getAttributes(): array {
364372
* @return Message
365373
*/
366374
public function getMessage(): Message {
367-
return $this->oMessage;
375+
return $this->message;
368376
}
369377

370378
/**
@@ -390,6 +398,45 @@ public function getMask(): string {
390398
return $this->mask;
391399
}
392400

401+
/**
402+
* Get the attachment options
403+
* @return array
404+
*/
405+
public function getOptions(): array {
406+
return $this->options;
407+
}
408+
409+
/**
410+
* Set the attachment options
411+
* @param array $options
412+
*
413+
* @return $this
414+
*/
415+
public function setOptions(array $options): Attachment {
416+
$this->options = $options;
417+
return $this;
418+
}
419+
420+
/**
421+
* Get the used config
422+
*
423+
* @return Config
424+
*/
425+
public function getConfig(): Config {
426+
return $this->config;
427+
}
428+
429+
/**
430+
* Set the used config
431+
* @param Config $config
432+
*
433+
* @return $this
434+
*/
435+
public function setConfig(Config $config): Attachment {
436+
$this->config = $config;
437+
return $this;
438+
}
439+
393440
/**
394441
* Get a masked instance by providing a mask name
395442
* @param string|null $mask

src/Client.php

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ class Client {
4646
*/
4747
public ?ProtocolInterface $connection = null;
4848

49+
/**
50+
* Client configuration
51+
*
52+
* @var Config
53+
*/
54+
protected Config $config;
55+
4956
/**
5057
* Server hostname.
5158
*
@@ -174,14 +181,14 @@ class Client {
174181

175182
/**
176183
* Client constructor.
177-
* @param array $config
184+
* @param Config $config
178185
*
179186
* @throws MaskNotFoundException
180187
*/
181-
public function __construct(array $config = []) {
188+
public function __construct(Config $config) {
182189
$this->setConfig($config);
183-
$this->setMaskFromConfig($config);
184-
$this->setEventsFromConfig($config);
190+
$this->setMaskFromConfig();
191+
$this->setEventsFromConfig();
185192
}
186193

187194
/**
@@ -199,16 +206,17 @@ public function __destruct() {
199206
* Clone the current Client instance
200207
*
201208
* @return Client
209+
* @throws MaskNotFoundException
202210
*/
203211
public function clone(): Client {
204-
$client = new self();
212+
$client = new self($this->config);
205213
$client->events = $this->events;
206214
$client->timeout = $this->timeout;
207215
$client->active_folder = $this->active_folder;
208216
$client->default_account_config = $this->default_account_config;
209217
$config = $this->getAccountConfig();
210218
foreach($config as $key => $value) {
211-
$client->setAccountConfig($key, $config, $this->default_account_config);
219+
$client->setAccountConfig($key, $this->default_account_config);
212220
}
213221
$client->default_message_mask = $this->default_message_mask;
214222
$client->default_attachment_mask = $this->default_message_mask;
@@ -217,16 +225,17 @@ public function clone(): Client {
217225

218226
/**
219227
* Set the Client configuration
220-
* @param array $config
228+
* @param Config $config
221229
*
222230
* @return self
223231
*/
224-
public function setConfig(array $config): Client {
225-
$default_account = ClientManager::get('default');
226-
$default_config = ClientManager::get("accounts.$default_account");
232+
public function setConfig(Config $config): Client {
233+
$this->config = $config;
234+
$default_account = $this->config->get('default');
235+
$default_config = $this->config->get("accounts.$default_account");
227236

228237
foreach ($this->default_account_config as $key => $value) {
229-
$this->setAccountConfig($key, $config, $default_config);
238+
$this->setAccountConfig($key, $default_config);
230239
}
231240

232241
return $this;
@@ -235,27 +244,20 @@ public function setConfig(array $config): Client {
235244
/**
236245
* Get the current config
237246
*
238-
* @return array
247+
* @return Config
239248
*/
240-
public function getConfig(): array {
241-
$config = [];
242-
foreach($this->default_account_config as $key => $value) {
243-
$config[$key] = $this->$key;
244-
}
245-
return $config;
249+
public function getConfig(): Config {
250+
return $this->config;
246251
}
247252

248253
/**
249254
* Set a specific account config
250255
* @param string $key
251-
* @param array $config
252256
* @param array $default_config
253257
*/
254-
private function setAccountConfig(string $key, array $config, array $default_config): void {
258+
private function setAccountConfig(string $key, array $default_config): void {
255259
$value = $this->default_account_config[$key];
256-
if(isset($config[$key])) {
257-
$value = $config[$key];
258-
}elseif(isset($default_config[$key])) {
260+
if(isset($default_config[$key])) {
259261
$value = $default_config[$key];
260262
}
261263
$this->$key = $value;
@@ -278,10 +280,9 @@ public function getAccountConfig(): array {
278280

279281
/**
280282
* Look for a possible events in any available config
281-
* @param $config
282283
*/
283-
protected function setEventsFromConfig($config): void {
284-
$this->events = ClientManager::get("events");
284+
protected function setEventsFromConfig(): void {
285+
$this->events = $this->config->get("events");
285286
if(isset($config['events'])){
286287
foreach($config['events'] as $section => $events) {
287288
$this->events[$section] = array_merge($this->events[$section], $events);
@@ -291,50 +292,50 @@ protected function setEventsFromConfig($config): void {
291292

292293
/**
293294
* Look for a possible mask in any available config
294-
* @param $config
295295
*
296296
* @throws MaskNotFoundException
297297
*/
298-
protected function setMaskFromConfig($config): void {
298+
protected function setMaskFromConfig(): void {
299+
$masks = $this->config->get("masks");
299300

300-
if(isset($config['masks'])){
301-
if(isset($config['masks']['message'])) {
302-
if(class_exists($config['masks']['message'])) {
303-
$this->default_message_mask = $config['masks']['message'];
301+
if(isset($masks)){
302+
if(isset($masks['message'])) {
303+
if(class_exists($masks['message'])) {
304+
$this->default_message_mask = $masks['message'];
304305
}else{
305-
throw new MaskNotFoundException("Unknown mask provided: ".$config['masks']['message']);
306+
throw new MaskNotFoundException("Unknown mask provided: ".$masks['message']);
306307
}
307308
}else{
308-
$default_mask = ClientManager::getMask("message");
309+
$default_mask = $this->config->getMask("message");
309310
if($default_mask != ""){
310311
$this->default_message_mask = $default_mask;
311312
}else{
312313
throw new MaskNotFoundException("Unknown message mask provided");
313314
}
314315
}
315-
if(isset($config['masks']['attachment'])) {
316-
if(class_exists($config['masks']['attachment'])) {
317-
$this->default_attachment_mask = $config['masks']['attachment'];
316+
if(isset($masks['attachment'])) {
317+
if(class_exists($masks['attachment'])) {
318+
$this->default_attachment_mask = $masks['attachment'];
318319
}else{
319-
throw new MaskNotFoundException("Unknown mask provided: ". $config['masks']['attachment']);
320+
throw new MaskNotFoundException("Unknown mask provided: ". $masks['attachment']);
320321
}
321322
}else{
322-
$default_mask = ClientManager::getMask("attachment");
323+
$default_mask = $this->config->getMask("attachment");
323324
if($default_mask != ""){
324325
$this->default_attachment_mask = $default_mask;
325326
}else{
326327
throw new MaskNotFoundException("Unknown attachment mask provided");
327328
}
328329
}
329330
}else{
330-
$default_mask = ClientManager::getMask("message");
331+
$default_mask = $this->config->getMask("message");
331332
if($default_mask != ""){
332333
$this->default_message_mask = $default_mask;
333334
}else{
334335
throw new MaskNotFoundException("Unknown message mask provided");
335336
}
336337

337-
$default_mask = ClientManager::getMask("attachment");
338+
$default_mask = $this->config->getMask("attachment");
338339
if($default_mask != ""){
339340
$this->default_attachment_mask = $default_mask;
340341
}else{
@@ -424,25 +425,25 @@ public function connect(): Client {
424425
$protocol = strtolower($this->protocol);
425426

426427
if (in_array($protocol, ['imap', 'imap4', 'imap4rev1'])) {
427-
$this->connection = new ImapProtocol($this->validate_cert, $this->encryption);
428+
$this->connection = new ImapProtocol($this->config, $this->validate_cert, $this->encryption);
428429
$this->connection->setConnectionTimeout($this->timeout);
429430
$this->connection->setProxy($this->proxy);
430431
}else{
431432
if (extension_loaded('imap') === false) {
432433
throw new ConnectionFailedException("connection setup failed", 0, new ProtocolNotSupportedException($protocol." is an unsupported protocol"));
433434
}
434-
$this->connection = new LegacyProtocol($this->validate_cert, $this->encryption);
435+
$this->connection = new LegacyProtocol($this->config, $this->validate_cert, $this->encryption);
435436
if (str_starts_with($protocol, "legacy-")) {
436437
$protocol = substr($protocol, 7);
437438
}
438439
$this->connection->setProtocol($protocol);
439440
}
440441

441-
if (ClientManager::get('options.debug')) {
442+
if ($this->config->get('options.debug')) {
442443
$this->connection->enableDebug();
443444
}
444445

445-
if (!ClientManager::get('options.uid_cache')) {
446+
if (!$this->config->get('options.uid_cache')) {
446447
$this->connection->disableUidCache();
447448
}
448449

@@ -507,7 +508,7 @@ public function disconnect(): Client {
507508
*/
508509
public function getFolder(string $folder_name, ?string $delimiter = null, bool $utf7 = false): ?Folder {
509510
// Set delimiter to false to force selection via getFolderByName (maybe useful for uncommon folder names)
510-
$delimiter = is_null($delimiter) ? ClientManager::get('options.delimiter', "/") : $delimiter;
511+
$delimiter = is_null($delimiter) ? $this->config->get('options.delimiter', "/") : $delimiter;
511512

512513
if (str_contains($folder_name, (string)$delimiter)) {
513514
return $this->getFolderByPath($folder_name, $utf7);

0 commit comments

Comments
 (0)