Passed
Branch master (613786)
by Hannes
02:27
created

ImportTransactionManager   A

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