1
|
|
|
<?php |
2
|
|
|
namespace Triadev\EsMigration\Business\Repository; |
3
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
5
|
|
|
use Triadev\EsMigration\Business\Events\MigrationStepDone; |
6
|
|
|
use Triadev\EsMigration\Business\Events\MigrationStepError; |
7
|
|
|
use Triadev\EsMigration\Business\Events\MigrationStepRunning; |
8
|
|
|
use Triadev\EsMigration\Business\Mapper\MigrationStatus; |
9
|
|
|
use Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationStepContract; |
10
|
|
|
use Triadev\EsMigration\Exception\MigrationsNotExist; |
11
|
|
|
|
12
|
|
|
class ElasticsearchMigrationStep implements ElasticsearchMigrationStepContract |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @inheritdoc |
16
|
|
|
*/ |
17
|
19 |
|
public function create( |
18
|
|
|
int $migrationId, |
19
|
|
|
string $type, |
20
|
|
|
array $params = [], |
21
|
|
|
int $priority = 1, |
22
|
|
|
bool $stopOnFailure = true |
23
|
|
|
): \Triadev\EsMigration\Models\Entity\ElasticsearchMigrationStep { |
24
|
19 |
|
$dbMigration = new \Triadev\EsMigration\Models\Entity\ElasticsearchMigrationStep(); |
25
|
|
|
|
26
|
19 |
|
$dbMigration->migration_id = $migrationId; |
27
|
19 |
|
$dbMigration->type = $type; |
28
|
19 |
|
$dbMigration->status = MigrationStatus::MIGRATION_STATUS_WAIT; |
29
|
19 |
|
$dbMigration->params = json_encode($params); |
30
|
19 |
|
$dbMigration->priority = $priority; |
31
|
19 |
|
$dbMigration->stop_on_failure = $stopOnFailure; |
32
|
|
|
|
33
|
19 |
|
$dbMigration->saveOrFail(); |
34
|
|
|
|
35
|
19 |
|
return $dbMigration; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
9 |
|
public function update( |
42
|
|
|
int $migrationStepId, |
43
|
|
|
int $status, |
44
|
|
|
?string $error = null, |
45
|
|
|
?int $priority = null, |
46
|
|
|
?bool $stopOnFailure = null |
47
|
|
|
): \Triadev\EsMigration\Models\Entity\ElasticsearchMigrationStep { |
48
|
9 |
|
$entity = $this->find($migrationStepId); |
49
|
9 |
|
if (!$entity) { |
50
|
1 |
|
throw new MigrationsNotExist(); |
51
|
|
|
} |
52
|
|
|
|
53
|
8 |
|
if ((new MigrationStatus())->isMigrationStatusValid($status)) { |
54
|
8 |
|
$entity->status = $status; |
55
|
8 |
|
$entity->error = $error; |
56
|
|
|
} |
57
|
|
|
|
58
|
8 |
|
if (is_int($priority)) { |
59
|
1 |
|
$entity->priority = $priority; |
60
|
|
|
} |
61
|
|
|
|
62
|
8 |
|
if (is_bool($stopOnFailure)) { |
63
|
|
|
$entity->stop_on_failure = $stopOnFailure; |
64
|
|
|
} |
65
|
|
|
|
66
|
8 |
|
$entity->saveOrFail(); |
67
|
|
|
|
68
|
8 |
|
$this->dispatchStatus($entity); |
69
|
|
|
|
70
|
8 |
|
return $entity; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
16 |
|
public function find(int $migrationStepId): ?\Triadev\EsMigration\Models\Entity\ElasticsearchMigrationStep |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
16 |
|
return \Triadev\EsMigration\Models\Entity\ElasticsearchMigrationStep::where('id', $migrationStepId) |
80
|
16 |
|
->firstOrFail(); |
81
|
7 |
|
} catch (ModelNotFoundException $e) { |
82
|
7 |
|
return null; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
2 |
|
public function delete(int $migrationStepId) |
90
|
|
|
{ |
91
|
2 |
|
if ($migrationStep = $this->find($migrationStepId)) { |
92
|
2 |
|
$migrationStep->delete(); |
93
|
|
|
} |
94
|
2 |
|
} |
95
|
|
|
|
96
|
8 |
|
private function dispatchStatus(\Triadev\EsMigration\Models\Entity\ElasticsearchMigrationStep $migration) |
97
|
|
|
{ |
98
|
8 |
|
switch ($migration->status) { |
99
|
8 |
|
case MigrationStatus::MIGRATION_STATUS_RUNNING: |
100
|
7 |
|
$event = new MigrationStepRunning($migration); |
101
|
7 |
|
break; |
102
|
8 |
|
case MigrationStatus::MIGRATION_STATUS_DONE: |
103
|
6 |
|
$event = new MigrationStepDone($migration); |
104
|
6 |
|
break; |
105
|
4 |
|
case MigrationStatus::MIGRATION_STATUS_ERROR: |
106
|
4 |
|
$event = new MigrationStepError($migration); |
107
|
4 |
|
break; |
108
|
|
|
default: |
109
|
1 |
|
$event = null; |
110
|
1 |
|
break; |
111
|
|
|
} |
112
|
|
|
|
113
|
8 |
|
if ($event) { |
114
|
8 |
|
event($event); |
115
|
|
|
} |
116
|
8 |
|
} |
117
|
|
|
} |
118
|
|
|
|