SitesListCommand::getSites()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 16
rs 9.9332
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 SitesListCommand extends AbstractCommand
13
{
14
    protected function configure(): void
15
    {
16
        parent::configure();
17
18
        $this
19
            ->setName('sites:list')
20
            ->addOption('json', null, InputOption::VALUE_NONE, 'Output as JSON')
21
            ->setDescription('List all sites')
22
        ;
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output): int
26
    {
27
        $sites = $this->getSites();
28
29
        if (true === $input->getOption('json')) {
30
            $output->write(json_encode($sites));
31
32
            return 0;
33
        }
34
35
        $table = new Table($output);
36
        $table->setHeaders(['Site ID', 'Status', 'Domain']);
37
        foreach ($sites as $site) {
38
            $table->addRow([$site['site_id'], $site['status'], $site['domain']]);
39
        }
40
        $table->render();
41
42
        return 0;
43
    }
44
45
    protected function getSites(): array
46
    {
47
        $api = $this->client->sites();
48
        $sites = [];
49
        $page = 0;
50
51
        while (true) {
52
            $resp = $api->list(50, $page);
53
            if (empty($resp['sites'])) {
54
                break;
55
            }
56
            $sites = array_merge($sites, $resp['sites']);
57
            ++$page;
58
        }
59
60
        return $sites;
61
    }
62
}
63