-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMailQueueExtension.php
More file actions
90 lines (73 loc) · 2.56 KB
/
Copy pathMailQueueExtension.php
File metadata and controls
90 lines (73 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace ADT\MailQueue\DI;
use ADT\MailQueue\Console;
use ADT\MailQueue\Entity;
use ADT\MailQueue\Service;
class MailQueueExtension extends \Nette\DI\CompilerExtension {
public function loadConfiguration() {
$config = $this->validateConfig([
'mailer' => NULL,
'messenger' => NULL,
'queueEntityClass' => Entity\MailQueueEntry::class,
'autowireMailer' => FALSE,
'sendErrorHandler' => NULL,
'onQueueDrained' => NULL,
'lockTimeout' => 600,
'limit' => 1000,
'tempDir' => $this->getContainerBuilder()->parameters['tempDir'],
'backgroundQueueService' => NULL,
'backgroundQueueCallbackName' => NULL,
]);
if (!empty($config['messenger']) && !empty($config['mailer'])) {
throw new \Nette\InvalidArgumentException('Cannot specify both mailer and messenger at the same time.');
}
if (empty($config['messenger']) && empty($config['mailer'])) {
throw new \Nette\InvalidArgumentException('Please specify mailer or messenger service class (e.g. @ServiceClass).');
}
if (!is_a($config['queueEntityClass'], Entity\AbstractMailQueueEntry::class, TRUE)) {
throw new \Nette\InvalidArgumentException('Invalid Queue entity class.');
}
if (empty($config['backgroundQueueService'])) {
throw new \Nette\InvalidArgumentException('You have to set "backgroundQueueService". E.g. "@ADT\BackgroundQueue\Service".');
}
if (empty($config['backgroundQueueCallbackName'])) {
throw new \Nette\InvalidArgumentException('You have to set "backgroundQueueCallbackName". E.g. "mailSending".');
}
// Queue service
$service = $config['mailer'] ?: $config['messenger'];
$queueService = $this->getContainerBuilder()
->addDefinition($this->prefix('queue'))
->setClass(Service\QueueService::class)
->setArguments([
$config,
])
->addSetup(
$config['mailer']
? '$service->setMailer(?)'
: '$service->setMessenger(?)',
[ $service, ]
);
if (!empty($config['sendErrorHandler'])) {
$queueService->addSetup(
'$service->setSendErrorHandler(?)',
[ $config['sendErrorHandler'], ]
);
}
if (!empty($config['onQueueDrained'])) {
$queueService->addSetup(
'$service->onQueueDrained[] = ?',
[ $config['onQueueDrained'], ]
);
}
// Mailer service
$this->getContainerBuilder()
->addDefinition($this->prefix('mailer'))
->setClass(Service\QueueMailer::class)
->setAutowired($config['autowireMailer']);
// Process command
$this->getContainerBuilder()
->addDefinition($this->prefix('command.process'))
->setClass(Console\ProcessCommand::class)
->addTag('kdyby.console.command');
}
}