1
|
|
|
<?php |
2
|
|
|
namespace AcMailer\Service; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class ConfigMigrationService |
6
|
|
|
* @author Alejandro Celaya Alastrué |
7
|
|
|
* @link http://www.alejandrocelaya.com |
8
|
|
|
*/ |
9
|
|
|
class ConfigMigrationService implements ConfigMigrationServiceInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Reads a configuration array with the structure used in version 4.5 and earlier and parses it to the new one |
13
|
|
|
* |
14
|
|
|
* @param array $oldConfig |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
5 |
|
public function parseConfig(array $oldConfig) |
18
|
|
|
{ |
19
|
|
|
$newConfig = [ |
20
|
5 |
|
'message_options' => [], |
21
|
5 |
|
'smtp_options' => [], |
22
|
5 |
|
'file_options' => [] |
23
|
5 |
|
]; |
24
|
|
|
|
25
|
|
|
// Mail acapter |
26
|
5 |
|
if (isset($oldConfig['mail_adapter_service'])) { |
27
|
1 |
|
$newConfig['mail_adapter'] = $oldConfig['mail_adapter_service']; |
28
|
5 |
|
} elseif (isset($oldConfig['mail_adapter'])) { |
29
|
1 |
|
$newConfig['mail_adapter'] = $oldConfig['mail_adapter']; |
30
|
1 |
|
} |
31
|
|
|
|
32
|
|
|
// Common message options |
33
|
5 |
|
if (isset($oldConfig['from'])) { |
34
|
1 |
|
$newConfig['message_options']['from'] = $oldConfig['from']; |
35
|
1 |
|
} |
36
|
5 |
|
if (isset($oldConfig['from_name'])) { |
37
|
1 |
|
$newConfig['message_options']['from_name'] = $oldConfig['from_name']; |
38
|
1 |
|
} |
39
|
5 |
|
if (isset($oldConfig['to'])) { |
40
|
1 |
|
$newConfig['message_options']['to'] = $oldConfig['to']; |
41
|
1 |
|
} |
42
|
5 |
|
if (isset($oldConfig['cc'])) { |
43
|
1 |
|
$newConfig['message_options']['cc'] = $oldConfig['cc']; |
44
|
1 |
|
} |
45
|
5 |
|
if (isset($oldConfig['bcc'])) { |
46
|
1 |
|
$newConfig['message_options']['bcc'] = $oldConfig['bcc']; |
47
|
1 |
|
} |
48
|
5 |
|
if (isset($oldConfig['subject'])) { |
49
|
1 |
|
$newConfig['message_options']['subject'] = $oldConfig['subject']; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
// Body |
53
|
5 |
|
$newConfig['message_options']['body'] = []; |
54
|
5 |
|
if (isset($oldConfig['body'])) { |
55
|
1 |
|
$newConfig['message_options']['body']['content'] = $oldConfig['body']; |
56
|
1 |
|
} |
57
|
5 |
|
if (isset($oldConfig['body_charset'])) { |
58
|
1 |
|
$newConfig['message_options']['body']['charset'] = $oldConfig['body_charset']; |
59
|
1 |
|
} |
60
|
5 |
|
if (isset($oldConfig['template'])) { |
61
|
1 |
|
$newConfig['message_options']['body']['template'] = $oldConfig['template']; |
62
|
1 |
|
$newConfig['message_options']['body']['use_template'] = $oldConfig['template']['use_template']; |
63
|
1 |
|
unset($newConfig['message_options']['body']['template']['use_template']); |
64
|
1 |
|
$newConfig['message_options']['body']['template']['default_layout'] = []; |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
// Attachments |
68
|
5 |
|
if (isset($oldConfig['attachments'])) { |
69
|
1 |
|
$newConfig['message_options']['attachments'] = $oldConfig['attachments']; |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
// SMTP |
73
|
5 |
|
if (isset($oldConfig['server'])) { |
74
|
1 |
|
$newConfig['smtp_options']['host'] = $oldConfig['server']; |
75
|
1 |
|
} |
76
|
5 |
|
if (isset($oldConfig['port'])) { |
77
|
1 |
|
$newConfig['smtp_options']['port'] = $oldConfig['port']; |
78
|
1 |
|
} |
79
|
5 |
|
if (isset($oldConfig['connection_class'])) { |
80
|
1 |
|
$newConfig['smtp_options']['connection_class'] = $oldConfig['connection_class']; |
81
|
1 |
|
} |
82
|
5 |
|
$newConfig['smtp_options']['connection_config'] = []; |
83
|
5 |
View Code Duplication |
if (isset($oldConfig['smtp_user'])) { |
|
|
|
|
84
|
1 |
|
$newConfig['smtp_options']['connection_config']['username'] = $oldConfig['smtp_user']; |
85
|
1 |
|
} |
86
|
5 |
View Code Duplication |
if (isset($oldConfig['smtp_user'])) { |
|
|
|
|
87
|
1 |
|
$newConfig['smtp_options']['connection_config']['username'] = $oldConfig['smtp_user']; |
88
|
1 |
|
} |
89
|
5 |
|
if (isset($oldConfig['smtp_password'])) { |
90
|
1 |
|
$newConfig['smtp_options']['connection_config']['password'] = $oldConfig['smtp_password']; |
91
|
1 |
|
} |
92
|
5 |
|
if (isset($oldConfig['ssl'])) { |
93
|
1 |
|
$newConfig['smtp_options']['connection_config']['ssl'] = $oldConfig['ssl']; |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
// File |
97
|
5 |
|
if (isset($oldConfig['file_path'])) { |
98
|
1 |
|
$newConfig['file_options']['path'] = $oldConfig['file_path']; |
99
|
1 |
|
} |
100
|
5 |
|
if (isset($oldConfig['file_callback'])) { |
101
|
1 |
|
$newConfig['file_options']['callback'] = $oldConfig['file_callback']; |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
return [ |
105
|
|
|
'acmailer_options' => [ |
106
|
|
|
'default' => $newConfig |
107
|
5 |
|
] |
108
|
5 |
|
]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.