SetAllCacheModeCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 30
c 2
b 0
f 0
dl 0
loc 57
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 25 3
A setCacheMode() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Incapsula\Command;
6
7
use function json_encode;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class SetAllCacheModeCommand extends SitesListCommand
14
{
15
    protected function configure(): void
16
    {
17
        parent::configure();
18
19
        $this
20
            ->setName('sites:setcachemode')
21
            ->addOption('json', null, InputOption::VALUE_NONE, 'Output as JSON')
22
            ->setDescription('Set all sites to static caching header mode')
23
        ;
24
    }
25
26
    protected function execute(InputInterface $input, OutputInterface $output): int
27
    {
28
        $changes = $this->setCacheMode();
29
30
        if (true === $input->getOption('json')) {
31
            $output->write(json_encode($changes));
32
33
            return 0;
34
        }
35
36
        $output->writeln('Sites that were changed in this run');
37
        $table = new Table($output);
38
        $table->setHeaders(['Site ID', 'Site Name', 'Return Message']);
39
40
        foreach ($changes as $changed) {
41
            $table->addRow([
42
                $changed['site_id'],
43
                $changed['domain'],
44
                $changed['return'],
45
            ]);
46
        }
47
48
        $table->render();
49
50
        return 0;
51
    }
52
53
    private function setCacheMode(): array
54
    {
55
        $sites = $this->getSites();
56
        $changed = [];
57
        foreach ($sites as $site) {
58
            $returnVal = $this->client->sites()->setStaticCacheMode($site['site_id']);
59
            $mode = $site['acceleration_level'];
60
            if ('advanced' === $mode) {
61
                array_push($changed, [
62
                    'site_id' => $site['site_id'],
63
                    'domain' => $site['domain'],
64
                    'return' => $returnVal['res_message'],
65
                ]);
66
            }
67
        }
68
69
        return $changed;
70
    }
71
}
72