1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmoothPhp\LaravelAdapter\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Contracts\Config\Repository; |
7
|
|
|
use Illuminate\Database\DatabaseManager; |
8
|
|
|
use SmoothPhp\LaravelAdapter\Exception\EventStoreFileNotFound; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ImportEventStore |
12
|
|
|
* @package SmoothPhp\LaravelAdapter\Console |
13
|
|
|
* @author Simon Bennett <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
final class ImportEventStore extends Command |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The name and signature of the console command. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'smoothphp:import {--file= : If specified, import from a JSON file}'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Import EventStore'; |
30
|
|
|
|
31
|
|
|
/** @var DatabaseManager */ |
32
|
|
|
private $databaseManager; |
33
|
|
|
|
34
|
|
|
/** @var Repository */ |
35
|
|
|
private $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* ExportEventStore constructor. |
39
|
|
|
* @param DatabaseManager $databaseManager |
40
|
|
|
* @param Repository $config |
41
|
|
|
*/ |
42
|
|
|
public function __construct(DatabaseManager $databaseManager, Repository $config) |
43
|
|
|
{ |
44
|
|
|
$this->databaseManager = $databaseManager; |
45
|
|
|
parent::__construct(); |
46
|
|
|
$this->config = $config; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Execute the console command. |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function handle() |
55
|
|
|
{ |
56
|
|
|
$events = json_decode(!!$this->option('file') ? $this->readFromJsonFile() : $this->readFromStdIn(), true); |
57
|
|
|
|
58
|
|
|
$this->truncate(); |
59
|
|
|
|
60
|
|
|
foreach ($events as $event) { |
61
|
|
|
$this->databaseManager |
62
|
|
|
->connection($this->config->get('cqrses.eventstore_connection')) |
63
|
|
|
->table($this->config->get('cqrses.eventstore_table')) |
64
|
|
|
->insert($event); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->call('smoothphp:rebuild'); |
68
|
|
|
|
69
|
|
|
return $this->line('Success'); |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Truncate (empty) event store table |
75
|
|
|
*/ |
76
|
|
|
private function truncate() |
77
|
|
|
{ |
78
|
|
|
$this->databaseManager |
79
|
|
|
->connection($this->config->get('cqrses.eventstore_connection')) |
80
|
|
|
->table($this->config->get('cqrses.eventstore_table')) |
81
|
|
|
->truncate(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Read event store JSON from std in |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
private function readFromStdIn() |
90
|
|
|
{ |
91
|
|
|
$fd = fopen("php://stdin", "r"); |
92
|
|
|
$eventsJson = ""; |
93
|
|
|
while (!feof($fd)) { |
94
|
|
|
$eventsJson .= fread($fd, 1024); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
fclose($fd); |
98
|
|
|
|
99
|
|
|
return $eventsJson; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Read event store JSON from file |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
private function readFromJsonFile() |
108
|
|
|
{ |
109
|
|
|
if (!file_exists($path = $this->option('file'))) { |
110
|
|
|
throw new EventStoreFileNotFound(sprintf('%s/%s', __DIR__, $path)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return file_get_contents($path); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|