Completed
Pull Request — master (#1)
by
unknown
15:02
created

ResetAccountCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 138
Duplicated Lines 14.49 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 13
c 2
b 0
f 2
lcom 1
cbo 10
dl 20
loc 138
ccs 0
cts 62
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 36 1
B execute() 0 24 5
C resetCustomers() 20 50 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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