1
|
|
|
<?php |
2
|
|
|
namespace SmoothPhp\LaravelAdapter\Console; |
3
|
|
|
|
4
|
|
|
use Illuminate\Console\Command; |
5
|
|
|
use Illuminate\Contracts\Config\Repository; |
6
|
|
|
use Illuminate\Database\DatabaseManager; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ImportEventStore |
10
|
|
|
* @package SmoothPhp\LaravelAdapter\Console |
11
|
|
|
* @author Simon Bennett <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
final class ImportEventStore extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The name and signature of the console command. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $signature = 'smoothphp:import'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Import EventStore'; |
28
|
|
|
|
29
|
|
|
/** @var DatabaseManager */ |
30
|
|
|
private $databaseManager; |
31
|
|
|
|
32
|
|
|
/** @var Repository */ |
33
|
|
|
private $config; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* ExportEventStore constructor. |
37
|
|
|
* @param DatabaseManager $databaseManager |
38
|
|
|
* @param Repository $config |
39
|
|
|
*/ |
40
|
|
|
public function __construct(DatabaseManager $databaseManager, Repository $config) |
41
|
|
|
{ |
42
|
|
|
$this->databaseManager = $databaseManager; |
43
|
|
|
parent::__construct(); |
44
|
|
|
$this->config = $config; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Execute the console command. |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function handle() |
53
|
|
|
{ |
54
|
|
|
$fd = fopen("php://stdin", "r"); |
55
|
|
|
$eventsJson = ""; |
56
|
|
|
while (!feof($fd)) { |
57
|
|
|
$eventsJson .= fread($fd, 1024); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
fclose($fd); |
61
|
|
|
|
62
|
|
|
$events = json_decode($eventsJson, true); |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->truncate(); |
66
|
|
|
|
67
|
|
|
foreach ($events as $event) { |
68
|
|
|
$this->databaseManager |
69
|
|
|
->connection($this->config->get('cqrses.eventstore_connection')) |
70
|
|
|
->table($this->config->get('cqrses.eventstore_table')) |
71
|
|
|
->insert($event); |
72
|
|
|
} |
73
|
|
|
$this->call('smoothphp:rebuild'); |
74
|
|
|
|
75
|
|
|
return $this->line('Success'); |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
*/ |
82
|
|
|
private function truncate() |
83
|
|
|
{ |
84
|
|
|
$this->databaseManager |
85
|
|
|
->connection($this->config->get('cqrses.eventstore_connection')) |
86
|
|
|
->table($this->config->get('cqrses.eventstore_table')) |
87
|
|
|
->truncate(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|