-
Notifications
You must be signed in to change notification settings - Fork 0
/
InvoiceExportWorker.php
125 lines (107 loc) · 3.23 KB
/
InvoiceExportWorker.php
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Drupal\art_invoice\Plugin\QueueWorker;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Queue\QueueWorkerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class InvoiceExportWorker.
*
* @QueueWorker(
* id = "invoice_export",
* title = @Translation("Invoice export"),
* cron = {"time" = 30}
* )
*/
class InvoiceExportWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {
/**
* Temp database storage for export.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $forExport;
/**
* Export target file storage.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $export;
/**
* The config manager.
*
* @var ConfigManagerInterface
*/
protected $configManager;
/**
* The queue factory.
*
* @var \Drupal\Core\Queue\QueueInterface
*/
protected $queue;
/**
* InvoiceExportWorker constructor.
*
* @param array $configuration
* The configuration array.
* @param string $plugin_id
* The plugin name.
* @param mixed $plugin_definition
* The plugin definition.
* @param \Drupal\Core\Config\StorageInterface $for_export
* Temp database storage for export.
* @param \Drupal\Core\Config\StorageInterface $export
* Export target file storage.
* @param \Drupal\Core\Config\ConfigManagerInterface $configManager
* The config manager.
* @param \Drupal\Core\Queue\QueueFactory $queueFactory
* The queue factory.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, StorageInterface $for_export, StorageInterface $export, ConfigManagerInterface $configManager, QueueFactory $queueFactory) {
$this->forExport = $for_export;
$this->export = $export;
$this->configManager = $configManager;
$this->queue = $queueFactory->get('invoice_export');
$this->initialize();
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('art_invoice.storage.for_export'),
$container->get('art_invoice.storage.export'),
$container->get('config.manager'),
$container->get('queue')
);
}
/**
* Import initialize.
*/
public function initialize() {
if (REQUEST_TIME - \Drupal::state()->get('invoice_export.last_run', 0) <= 24 * 60 * 60) {
return;
}
\Drupal::state()->set('invoice_export.last_run', REQUEST_TIME);
$export_data = [];
foreach ($this->forExport->listAll() as $item) {
if ($data = $this->forExport->read($item)) {
$this->forExport->delete($item);
$export_data[] = $data;
}
}
$this->queue->createItem($export_data);
}
/**
* {@inheritdoc}
*/
public function processItem($data) {
$invoice_pack = date('Y-m-d-H-i-s') . '_paid_invoices';
$this->export->write($invoice_pack, $data);
}
}