DeleteAttributeConsole::execute()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
nc 12
nop 2
dl 0
loc 32
rs 9.4222
c 1
b 0
f 0
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\DependencyInjection\CommandBusProperty;
27
use byrokrat\giroapp\CommandBus;
28
use Symfony\Component\Console\Command\Command;
29
use Symfony\Component\Console\Input\InputOption;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Output\OutputInterface;
32
use Symfony\Component\Console\Helper\QuestionHelper;
33
use Symfony\Component\Console\Question\ConfirmationQuestion;
34
35
final class DeleteAttributeConsole implements ConsoleInterface
36
{
37
    use CommandBusProperty;
38
    use Helper\DonorArgument;
39
    use Helper\DryRun;
40
41
    public function configure(Command $command): void
42
    {
43
        $this->configureDonorArgument($command);
44
        $command->setName('delete-attribute');
45
        $command->setDescription('Delete attribute(s) from donor');
46
        $command->setHelp('Delete one or more attributes from an existing donor.');
47
48
        $command->addOption(
49
            'attr-key',
50
            null,
51
            InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
52
            'Attribute key'
53
        );
54
55
        $this->configureDryRun($command);
56
    }
57
58
    public function execute(InputInterface $input, OutputInterface $output): void
59
    {
60
        $commandQueue = [];
61
62
        $donor = $this->readDonor($input);
63
64
        /** @var array<string> */
65
        $attrKeys = $input->getOption('attr-key');
66
67
        foreach ($attrKeys as $attrKey) {
68
            $commandQueue[] = new CommandBus\RemoveAttribute($donor, $attrKey);
69
        }
70
71
        $questionHelper = new QuestionHelper();
72
73
        foreach ($donor->getAttributes() as $attrKey => $attrValue) {
74
            $requestRemove = $questionHelper->ask(
75
                $input,
76
                $output,
77
                new ConfirmationQuestion("Remove attribute <comment>$attrKey</comment> [<info>y/N</info>]? ", false)
78
            );
79
80
            if ($requestRemove) {
81
                $commandQueue[] = new CommandBus\RemoveAttribute($donor, $attrKey);
82
            }
83
        }
84
85
        foreach ($commandQueue as $command) {
86
            $this->commandBus->handle($command);
87
        }
88
89
        $this->evaluateDryRun($input);
90
    }
91
}
92