|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpCfdi\RfcLinc\Application\Cli\Commands; |
|
6
|
|
|
|
|
7
|
|
|
use PhpCfdi\RfcLinc\Application\Cli\OutputLogger; |
|
8
|
|
|
use PhpCfdi\RfcLinc\Application\Config; |
|
9
|
|
|
use PhpCfdi\RfcLinc\DataGateway\FactoryInterface; |
|
10
|
|
|
use PhpCfdi\RfcLinc\Domain\Catalog; |
|
11
|
|
|
use PhpCfdi\RfcLinc\Domain\VersionDate; |
|
12
|
|
|
use Pimple\Container; |
|
13
|
|
|
use Symfony\Component\Console\Command\Command; |
|
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
17
|
|
|
|
|
18
|
|
|
class CatalogCommand extends Command |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var Container */ |
|
21
|
|
|
private $container; |
|
22
|
|
|
|
|
23
|
5 |
|
public function __construct(Container $container) |
|
24
|
|
|
{ |
|
25
|
5 |
|
parent::__construct('catalog'); |
|
26
|
5 |
|
$this->container = $container; |
|
27
|
5 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function gateways(): FactoryInterface |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->container['gateways']; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
public function config(): Config |
|
35
|
|
|
{ |
|
36
|
2 |
|
return $this->container['config']; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
5 |
|
protected function configure() |
|
40
|
|
|
{ |
|
41
|
5 |
|
$this->setDescription('Obtain catalog information'); |
|
42
|
5 |
|
$this->addArgument('catalog', InputArgument::REQUIRED, 'Catalog (as date) to request, can use "latest"'); |
|
43
|
5 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getLatestCatalog(): Catalog |
|
46
|
|
|
{ |
|
47
|
|
|
$latest = $this->gateways()->catalog()->latest(); |
|
48
|
|
|
if ($latest instanceof Catalog) { |
|
49
|
|
|
return $latest; |
|
50
|
|
|
} |
|
51
|
|
|
throw new \RuntimeException('There are no latest catalog in the database'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function getCatalogByDateString(string $dateString): Catalog |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->gateways()->catalog()->get( |
|
57
|
|
|
VersionDate::createFromString($dateString) |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
62
|
|
|
{ |
|
63
|
|
|
// input arguments |
|
64
|
2 |
|
$argumentCatalog = (string) $input->getArgument('catalog'); |
|
65
|
|
|
|
|
66
|
|
|
// create logger |
|
67
|
2 |
|
$logger = new OutputLogger($output); |
|
68
|
|
|
|
|
69
|
|
|
// report working info |
|
70
|
2 |
|
$logger->info('Required catalog: ' . $argumentCatalog); |
|
71
|
|
|
|
|
72
|
|
|
// show debug messages of database config |
|
73
|
2 |
|
$config = $this->config(); |
|
74
|
2 |
|
$logger->debug(sprintf('DB: [%s], Username: [%s]', $config->dbDsn(), $config->dbUsername())); |
|
75
|
|
|
|
|
76
|
|
|
// get data |
|
77
|
2 |
|
if ('latest' === $argumentCatalog) { |
|
78
|
1 |
|
$catalog = $this->getLatestCatalog(); |
|
79
|
|
|
} else { |
|
80
|
1 |
|
$catalog = $this->getCatalogByDateString($argumentCatalog); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// print data |
|
84
|
2 |
|
$logger->notice(sprintf( |
|
85
|
2 |
|
'Catalog: %s, Active: %s, Inserted: %s, Updated: %s, Deleted: %s', |
|
86
|
2 |
|
$catalog->date()->format(), |
|
87
|
2 |
|
number_format($catalog->records(), 0), |
|
88
|
2 |
|
number_format($catalog->inserted(), 0), |
|
89
|
2 |
|
number_format($catalog->updated(), 0), |
|
90
|
2 |
|
number_format($catalog->deleted(), 0) |
|
91
|
|
|
)); |
|
92
|
|
|
|
|
93
|
2 |
|
return 0; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|