Passed
Push — master ( 3b9ce6...298e9b )
by Hannes
02:09
created

ImportConsole::execute()   B

Complexity

Conditions 9
Paths 24

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 24
nop 2
dl 0
loc 36
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of byrokrat\giroapp.
4
 *
5
 * byrokrat\giroapp is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\giroapp is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016-20 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\giroapp\Console;
24
25
use byrokrat\giroapp\CommandBus\ImportAutogiroFile;
26
use byrokrat\giroapp\CommandBus\Rollback;
27
use byrokrat\giroapp\DependencyInjection;
28
use byrokrat\giroapp\Filesystem\FilesystemInterface;
29
use byrokrat\giroapp\Filesystem\Sha256File;
30
use byrokrat\giroapp\Event\Listener\ErrorListener;
31
use byrokrat\giroapp\Event\LogEvent;
32
use Psr\Log\LogLevel;
33
use Symfony\Component\Console\Command\Command;
34
use Symfony\Component\Console\Input\InputArgument;
35
use Symfony\Component\Console\Input\InputInterface;
36
use Symfony\Component\Console\Input\InputOption;
37
use Symfony\Component\Console\Output\OutputInterface;
38
use Streamer\Stream;
39
40
final class ImportConsole implements ConsoleInterface
41
{
42
    use DependencyInjection\CommandBusProperty,
43
        DependencyInjection\DispatcherProperty;
44
45
    /** @var FilesystemInterface */
46
    private $filesystem;
47
48
    /** @var ErrorListener */
49
    private $errorListener;
50
51
    public function __construct(FilesystemInterface $filesystem, ErrorListener $errorListener)
52
    {
53
        $this->filesystem = $filesystem;
54
        $this->errorListener = $errorListener;
55
    }
56
57
    public function configure(Command $command): void
58
    {
59
        $command
60
            ->setName('import')
61
            ->setDescription('Import a file from autogirot')
62
            ->setHelp('Import one or more files with data from autogirot')
63
            ->addArgument(
64
                'path',
65
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
66
                'One or more paths to import'
67
            )
68
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force import')
69
        ;
70
    }
71
72
    public function execute(InputInterface $input, OutputInterface $output): void
73
    {
74
        $files = [];
75
76
        $paths = (array)$input->getArgument('path');
77
78
        if (empty($paths) && defined('STDIN')) {
79
            $files[] = new Sha256File('STDIN', (new Stream(STDIN))->getContent());
80
        }
81
82
        foreach ($paths as $path) {
83
            if ($this->filesystem->isFile($path)) {
84
                $files[] = $this->filesystem->readFile($path);
85
                continue;
86
            }
87
88
            foreach ($this->filesystem->readDir($path) as $file) {
89
                $files[] = $file;
90
            }
91
        }
92
93
        foreach ($files as $file) {
94
            $this->commandBus->handle(new ImportAutogiroFile($file));
95
        }
96
97
        // Rollback on errors
98
        if (!$input->getOption('force') && $this->errorListener->getErrors()) {
99
            $this->dispatcher->dispatch(
100
                new LogEvent(
101
                    'Import failed as there were errors. Changes will be ignored. Use --force to override.',
102
                    [],
103
                    LogLevel::ERROR
104
                )
105
            );
106
107
            $this->commandBus->handle(new Rollback);
108
        }
109
    }
110
}
111