SitesListCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 26
c 4
b 0
f 0
dl 0
loc 49
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 18 3
A configure() 0 8 1
A getSites() 0 16 3
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