EditAmountConsole::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 14
rs 9.9
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;
27
use byrokrat\giroapp\CommandBus\UpdateDonationAmount;
28
use byrokrat\giroapp\Validator;
29
use Money\Currency;
30
use Money\Money;
31
use Symfony\Component\Console\Command\Command;
32
use Symfony\Component\Console\Input\InputOption;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Output\OutputInterface;
35
use Symfony\Component\Console\Helper\QuestionHelper;
36
37
final class EditAmountConsole implements ConsoleInterface
38
{
39
    use DependencyInjection\CommandBusProperty;
40
    use DependencyInjection\MoneyFormatterProperty;
41
    use DependencyInjection\MoneyParserProperty;
42
    use Helper\DonorArgument;
43
    use Helper\DryRun;
44
45
    public function configure(Command $command): void
46
    {
47
        $this->configureDonorArgument($command);
48
        $command->setName('edit-amount');
49
        $command->setDescription('Update donor donation amount');
50
        $command->setHelp('Update monthly donation amount for a donor.');
51
        $command->addOption(
52
            self::OPTION_NEW_AMOUNT,
53
            null,
54
            InputOption::VALUE_REQUIRED,
55
            self::OPTION_DESCS[self::OPTION_NEW_AMOUNT]
56
        );
57
        $command->addOption('message', 'm', InputOption::VALUE_REQUIRED, 'Message describing amount change');
58
        $this->configureDryRun($command);
59
    }
60
61
    public function execute(InputInterface $input, OutputInterface $output): void
62
    {
63
        $donor = $this->readDonor($input);
64
65
        $inputReader = new Helper\InputReader($input, $output, new QuestionHelper());
66
67
        $amount = $this->moneyParser->parse(
68
            $inputReader->readInput(
69
                self::OPTION_NEW_AMOUNT,
70
                Helper\QuestionFactory::createQuestion(
71
                    self::OPTION_DESCS[self::OPTION_NEW_AMOUNT],
72
                    $this->moneyFormatter->format($donor->getDonationAmount())
73
                ),
74
                new Validator\ValidatorCollection(
75
                    new Validator\NotEmptyValidator(),
76
                    new Validator\NumericValidator()
77
                )
78
            ),
79
            new Currency('SEK')
80
        );
81
82
        /** @var string $msg */
83
        $msg = $input->getOption('message') ?: 'Amount edited by user';
84
85
        $this->commandBus->handle(new UpdateDonationAmount($donor, $amount, $msg));
86
87
        $this->evaluateDryRun($input);
88
    }
89
}
90