Passed
Pull Request — master (#2)
by
unknown
01:45
created

SubAccountListCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A initialize() 0 3 1
A execute() 0 29 5
1
<?php
2
3
namespace Incapsula\Command;
4
5
use Symfony\Component\Console\Helper\Table;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class SubAccountListCommand extends AbstractCommand
11
{
12
    /**
13
     * @var string
14
     */
15
    private $accountName;
0 ignored issues
show
introduced by
The private property $accountName is not used, and could be removed.
Loading history...
16
17
    protected function configure()
18
    {
19
        parent::configure();
20
21
        $this
22
            ->setName('subaccount:list')
23
            ->addOption('json', null, InputOption::VALUE_NONE, 'Output as JSON')
24
            ->setDescription('List all sites')
25
        ;
26
    }
27
28
    /**
29
     * @param InputInterface  $input
30
     * @param OutputInterface $output
31
     */
32
    protected function initialize(InputInterface $input, OutputInterface $output)
33
    {
34
        parent::initialize($input, $output);
35
    }
36
37
    /**
38
     * @param InputInterface  $input
39
     * @param OutputInterface $output
40
     *
41
     * @return int
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $api = $this->client->Accounts();
46
        $subAcc = [];
47
        $page = 0;
48
49
        while (true) {
50
            $resp = $api->list(50, $page);
51
            if (empty($resp['resultList'])) {
52
                break;
53
            }
54
            $subAcc = array_merge($subAcc, $resp['resultList']);
55
            ++$page;
56
        }
57
58
        if (true === $input->getOption('json')) {
59
            $output->write(json_encode($subAcc));
60
61
            return 0;
62
        }
63
64
        $table = new Table($output);
65
        $table->setHeaders(['Name', 'AccountID']);
66
        foreach ($subAcc as $acc) {
67
            $table->addRow([$acc['sub_account_name'], $acc['sub_account_id']]);
68
        }
69
        $table->render();
70
71
        return 0;
72
    }
73
}
74