Completed
Push — master ( 56028c...0c27d5 )
by Dragos
09:05
created

ResetAccountCommand::resetCustomers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
ccs 0
cts 18
cp 0
rs 9.2
cc 2
eloc 14
nc 2
nop 0
crap 6
1
<?php
2
3
namespace ApiBundle\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
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function execute(InputInterface $input, OutputInterface $output)
63
    {
64
        $this->input = $input;
65
        $this->output = $output;
66
67
        $noInteraction = $input->getOption('no-interaction');
68
        if ($noInteraction !== true) {
69
            $helper = $this->getHelper('question');
70
            $question = new ConfirmationQuestion(
71
                '<question>Are you sure you want to reset the Fastbill data?</question> [N]: ',
72
                false
73
            );
74
75
            if (!$helper->ask($input, $output, $question)) {
76
                $output->writeln('Aborting.');
77
78
                return;
79
            }
80
        }
81
82
        if ($noInteraction === true || $input->getOption('reset-customers')) {
83
            $this->resetCustomers();
84
        }
85
    }
86
87
    /**
88
     * Reset all customers including subscriptions, but not invoices.
89
     */
90
    protected function resetCustomers()
91
    {
92
        /** @var \Speicher210\Fastbill\Api\Service\Customer\CustomerService $customerService */
93
        $customerService = $this->getContainer()->get('speicher210_fastbill.service.customer');
94
        $requestData = new RequestData();
95
        $requestData->setCustomerId($this->input->getOption('id'));
96
        $requestData->setCustomerExternalUid($this->input->getOption('ext-id'));
97
        $apiResponse = $customerService->getCustomers($requestData);
98
        $customers = $apiResponse->getResponse()->getCustomers();
99
100
        foreach ($customers as $customer) {
101
            $infoMsg = sprintf(
102
                '<info>Deleting customer %s (ext. id: %s)</info>',
103
                $customer->getCustomerId(),
104
                $customer->getCustomerExternalUid()
105
            );
106
            $this->output->writeln($infoMsg);
107
108
            $customerService->deleteCustomer($customer->getCustomerId());
109
        }
110
111
    }
112
}
113