1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Speicher210\FastbillBundle\Command; |
4
|
|
|
|
5
|
|
|
use Speicher210\Fastbill\Api\Service\Customer\Get\RequestData; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Command to reset the Fastbill account. |
14
|
|
|
*/ |
15
|
|
|
class ResetAccountCommand extends ContainerAwareCommand |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The input. |
19
|
|
|
* |
20
|
|
|
* @var InputInterface |
21
|
|
|
*/ |
22
|
|
|
protected $input; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The output. |
26
|
|
|
* |
27
|
|
|
* @var OutputInterface |
28
|
|
|
*/ |
29
|
|
|
protected $output; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
protected function configure() |
35
|
|
|
{ |
36
|
|
|
$this |
37
|
|
|
->setName('sp210:fastbill:reset') |
38
|
|
|
->setDescription('Reset (delete) the data from the Fastbill account') |
39
|
|
|
->addOption( |
40
|
|
|
'reset-customers', |
41
|
|
|
null, |
42
|
|
|
InputOption::VALUE_NONE, |
43
|
|
|
'Flag if the customers should be reset' |
44
|
|
|
) |
45
|
|
|
->addOption( |
46
|
|
|
'id', |
47
|
|
|
null, |
48
|
|
|
InputOption::VALUE_REQUIRED, |
49
|
|
|
'Customer ID to reset' |
50
|
|
|
) |
51
|
|
|
->addOption( |
52
|
|
|
'ext-id', |
53
|
|
|
null, |
54
|
|
|
InputOption::VALUE_REQUIRED, |
55
|
|
|
'Optional external ID' |
56
|
|
|
) |
57
|
|
|
->addOption( |
58
|
|
|
'skip-id', |
59
|
|
|
null, |
60
|
|
|
InputOption::VALUE_REQUIRED, |
61
|
|
|
'Customer ID to skip' |
62
|
|
|
) |
63
|
|
|
->addOption( |
64
|
|
|
'skip-ext-id', |
65
|
|
|
null, |
66
|
|
|
InputOption::VALUE_REQUIRED, |
67
|
|
|
'Customer external ID to skip' |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
75
|
|
|
{ |
76
|
|
|
$this->input = $input; |
77
|
|
|
$this->output = $output; |
78
|
|
|
|
79
|
|
|
$noInteraction = $input->getOption('no-interaction'); |
80
|
|
|
if ($noInteraction !== true) { |
81
|
|
|
$helper = $this->getHelper('question'); |
82
|
|
|
$question = new ConfirmationQuestion( |
83
|
|
|
'<question>Are you sure you want to reset the Fastbill data?</question> [N]: ', |
84
|
|
|
false |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
if (!$helper->ask($input, $output, $question)) { |
88
|
|
|
$output->writeln('Aborting.'); |
89
|
|
|
|
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($noInteraction === true || $input->getOption('reset-customers')) { |
95
|
|
|
$this->resetCustomers(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Reset all customers including subscriptions, but not invoices. |
101
|
|
|
*/ |
102
|
|
|
protected function resetCustomers() |
103
|
|
|
{ |
104
|
|
|
/** @var \Speicher210\Fastbill\Api\Service\Customer\CustomerService $customerService */ |
105
|
|
|
$customerService = $this->getContainer()->get('speicher210_fastbill.service.customer'); |
106
|
|
|
$requestData = new RequestData(); |
107
|
|
|
$requestData->setCustomerId($this->input->getOption('id')); |
108
|
|
|
$requestData->setCustomerExternalUid($this->input->getOption('ext-id')); |
109
|
|
|
$apiResponse = $customerService->getCustomers($requestData); |
110
|
|
|
$customers = $apiResponse->getResponse()->getCustomers(); |
111
|
|
|
|
112
|
|
|
$idToSkip = $this->input->getOption('skip-id'); |
113
|
|
|
$externalIdToSkip = $this->input->getOption('skip-ext-id'); |
114
|
|
|
|
115
|
|
|
if (count($customers) === 0) { |
116
|
|
|
$this->output->writeln('<info>No customers to reset.</info>'); |
117
|
|
|
|
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
foreach ($customers as $customer) { |
122
|
|
View Code Duplication |
if ($idToSkip && $customer->getCustomerId() == $idToSkip) { |
|
|
|
|
123
|
|
|
$infoMsg = sprintf( |
124
|
|
|
'<info>Skipping resetting customer %s (ext. id: %s)</info>', |
125
|
|
|
$customer->getCustomerId(), |
126
|
|
|
$customer->getCustomerExternalUid() |
127
|
|
|
); |
128
|
|
|
$this->output->writeln($infoMsg); |
129
|
|
|
|
130
|
|
|
continue; |
131
|
|
|
} |
132
|
|
View Code Duplication |
if ($externalIdToSkip && $customer->getCustomerExternalUid() == $externalIdToSkip) { |
|
|
|
|
133
|
|
|
$infoMsg = sprintf( |
134
|
|
|
'<info>Skipping resetting customer %s (ext. id: %s)</info>', |
135
|
|
|
$customer->getCustomerId(), |
136
|
|
|
$customer->getCustomerExternalUid() |
137
|
|
|
); |
138
|
|
|
$this->output->writeln($infoMsg); |
139
|
|
|
|
140
|
|
|
continue; |
141
|
|
|
} |
142
|
|
|
$infoMsg = sprintf( |
143
|
|
|
'<info>Deleting customer %s (ext. id: %s)</info>', |
144
|
|
|
$customer->getCustomerId(), |
145
|
|
|
$customer->getCustomerExternalUid() |
146
|
|
|
); |
147
|
|
|
$this->output->writeln($infoMsg); |
148
|
|
|
|
149
|
|
|
$customerService->deleteCustomer($customer->getCustomerId()); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.