ImportTransactionManager   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 48
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A manageTransaction() 0 23 5
A __construct() 0 4 1
A configure() 0 5 1
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\Config\ConfigInterface;
28
use byrokrat\giroapp\DependencyInjection;
29
use byrokrat\giroapp\Event\Listener\ErrorListener;
30
use byrokrat\giroapp\Event;
31
use Symfony\Component\Console\Command\Command;
32
use Symfony\Component\Console\Input\InputInterface;
33
use Symfony\Component\Console\Input\InputOption;
34
35
final class ImportTransactionManager
36
{
37
    use DependencyInjection\CommandBusProperty;
38
    use DependencyInjection\DispatcherProperty;
39
40
    /** @var ConfigInterface */
41
    private $alwaysForceImportsConfig;
42
43
    /** @var ErrorListener */
44
    private $errorListener;
45
46
    public function __construct(ConfigInterface $alwaysForceImportsConfig, ErrorListener $errorListener)
47
    {
48
        $this->alwaysForceImportsConfig = $alwaysForceImportsConfig;
49
        $this->errorListener = $errorListener;
50
    }
51
52
    public function configure(Command $command): void
53
    {
54
        $command
55
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force import')
56
            ->addOption('not-force', '', InputOption::VALUE_NONE, 'Do not force import, overrides')
57
        ;
58
    }
59
60
    public function manageTransaction(InputInterface $input): void
61
    {
62
        if (!$this->errorListener->getErrors()) {
63
            return;
64
        }
65
66
        if (!$input->getOption('not-force')) {
67
            if (!!$this->alwaysForceImportsConfig->getValue()) {
68
                return;
69
            }
70
71
            if (!!$input->getOption('force')) {
72
                return;
73
            }
74
        }
75
76
        $this->dispatcher->dispatch(
77
            new Event\ErrorEvent(
78
                'Import failed as there were errors. Changes will be ignored. Use --force to override.'
79
            )
80
        );
81
82
        $this->commandBus->handle(new CommandBus\Rollback());
83
    }
84
}
85