Completed
Push — master ( 96f1c9...c70f55 )
by Dragos
12:29
created

ResetAccountCommand::resetCustomers()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 42
ccs 0
cts 8
cp 0
rs 8.439
cc 5
eloc 27
nc 4
nop 0
crap 30
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 | InputOption::VALUE_IS_ARRAY,
61
                'Customer ID to skip'
62
            )
63
            ->addOption(
64
                'skip-ext-id',
65
                null,
66
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
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
        $idsToSkip = $this->input->getOption('skip-id');
113
        $externalIdsToSkip = $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
            if (in_array($customer->getCustomerId(), $idsToSkip)
123
                || in_array($customer->getCustomerExternalUid(), $externalIdsToSkip)
124
            ) {
125
                $infoMsg = sprintf(
126
                    '<info>Skipping resetting customer %s (ext. id: %s)</info>',
127
                    $customer->getCustomerId(),
128
                    $customer->getCustomerExternalUid()
129
                );
130
                $this->output->writeln($infoMsg);
131
132
                continue;
133
            }
134
            $infoMsg = sprintf(
135
                '<info>Deleting customer %s (ext. id: %s)</info>',
136
                $customer->getCustomerId(),
137
                $customer->getCustomerExternalUid()
138
            );
139
            $this->output->writeln($infoMsg);
140
141
            $customerService->deleteCustomer($customer->getCustomerId());
142
        }
143
    }
144
}
145