SitesListAttributeCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 24
c 3
b 0
f 0
dl 0
loc 45
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A getSites() 0 16 3
A execute() 0 14 2
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 SitesListAttributeCommand extends AbstractCommand
13
{
14
    protected function configure(): void
15
    {
16
        parent::configure();
17
18
        $this
19
            ->setName('sites:listattribute')
20
            ->addOption('attribute', null, InputOption::VALUE_REQUIRED, 'json key to inspect')
21
            ->setDescription('List a config value from all sites')
22
        ;
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output): int
26
    {
27
        $attribute = $input->getOption('attribute');
28
29
        $sites = $this->getSites();
30
31
        $table = new Table($output);
32
        $table->setHeaders(['Site ID', 'Domain', 'Attribute']);
33
        foreach ($sites as $site) {
34
            $table->addRow([$site['site_id'], $site['domain'], $site[$attribute]]);
35
        }
36
        $table->render();
37
38
        return 0;
39
    }
40
41
    protected function getSites(): array
42
    {
43
        $api = $this->client->sites();
44
        $sites = [];
45
        $page = 0;
46
47
        while (true) {
48
            $resp = $api->list(50, $page);
49
            if (empty($resp['sites'])) {
50
                break;
51
            }
52
            $sites = array_merge($sites, $resp['sites']);
53
            ++$page;
54
        }
55
56
        return $sites;
57
    }
58
}
59