Passed
Pull Request — master (#6)
by Matt
03:20
created

SitesListCommand::getSites()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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