1: <?php
2:
3: namespace Charcoal\Email\Script;
4:
5:
6: use \Psr\Http\Message\RequestInterface;
7: use \Psr\Http\Message\ResponseInterface;
8:
9: use \Pimple\Container;
10:
11:
12: use \Charcoal\App\Script\AbstractScript;
13: use \Charcoal\App\Script\CronScriptInterface;
14: use \Charcoal\App\Script\CronScriptTrait;
15:
16:
17: use \Charcoal\Factory\FactoryInterface;
18:
19:
20: use \Charcoal\Email\EmailQueueManager;
21:
22: 23: 24: 25: 26:
27: class ProcessQueueScript extends AbstractScript implements CronScriptInterface
28: {
29: use CronScriptTrait;
30:
31: 32: 33:
34: private $queueItemFactory;
35:
36: 37: 38: 39: 40:
41: private $sent;
42:
43: 44: 45: 46:
47: public function setDependencies(Container $container)
48: {
49: parent::setDependencies($container);
50: $this->setQueueItemFactory($container['model/factory']);
51: }
52:
53: 54: 55: 56:
57: private function setQueueItemFactory(FactoryInterface $factory)
58: {
59: $this->queueItemFactory = $factory;
60: }
61:
62: 63: 64: 65: 66: 67: 68:
69: public function run(RequestInterface $request, ResponseInterface $response)
70: {
71:
72: unset($request);
73:
74: $this->startLock();
75:
76: $climate = $this->climate();
77:
78: $processedCallback = function($success, $failures, $skipped) use ($climate) {
79: if (!empty($success)) {
80: $climate->green()->out(sprintf('%s emails were successfully sent.', count($success)));
81: }
82:
83: if (!empty($failures)) {
84: $climate->red()->out(sprintf('%s emails were not successfully sent', count($failures)));
85: }
86:
87: if (!empty($skipped)) {
88: $climate->orange()->out(sprintf('%s emails were skipped.', count($skipped)));
89: }
90: };
91:
92: $queueManager = new EmailQueueManager([
93: 'logger' => $this->logger,
94: 'queue_item_factory' => $this->queueItemFactory
95: ]);
96: $queueManager->setProcessedCallback($processedCallback);
97: $queueManager->processQueue();
98:
99: $this->stopLock();
100:
101: return $response;
102: }
103: }
104: