Skip to content

Commit 73e7d09

Browse files
committed
Override batch notification recipient email
1 parent 93bc623 commit 73e7d09

File tree

5 files changed

+116
-15
lines changed

5 files changed

+116
-15
lines changed

Command/BatchCommand.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,20 @@ protected function configure()
3131
->setDescription('Launch a registered job instance')
3232
->addArgument('code', InputArgument::REQUIRED, 'Job instance code')
3333
->addArgument('execution', InputArgument::OPTIONAL, 'Job execution id')
34-
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Override job configuration (formatted as json. ie: php app/console oro:batch:job -c \'[{"reader":{"filePath":"/tmp/foo.csv"}}]\' acme_product_import)');
34+
->addOption(
35+
'config',
36+
'c',
37+
InputOption::VALUE_REQUIRED,
38+
'Override job configuration (formatted as json. ie: ' .
39+
'php app/console oro:batch:job -c \'[{"reader":{"filePath":"/tmp/foo.csv"}}]\' ' .
40+
'acme_product_import)'
41+
)
42+
->addOption(
43+
'email',
44+
null,
45+
InputOption::VALUE_REQUIRED,
46+
'The email to notify at the end of the job execution'
47+
);
3548
}
3649

3750
/**
@@ -62,6 +75,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
6275
);
6376
}
6477

78+
// Override mail notifier recipient email
79+
if ($email = $input->getOption('email')) {
80+
$this
81+
->getMailNotifier()
82+
->setRecipientEmail($email);
83+
}
84+
6585
$errors = $this->getValidator()->validate($jobInstance, array('Default', 'Execution'));
6686
if (count($errors) > 0) {
6787
throw new \RuntimeException(sprintf('Job "%s" is invalid: %s', $code, $this->getErrorMessages($errors)));
@@ -72,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7292
$jobExecution = $this->getEntityManager()->getRepository('OroBatchBundle:JobExecution')->find($executionId);
7393
if (!$jobExecution) {
7494
throw new \InvalidArgumentException(sprintf('Could not find job execution "%s".', $id));
75-
}
95+
}
7696
if (!$jobExecution->getStatus()->isStarting()) {
7797
throw new \RuntimeException(
7898
sprintf('Job execution "%s" has invalid status: %s', $executionId, $jobExecution->getStatus())
@@ -124,6 +144,14 @@ protected function getValidator()
124144
return $this->getContainer()->get('validator');
125145
}
126146

147+
/**
148+
* @return Validator
149+
*/
150+
protected function getMailNotifier()
151+
{
152+
return $this->getContainer()->get('oro_batch.mail_notifier');
153+
}
154+
127155
/**
128156
* @return \Oro\Bundle\BatchBundle\Connector\ConnectorRegistry
129157
*/

Notification/MailNotifier.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class MailNotifier implements Notifier
3737
*/
3838
protected $senderEmail;
3939

40+
/**
41+
* @var string $recipientEmail
42+
*/
43+
protected $recipientEmail;
44+
4045
/**
4146
* @param BatchLogHandler $logger
4247
* @param SecurityContextInterface $securityContext
@@ -58,18 +63,30 @@ public function __construct(
5863
$this->senderEmail = $senderEmail;
5964
}
6065

66+
/**
67+
* Set the recipient email
68+
*
69+
* @param string $recipientEmail
70+
*
71+
* @return MailNotifier
72+
*/
73+
public function setRecipientEmail($recipientEmail)
74+
{
75+
$this->recipientEmail = $recipientEmail;
76+
77+
return $this;
78+
}
79+
6180
/**
6281
* {@inheritdoc}
6382
*/
6483
public function notify(JobExecution $jobExecution)
6584
{
66-
$user = $this->getUser();
67-
if (!$user) {
85+
if (null === $email = $this->getEmail()) {
6886
return;
6987
}
7088

7189
$parameters = array(
72-
'user' => $user,
7390
'jobExecution' => $jobExecution,
7491
'log' => $this->logger->getFilename(),
7592
);
@@ -80,7 +97,7 @@ public function notify(JobExecution $jobExecution)
8097
$message = $this->mailer->createMessage();
8198
$message->setSubject('Job has been executed');
8299
$message->setFrom($this->senderEmail);
83-
$message->setTo($user->getEmail());
100+
$message->setTo($email);
84101
$message->setBody($txtBody, 'text/plain');
85102
$message->addPart($htmlBody, 'text/html');
86103

@@ -90,10 +107,14 @@ public function notify(JobExecution $jobExecution)
90107
/**
91108
* Get the current authenticated user
92109
*
93-
* @return null|UserInterface
110+
* @return null|string
94111
*/
95-
private function getUser()
112+
private function getEmail()
96113
{
114+
if ($this->recipientEmail) {
115+
return $this->recipientEmail;
116+
}
117+
97118
if (null === $token = $this->securityContext->getToken()) {
98119
return;
99120
}
@@ -102,6 +123,6 @@ private function getUser()
102123
return;
103124
}
104125

105-
return $user;
126+
return $user->getEmail();
106127
}
107128
}

