|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpCfdi\SatCatalogosPopulate\Commands; |
|
6
|
|
|
|
|
7
|
|
|
use PhpCfdi\SatCatalogosPopulate\Database\Repository; |
|
8
|
|
|
use PhpCfdi\SatCatalogosPopulate\Importers\SourcesImporter; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use RuntimeException; |
|
11
|
|
|
|
|
12
|
|
|
final class UpdateDatabase implements CommandInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private string $sourceFolder; |
|
15
|
|
|
|
|
16
|
|
|
private string $destinationDatabase; |
|
17
|
|
|
|
|
18
|
|
|
private LoggerInterface $logger; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(string $sourceCatalog, string $destinationDatabase, LoggerInterface $logger) |
|
21
|
|
|
{ |
|
22
|
|
|
// TODO: check if we can move these setters to this constructors |
|
23
|
|
|
$this->setSourceCatalog($sourceCatalog); |
|
24
|
|
|
$this->setDestinationDatabase($destinationDatabase); |
|
25
|
|
|
$this->setLogger($logger); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
private function setSourceCatalog(string $sourceCatalog): void |
|
29
|
|
|
{ |
|
30
|
|
|
if ('' === $sourceCatalog) { |
|
31
|
|
|
throw new RuntimeException('Invalid source catalog: empty string received'); |
|
32
|
|
|
} |
|
33
|
|
|
if (! is_dir($sourceCatalog)) { |
|
34
|
|
|
throw new RuntimeException('Invalid source catalog: is not a directory'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->sourceFolder = $sourceCatalog; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function setDestinationDatabase(string $destinationDatabase): void |
|
41
|
|
|
{ |
|
42
|
|
|
// TODO: why is needed an absolute path ? |
|
43
|
|
|
if (! str_starts_with($destinationDatabase, DIRECTORY_SEPARATOR)) { |
|
44
|
|
|
$destinationDatabase = getcwd() . DIRECTORY_SEPARATOR . $destinationDatabase; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->destinationDatabase = $destinationDatabase; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getSourceFolder(): string |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->sourceFolder; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getDestinationDatabase(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->destinationDatabase; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function run(): int |
|
61
|
|
|
{ |
|
62
|
|
|
$repository = $this->createRepository(); |
|
63
|
|
|
$importer = $this->createImporter(); |
|
64
|
|
|
$repository->pdo()->beginTransaction(); |
|
65
|
|
|
$importer->import($this->getSourceFolder(), $repository, $this->logger); |
|
66
|
|
|
$repository->pdo()->commit(); |
|
67
|
|
|
$this->logger->info('Se terminó correctamente con la actualización de la base de datos'); |
|
68
|
|
|
return 0; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function createRepository(): Repository |
|
72
|
|
|
{ |
|
73
|
|
|
// TODO: validate destination |
|
74
|
|
|
return new Repository($this->getDestinationDatabase()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function createImporter(): SourcesImporter |
|
78
|
|
|
{ |
|
79
|
|
|
return new SourcesImporter(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public static function help(string $commandName): string |
|
83
|
|
|
{ |
|
84
|
|
|
return implode(PHP_EOL, [ |
|
85
|
|
|
"Sintax: $commandName folder database", |
|
86
|
|
|
' folder: location where all source catalogs exists', |
|
87
|
|
|
' database: database location (if not found it will be created)', |
|
88
|
|
|
]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public static function createFromArguments(array $arguments): self |
|
92
|
|
|
{ |
|
93
|
|
|
return new self($arguments[0] ?? '', $arguments[1] ?? '', new TerminalLogger()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function setLogger(LoggerInterface $logger): void |
|
97
|
|
|
{ |
|
98
|
|
|
$this->logger = $logger; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|