Completed
Push — master ( cb7793...7b1e0a )
by Arne
01:36
created

InfoCommand::executePrepared()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
namespace Archivr\Cli\Command;
4
5
use Archivr\ArchivR;
6
use Archivr\Cli\Application;
7
use Archivr\Configuration;
8
use Archivr\Synchronization;
9
use Symfony\Component\Console\Helper\Table;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class InfoCommand extends AbstractCommand
15
{
16
    protected function configure()
17
    {
18
        $this->setName('info');
19
        $this->setDescription('Displays information about a vault and its local representation.');
20
        $this->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Configuration file to use. Defaults to "archivr.json".');
21
    }
22
23
    protected function executePrepared(InputInterface $input, OutputInterface $output, Configuration $configuration): int
24
    {
25
        $archivr = new ArchivR($configuration);
26
27
        $output->writeln(Application::LOGO);
28
29
        $this->displayGeneralInfo($archivr, $output);
30
        $this->displaySynchronizationHistory($archivr, $output);
31
        $this->displayOutstandingOperations($archivr, $output);
32
33
        return 0;
34
    }
35
36
    protected function displayGeneralInfo(ArchivR $archivR, OutputInterface $output)
37
    {
38
        $config = $archivR->getConfiguration();
39
40
        $table = new Table($output);
41
        $table->setStyle('compact');
42
        $table->addRow(['Base path:', sprintf('<info>%s</info>', $config->getLocalPath())]);
43
        $table->addRow(['Excluded:', implode(',', $config->getExclusions()) ?: '-']);
44
        $table->addRow(['Identity:', sprintf('<bold>%s</bold>', $config->getIdentity())]);
45
46
        foreach ($config->getConnectionConfigurations() as $index => $connectionConfiguration)
47
        {
48
            $table->addRow([
49
                sprintf('Vault <bold>#%d</bold>', $index),
50
                sprintf(
51
                    "Title: <bold>%s</bold>\nAdapter: <bold>%s</bold>\nLocking: <bold>%s</bold>\nSettings: <bold>%s</bold>",
52
                    $connectionConfiguration->getTitle(),
53
                    $connectionConfiguration->getVaultAdapter(),
54
                    $connectionConfiguration->getLockAdapter(),
55
                    implode(
56
                        ',',
57
                        array_map(
58
                            function($key, $value) { return "{$key}: {$value}"; },
59
                            array_keys($connectionConfiguration->getSettings()),
60
                            array_values($connectionConfiguration->getSettings())
61
                        )
62
                    )
63
                )
64
            ]);
65
        }
66
67
        $table->render();
68
    }
69
70
    protected function displaySynchronizationHistory(ArchivR $archivR, OutputInterface $output)
71
    {
72
        $output->writeln('');
73
        $output->writeln('Last synchronizations (recent first):');
74
75
        $history = array_reverse($archivR->buildSynchronizationHistory(), true);
76
77
        $table = new Table($output);
78
        $table->setHeaders(['Revision', 'Time (Start)', 'Identity', 'Vault(s)']);
79
80
        foreach ($history as $revision => $synchronizations)
81
        {
82
            $time = \DateTime::createFromFormat('U', 0);
83
            $identity = null;
84
85
            foreach ($synchronizations as $vaultTitle => $synchronization)
86
            {
87
                /** @var Synchronization $synchronization */
88
89
                $identity = $synchronization->getIdentity();
90
                $time = max($time, $synchronization->getTime());
91
            }
92
93
            $table->addRow([
94
                $revision,
95
                $time->format('r'),
96
                $identity,
97
                implode(',', array_unique(array_keys($synchronizations)))
98
            ]);
99
        }
100
101
        $table->render();
102
    }
103
104
    protected function displayOutstandingOperations(ArchivR $archivR, OutputInterface $output)
105
    {
106
        $output->writeln('');
107
108
        $operationCollection = $archivR->buildOperationCollection();
109
110
        if (count($operationCollection))
111
        {
112
            $output->writeln(sprintf('Current state: <bold>There are %d outstanding operations.</bold>', count($operationCollection)));
113
        }
114
        else
115
        {
116
            $output->writeln('Current state: <info>Everything is up to date!</info>');
117
        }
118
    }
119
}
120