Resources/views/Mails/notification.html.twig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
<p>{{ user.firstName }} {{ user.lastName }},</p>
2-
31
<p>
42
Akeneo successfully completed your batch {{ jobExecution.jobInstance.type }}.<br />
53
<br />

Resources/views/Mails/notification.txt.twig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{{ user.firstName }} {{ user.lastName }},
2-
31
Akeneo successfully completed your batch {{ jobExecution.jobInstance.type }}.
42

53
Started on {{ jobExecution.startTime|date("Y-m-d") }} at {{ jobExecution.startTime|date("H:i:s") }}.
@@ -12,5 +10,5 @@ Results:
1210
- {{ stepExecution.writeCount }} item(s) written
1311
{% endfor %}
1412

15-
--
13+
--
1614
Oro Platform

Tests/Unit/Notification/MailNotifierTest.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public function testNotifyWithLoggedInUserEmail()
5151

5252
$jobExecution = $this->getDisabledConstructorMock('Oro\Bundle\BatchBundle\Entity\JobExecution');
5353
$parameters = array(
54-
'user' => $user,
5554
'jobExecution' => $jobExecution,
5655
'log' => '/tmp/foo.log',
5756
);
@@ -102,6 +101,63 @@ public function testNotifyWithLoggedInUserEmail()
102101
$this->notifier->notify($jobExecution);
103102
}
104103

104+
public function testNotifyIfRecipientEmailIsSet()
105+
{
106+
$this->handler
107+
->expects($this->once())
108+
->method('getFilename')
109+
->will($this->returnValue('/tmp/foo.log'));
110+
111+
$jobExecution = $this->getDisabledConstructorMock('Oro\Bundle\BatchBundle\Entity\JobExecution');
112+
$parameters = array(
113+
'jobExecution' => $jobExecution,
114+
'log' => '/tmp/foo.log',
115+
);
116+
$this->twig
117+
->expects($this->exactly(2))
118+
->method('render')
119+
->will(
120+
$this->returnValueMap(
121+
array(
122+
array('OroBatchBundle:Mails:notification.txt.twig', $parameters, 'notification'),
123+
array('OroBatchBundle:Mails:notification.html.twig', $parameters, '<p>notification</p>'),
124+
)
125+
)
126+
);
127+
128+
$message = $this->getDisabledConstructorMock('\Swift_Message');
129+
$this->mailer
130+
->expects($this->once())
131+
->method('createMessage')
132+
->will($this->returnValue($message));
133+
134+
$message->expects($this->once())
135+
->method('setSubject')
136+
->with('Job has been executed');
137+
138+
$message->expects($this->once())
139+
->method('setFrom')
140+
->with('[email protected]');
141+
142+
$message->expects($this->once())
143+
->method('setTo')
144+
->with('[email protected]');
145+
$message->expects($this->once())
146+
->method('setBody')
147+
->with('notification', 'text/plain');
148+
$message->expects($this->once())
149+
->method('addPart')
150+
->with('<p>notification</p>', 'text/html');
151+
152+
$this->mailer
153+
->expects($this->once())
154+
->method('send')
155+
->with($message);
156+
157+
$this->notifier->setRecipientEmail('[email protected]');
158+
$this->notifier->notify($jobExecution);
159+
}
160+
105161
public function testDoNotNotifyIfNoUserLoggedIn()
106162
{
107163
$token = $this->getTokenMock(null);

0 commit comments

Comments
 (0)