Passed
Push — master ( 5ba948...3b9ce6 )
by Hannes
14:31
created

DeleteAttributeConsole::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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