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\Helper; |
25
|
|
|
|
26
|
|
|
use byrokrat\giroapp\DependencyInjection\DonorQueryProperty; |
27
|
|
|
use byrokrat\giroapp\Exception\DonorDoesNotExistException; |
28
|
|
|
use byrokrat\giroapp\Exception\RuntimeException; |
29
|
|
|
use byrokrat\giroapp\Domain\Donor; |
30
|
|
|
use byrokrat\giroapp\Validator\StringValidator; |
31
|
|
|
use Symfony\Component\Console\Command\Command; |
32
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
33
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
34
|
|
|
use Symfony\Component\Console\Input\InputOption; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Methods for fetching donors based on command line argument |
38
|
|
|
*/ |
39
|
|
|
trait DonorArgument |
40
|
|
|
{ |
41
|
|
|
use DonorQueryProperty; |
42
|
|
|
|
43
|
|
|
protected function configureDonorArgument(Command $command, bool $required = true): void |
44
|
|
|
{ |
45
|
|
|
$requiredFlag = $required ? InputArgument::REQUIRED : InputArgument::OPTIONAL; |
46
|
|
|
$command->addArgument('donor', $requiredFlag, 'Donor identified by mandate key, payer number or name'); |
47
|
|
|
$command->addOption('id-payer-number', null, InputOption::VALUE_NONE, 'Only use payer number as donor id'); |
48
|
|
|
$command->addOption('id-mandate-key', null, InputOption::VALUE_NONE, 'Only use mandate key as donor id'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function readDonor(InputInterface $input): Donor |
52
|
|
|
{ |
53
|
|
|
$taintedId = $input->getArgument('donor'); |
54
|
|
|
|
55
|
|
|
if (!is_string($taintedId)) { |
56
|
|
|
throw new \LogicException('Donor key must be string'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$donorId = (new StringValidator())->validate('donor', $taintedId); |
60
|
|
|
|
61
|
|
|
if ($input->getOption('id-payer-number') && $input->getOption('id-mandate-key')) { |
62
|
|
|
throw new RuntimeException("Illegal to use the 'id-payer-number' and 'id-mandate-key' flags toghether."); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($input->getOption('id-payer-number')) { |
66
|
|
|
return $this->donorQuery->requireByPayerNumber($donorId); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($input->getOption('id-mandate-key')) { |
70
|
|
|
return $this->donorQuery->requireByMandateKey($donorId); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($donor = $this->donorQuery->findByPayerNumber($donorId)) { |
74
|
|
|
return $donor; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($donor = $this->donorQuery->findByMandateKey($donorId)) { |
78
|
|
|
return $donor; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$regexp = '/' . preg_quote($donorId, '/') . '/i'; |
82
|
|
|
|
83
|
|
|
/** @var ?Donor */ |
84
|
|
|
$matchedDonor = null; |
85
|
|
|
|
86
|
|
|
foreach ($this->donorQuery->findAll() as $donor) { |
87
|
|
|
if (preg_match($regexp, $donor->getName())) { |
88
|
|
|
if ($matchedDonor) { |
89
|
|
|
throw new DonorDoesNotExistException("Unable to find donor '$donorId', more than one match."); |
90
|
|
|
} |
91
|
|
|
$matchedDonor = $donor; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($matchedDonor) { |
|
|
|
|
96
|
|
|
return $matchedDonor; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
throw new DonorDoesNotExistException("Unable to find donor '$donorId'"); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|