|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
|
4
|
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationStep; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\ReferenceResolverInterface; |
|
7
|
|
|
use Kaliop\eZMigrationBundle\Core\ReferenceResolver\PrefixBasedResolverInterface; |
|
8
|
|
|
use Swift_Message; |
|
9
|
|
|
use Swift_Attachment; |
|
10
|
|
|
|
|
11
|
|
|
class MailExecutor extends AbstractExecutor |
|
12
|
|
|
{ |
|
13
|
|
|
protected $supportedStepTypes = array('mail'); |
|
14
|
|
|
protected $supportedActions = array('send'); |
|
15
|
|
|
|
|
16
|
|
|
protected $mailService; |
|
17
|
|
|
/** @var ReferenceResolverInterface $referenceResolver */ |
|
18
|
|
|
protected $referenceResolver; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct($mailService, PrefixBasedResolverInterface $referenceResolver) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->mailService = $mailService; |
|
23
|
|
|
$this->referenceResolver = $referenceResolver; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param MigrationStep $step |
|
28
|
|
|
* @return mixed |
|
29
|
|
|
* @throws \Exception |
|
30
|
|
|
*/ |
|
31
|
|
View Code Duplication |
public function execute(MigrationStep $step) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
parent::execute($step); |
|
34
|
|
|
|
|
35
|
|
|
if (!isset($step->dsl['mode'])) { |
|
36
|
|
|
throw new \Exception("Invalid step definition: missing 'mode'"); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$action = $step->dsl['mode']; |
|
40
|
|
|
|
|
41
|
|
|
if (!in_array($action, $this->supportedActions)) { |
|
42
|
|
|
throw new \Exception("Invalid step definition: value '$action' is not allowed for 'mode'"); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->$action($step->dsl, $step->context); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param array $dsl |
|
50
|
|
|
* @param array $context |
|
51
|
|
|
* @return true |
|
52
|
|
|
* @throws \Exception |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function send($dsl, $context) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
|
|
57
|
|
|
$message = Swift_Message::newInstance(); |
|
58
|
|
|
|
|
59
|
|
|
if (isset($dsl['from'])) { |
|
60
|
|
|
$message->setFrom($this->resolveReferencesRecursively($dsl['from'])); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
if (isset($dsl['to'])) { |
|
63
|
|
|
$message->setTo($this->resolveReferencesRecursively($dsl['to'])); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
if (isset($dsl['cc'])) { |
|
66
|
|
|
$message->setCc($this->resolveReferencesRecursively($dsl['cc'])); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
if (isset($dsl['bcc'])) { |
|
69
|
|
|
$message->setBcc($this->resolveReferencesRecursively($dsl['bcc'])); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
if (isset($dsl['subject'])) { |
|
72
|
|
|
$message->setSubject($this->resolveReferencesInText($dsl['subject'])); |
|
73
|
|
|
} |
|
74
|
|
|
if (isset($dsl['body'])) { |
|
75
|
|
|
$message->setBody($this->resolveReferencesInText($dsl['body'])); |
|
76
|
|
|
} |
|
77
|
|
|
if (isset($dsl['attach'])) { /// @todo |
|
78
|
|
|
$message->attach(Swift_Attachment::fromPath($this->resolveReferencesRecursively($dsl['attach']))); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (isset($dsl['priority'])) { |
|
82
|
|
|
$message->setPriority($this->resolveReferencesRecursively($dsl['priority'])); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
if (isset($dsl['read_receipt_to'])) { |
|
85
|
|
|
$message->setReadReceiptTo($this->resolveReferencesRecursively($dsl['read_receipt_to'])); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
if (isset($dsl['return_path'])) { |
|
88
|
|
|
$message->setReturnPath($this->resolveReferencesRecursively($dsl['return_path'])); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
if (isset($dsl['reply_to'])) { |
|
91
|
|
|
$message->setReplyTo($this->resolveReferencesRecursively($dsl['reply_to'])); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
if (isset($dsl['sender'])) { |
|
94
|
|
|
$message->setSender($this->resolveReferencesRecursively($dsl['sender'])); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->mailService->send($message); |
|
98
|
|
|
|
|
99
|
|
|
// q: shall we set any reference? |
|
100
|
|
|
|
|
101
|
|
|
// q: what to return? |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @deprecated should be moved into the reference resolver classes |
|
107
|
|
|
*/ |
|
108
|
|
View Code Duplication |
protected function resolveReferencesRecursively($match) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
if (is_array($match)) { |
|
111
|
|
|
foreach ($match as $condition => $values) { |
|
112
|
|
|
$match[$condition] = $this->resolveReferencesRecursively($values); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
return $match; |
|
115
|
|
|
} else { |
|
116
|
|
|
return $this->referenceResolver->resolveReference($match); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Replaces any references inside a string |
|
122
|
|
|
* |
|
123
|
|
|
* @param string |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function resolveReferencesInText($text) |
|
127
|
|
|
{ |
|
128
|
|
|
// we need to alter the regexp we get from the resolver, as it will be used to match parts of text, not the whole string |
|
129
|
|
|
$regexp = substr($this->referenceResolver->getRegexp(), 1, -1); |
|
|
|
|
|
|
130
|
|
|
// NB: here we assume that all regexp resolvers give us a regexp with a very specific format... |
|
131
|
|
|
$regexp = '/\[' . preg_replace(array('/^\^/'), array('', ''), $regexp) . '[^]]+\]/'; |
|
132
|
|
|
|
|
133
|
|
|
$count = preg_match_all($regexp, $text, $matches); |
|
134
|
|
|
// $matches[0][] will have the matched full string eg.: [reference:example_reference] |
|
135
|
|
View Code Duplication |
if ($count) { |
|
|
|
|
|
|
136
|
|
|
foreach ($matches[0] as $referenceIdentifier) { |
|
137
|
|
|
$reference = $this->referenceResolver->getReferenceValue(substr($referenceIdentifier, 1, -1)); |
|
138
|
|
|
$text = str_replace($referenceIdentifier, $reference, $text); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $text; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
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.