-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDatabaseHandler.php
87 lines (77 loc) · 2.37 KB
/
DatabaseHandler.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
<?php
namespace Jh\Import\Report\Handler;
use Jh\Import\Report\Message;
use Jh\Import\Report\Report;
use Jh\Import\Report\ReportItem;
use Magento\Framework\App\ResourceConnection;
/**
* @author Aydin Hassan <[email protected]>
*/
class DatabaseHandler implements Handler
{
/**
* @var \Magento\Framework\DB\Adapter\Pdo\Mysql
*/
private $adapter;
/**
* @var int|null
*/
private $id;
public function __construct(ResourceConnection $resourceConnection)
{
$this->adapter = $resourceConnection->getConnection();
}
public function start(Report $report, \DateTime $startTime): void
{
$this->adapter->insert('jh_import_history', [
'started' => $startTime->format('Y-m-d H:i:s'),
'import_name' => $report->getImportName(),
'source_id' => $report->getSourceId()
]);
$this->id = $this->adapter->lastInsertId();
}
public function finish(Report $report, \DateTime $finishTime, int $memoryUsage): void
{
$this->adapter->update(
'jh_import_history',
[
'finished' => $finishTime->format('Y-m-d H:i:s'),
'memory_usage' => $memoryUsage
],
[
'id = ?' => $this->id
]
);
}
/**
* TODO: Batch insert
*
* @param Message $message
*/
public function handleMessage(Message $message): void
{
$this->adapter->insert('jh_import_history_log', [
'history_id' => $this->id,
'log_level' => $message->getLogLevel(),
'message' => $message->getMessage(),
'created' => $message->getDateTime()->format('Y-m-d H:i:s')
]);
}
/**
* TODO: Batch insert
*
* @param Message $message
*/
public function handleItemMessage(ReportItem $item, Message $message): void
{
$this->adapter->insert('jh_import_history_item_log', [
'history_id' => $this->id,
'log_level' => $message->getLogLevel(),
'reference_line' => $item->getReferenceLine(),
'id_field' => $item->getIdField(),
'id_value' => $item->getIdValue(),
'created' => $message->getDateTime()->format('Y-m-d H:i:s'),
'message' => $message->getMessage(),
]);
}
}