1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of byrokrat\giroapp. |
5
|
|
|
* |
6
|
|
|
* byrokrat\giroapp is free software: you can redistribute it and/or |
7
|
|
|
* modify it under the terms of the GNU General Public License as published |
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* byrokrat\giroapp is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
* |
19
|
|
|
* Copyright 2016-21 Hannes Forsgård |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace byrokrat\giroapp\Console; |
25
|
|
|
|
26
|
|
|
use byrokrat\giroapp\CommandBus; |
27
|
|
|
use byrokrat\giroapp\DependencyInjection; |
28
|
|
|
use Symfony\Component\Console\Command\Command; |
29
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
30
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
31
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
32
|
|
|
|
33
|
|
|
final class ImportConsole implements ConsoleInterface |
34
|
|
|
{ |
35
|
|
|
use DependencyInjection\CommandBusProperty; |
36
|
|
|
use Helper\DryRun; |
37
|
|
|
|
38
|
|
|
/** @var ImportTransactionManager */ |
39
|
|
|
private $importTransactionManager; |
40
|
|
|
|
41
|
|
|
/** @var Helper\FileOrStdinInputLocator */ |
42
|
|
|
private $inputLocator; |
43
|
|
|
|
44
|
|
|
public function __construct( |
45
|
|
|
ImportTransactionManager $importTransactionManager, |
46
|
|
|
Helper\FileOrStdinInputLocator $inputLocator |
47
|
|
|
) { |
48
|
|
|
$this->importTransactionManager = $importTransactionManager; |
49
|
|
|
$this->inputLocator = $inputLocator; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function configure(Command $command): void |
53
|
|
|
{ |
54
|
|
|
$command |
55
|
|
|
->setName('import') |
56
|
|
|
->setDescription('Import a file from autogirot') |
57
|
|
|
->setHelp('Import one or more files with data from autogirot') |
58
|
|
|
->addArgument( |
59
|
|
|
self::OPTION_PATH, |
60
|
|
|
InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
61
|
|
|
self::OPTION_DESCS[self::OPTION_PATH] |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$this->importTransactionManager->configure($command); |
65
|
|
|
|
66
|
|
|
$this->configureDryRun($command); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function execute(InputInterface $input, OutputInterface $output): void |
70
|
|
|
{ |
71
|
|
|
foreach ($this->inputLocator->getFiles((array)$input->getArgument(self::OPTION_PATH)) as $file) { |
72
|
|
|
$this->commandBus->handle(new CommandBus\ImportAutogiroFile($file)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Rollback on errors |
76
|
|
|
$this->importTransactionManager->manageTransaction($input); |
77
|
|
|
|
78
|
|
|
// Rollback on dry run |
79
|
|
|
$this->evaluateDryRun($input); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|