Skip to content

Commit 1995b63

Browse files
committed
Merge branch 'maintenance/pim_beta1' into feature/invalid-item-handling
2 parents 9076ee0 + 6381923 commit 1995b63

File tree

9 files changed

+136
-21
lines changed

9 files changed

+136
-21
lines changed

Command/BatchCommand.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Console\Output\OutputInterface;
1010
use Symfony\Component\Validator\ConstraintViolationList;
1111
use Symfony\Component\Validator\Validator;
12+
use Symfony\Component\Validator\Constraints as Assert;
1213
use Monolog\Handler\StreamHandler;
1314
use Doctrine\ORM\EntityManager;
1415
use Oro\Bundle\BatchBundle\Entity\JobExecution;
@@ -31,7 +32,20 @@ protected function configure()
3132
->setDescription('Launch a registered job instance')
3233
->addArgument('code', InputArgument::REQUIRED, 'Job instance code')
3334
->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)');
35+
->addOption(
36+
'config',
37+
'c',
38+
InputOption::VALUE_REQUIRED,
39+
'Override job configuration (formatted as json. ie: ' .
40+
'php app/console oro:batch:job -c \'[{"reader":{"filePath":"/tmp/foo.csv"}}]\' ' .
41+
'acme_product_import)'
42+
)
43+
->addOption(
44+
'email',
45+
null,
46+
InputOption::VALUE_REQUIRED,
47+
'The email to notify at the end of the job execution'
48+
);
3549
}
3650

3751
/**
@@ -62,17 +76,34 @@ protected function execute(InputInterface $input, OutputInterface $output)
6276
);
6377
}
6478

65-
$errors = $this->getValidator()->validate($jobInstance, array('Default', 'Execution'));
79+
$validator = $this->getValidator();
80+
81+
// Override mail notifier recipient email
82+
if ($email = $input->getOption('email')) {
83+
$errors = $validator->validateValue($email, new Assert\Email());
84+
if (count($errors) > 0) {
85+
throw new \RuntimeException(
86+
sprintf('Email "%s" is invalid: %s', $email, $this->getErrorMessages($errors))
87+
);
88+
}
89+
$this
90+
->getMailNotifier()
91+
->setRecipientEmail($email);
92+
}
93+
94+
$errors = $validator->validate($jobInstance, array('Default', 'Execution'));
6695
if (count($errors) > 0) {
67-
throw new \RuntimeException(sprintf('Job "%s" is invalid: %s', $code, $this->getErrorMessages($errors)));
96+
throw new \RuntimeException(
97+
sprintf('Job "%s" is invalid: %s', $code, $this->getErrorMessages($errors))
98+
);
6899
}
69100

70101
$executionId = $input->getArgument('execution');
71102
if ($executionId) {
72103
$jobExecution = $this->getEntityManager()->getRepository('OroBatchBundle:JobExecution')->find($executionId);
73104
if (!$jobExecution) {
74105
throw new \InvalidArgumentException(sprintf('Could not find job execution "%s".', $id));
75-
}
106+
}
76107
if (!$jobExecution->getStatus()->isStarting()) {
77108
throw new \RuntimeException(
78109
sprintf('Job execution "%s" has invalid status: %s', $executionId, $jobExecution->getStatus())
@@ -124,6 +155,14 @@ protected function getValidator()
124155
return $this->getContainer()->get('validator');
125156
}
126157

158+
/**
159+
* @return Validator
160+
*/
161+
protected function getMailNotifier()
162+
{
163+
return $this->getContainer()->get('oro_batch.mail_notifier');
164+
}
165+
127166
/**
128167
* @return \Oro\Bundle\BatchBundle\Connector\ConnectorRegistry
129168
*/

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function getConfigTreeBuilder()
2222

2323
$root
2424
->children()
25+
->booleanNode('enable_mail_notification')->defaultFalse()->end()
2526
->scalarNode('sender_email')->defaultValue('[email protected]')->end()
2627
->end()
2728
->end();

DependencyInjection/OroBatchExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,10 @@ public function load(array $configs, ContainerBuilder $container)
2525
$loader->load('services.yml');
2626

2727
$container->setParameter('oro_batch.mail_notifier.sender_email', $config['sender_email']);
28+
if ($config['enable_mail_notification']) {
29+
$container
30+
->getDefinition('oro_batch.mail_notifier')
31+
->addTag('oro_batch.notifier');
32+
}
2833
}
2934
}

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/config/services.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ services:
5151
- @twig
5252
- @mailer
5353
- %oro_batch.mail_notifier.sender_email%
54-
# There is no need to send email on each job
55-
# tags:
56-
# - { name: oro_batch.notifier }
5754

5855
oro_batch.step_factory:
5956
class: %oro_batch.step_factory.class%

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/Job/JobTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public function testAddStep()
292292
$this->job->addStep('name1', $mockStep1);
293293
$this->job->addStep('name2', $mockStep2);
294294

295-
$this->assertEquals(array('name1' => $mockStep1, 'name2' => $mockStep2), $this->job->getSteps());
295+
$this->assertEquals(array($mockStep1, $mockStep2), $this->job->getSteps());
296296
}
297297

298298
public function testSetSteps()

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)