1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Locastic\SymfonyTranslationBundle\TranslationMigration; |
6
|
|
|
|
7
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
8
|
|
|
use Exception; |
9
|
|
|
use Locastic\SymfonyTranslationBundle\Factory\TranslationMigrationFactoryInterface; |
10
|
|
|
use Locastic\SymfonyTranslationBundle\Model\Translation; |
11
|
|
|
use Locastic\SymfonyTranslationBundle\Model\TranslationMigrationInterface; |
12
|
|
|
use Locastic\SymfonyTranslationBundle\Saver\TranslationValueSaverInterface; |
13
|
|
|
|
14
|
|
|
final class Executor implements ExecutorInterface |
15
|
|
|
{ |
16
|
|
|
/** @var Translation[] */ |
17
|
|
|
private array $translations = []; |
18
|
|
|
|
19
|
|
|
private TranslationValueSaverInterface $translationValueSaver; |
20
|
|
|
|
21
|
|
|
private TranslationMigrationFactoryInterface $translationMigrationFactory; |
22
|
|
|
|
23
|
|
|
private ManagerRegistry $managerRegistry; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
TranslationValueSaverInterface $translationValueSaver, |
27
|
|
|
TranslationMigrationFactoryInterface $translationMigrationFactory, |
28
|
|
|
ManagerRegistry $managerRegistry |
29
|
|
|
) { |
30
|
|
|
$this->translationValueSaver = $translationValueSaver; |
31
|
|
|
$this->translationMigrationFactory = $translationMigrationFactory; |
32
|
|
|
$this->managerRegistry = $managerRegistry; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function addTranslation(Translation $translation): void |
36
|
|
|
{ |
37
|
|
|
$this->translations[] = $translation; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function clearTranslations(): void |
41
|
|
|
{ |
42
|
|
|
$this->translations = []; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function execute(AbstractTranslationMigration $migration, bool $resync = false): void |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
|
|
$this->executeMigration($migration, $resync); |
49
|
|
|
} catch (Exception $exception) { |
50
|
|
|
$this->skipMigration($migration); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function executeMigration(AbstractTranslationMigration $migration, bool $resync = false): void |
55
|
|
|
{ |
56
|
|
|
if ($this->hasAlreadyBeenPlayed($migration) && !$resync) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
$migration->up(); |
60
|
|
|
|
61
|
|
|
foreach ($this->translations as $translation) { |
62
|
|
|
foreach ($translation->getValues() as $translationValue) { |
63
|
|
|
$this->translationValueSaver->saveTranslationValue($translationValue, !$resync); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!$this->hasAlreadyBeenPlayed($migration)) { |
68
|
|
|
$this->markVersion($migration); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function skipMigration(AbstractTranslationMigration $migration): void |
73
|
|
|
{ |
74
|
|
|
$this->markVersion($migration); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function markVersion(AbstractTranslationMigration $migration): void |
78
|
|
|
{ |
79
|
|
|
$translationMigration = $this->translationMigrationFactory->createNew(); |
80
|
|
|
$translationMigration->setNumber($migration->getVersionNumber()); |
81
|
|
|
|
82
|
|
|
$this->managerRegistry->getManager()->persist($translationMigration); |
83
|
|
|
$this->managerRegistry->getManager()->flush(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function hasAlreadyBeenPlayed(AbstractTranslationMigration $migration): bool |
87
|
|
|
{ |
88
|
|
|
return $this->managerRegistry |
89
|
|
|
->getRepository(TranslationMigrationInterface::class) |
90
|
|
|
->findOneBy(['number' => $migration->getVersionNumber()]) instanceof TranslationMigrationInterface; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|