SubAccountListCommand::execute()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 29
rs 9.3554
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Incapsula\Command;
6
7
use Symfony\Component\Console\Helper\Table;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class SubAccountListCommand extends AbstractCommand
13
{
14
    protected function configure(): void
15
    {
16
        parent::configure();
17
18
        $this
19
            ->setName('subaccount:list')
20
            ->addOption('json', null, InputOption::VALUE_NONE, 'Output as JSON')
21
            ->setDescription('List all sites')
22
        ;
23
    }
24
25
    protected function initialize(InputInterface $input, OutputInterface $output): void
26
    {
27
        parent::initialize($input, $output);
28
    }
29
30
    protected function execute(InputInterface $input, OutputInterface $output): int
31
    {
32
        $api = $this->client->accounts();
33
        $subAcc = [];
34
        $page = 0;
35
36
        while (true) {
37
            $resp = $api->list(50, $page);
38
            if (empty($resp['resultList'])) {
39
                break;
40
            }
41
            $subAcc = array_merge($subAcc, $resp['resultList']);
42
            ++$page;
43
        }
44
45
        if (true === $input->getOption('json')) {
46
            $output->write(json_encode($subAcc));
47
48
            return 0;
49
        }
50
51
        $table = new Table($output);
52
        $table->setHeaders(['Name', 'AccountID']);
53
        foreach ($subAcc as $acc) {
54
            $table->addRow([$acc['sub_account_name'], $acc['sub_account_id']]);
55
        }
56
        $table->render();
57
58
        return 0;
59
    }
60
}
61