Completed
Push — master ( fef7a6...aff413 )
by Arne
02:38
created

InfoCommand::executeConfigured()   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 Archivr\Vault;
10
use Symfony\Component\Console\Helper\Table;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class InfoCommand extends AbstractConfiguredCommand
15
{
16
    protected function configure()
17
    {
18
        parent::configure();
19
20
        $this->setName('info');
21
        $this->setDescription('Displays information about a vault and its local representation.');
22
    }
23
24
    protected function executeConfigured(InputInterface $input, OutputInterface $output, Configuration $configuration): int
25
    {
26
        $archivr = new ArchivR($configuration);
27
28
        $output->writeln(Application::LOGO);
29
30
        $this->displayGeneralInfo($archivr, $output);
31
        $this->displaySynchronizationHistory($archivr, $output);
32
        $this->displayOutstandingOperations($archivr, $output);
33
34
        return 0;
35
    }
36
37
    protected function displayGeneralInfo(ArchivR $archivR, OutputInterface $output)
38
    {
39
        $config = $archivR->getConfiguration();
40
41
        $table = new Table($output);
42
        $table->setStyle('compact');
43
        $table->addRow(['Base path:', sprintf('<info>%s</info>', $config->getLocalPath())]);
44
        $table->addRow(['Excluded:', implode(',', $config->getExclusions()) ?: '-']);
45
        $table->addRow(['Identity:', sprintf('<bold>%s</bold>', $config->getIdentity())]);
46
47
        foreach ($config->getVaultConfigurations() as $index => $vaultConfiguration)
48
        {
49
            $vault = $archivR->getVault($vaultConfiguration->getTitle());
50
            $currentLock = $vault->getLockAdapter()->getLock(Vault::LOCK_SYNC);
51
52
            $table->addRow([
53
                "Vault #{$index}",
54
                "Title:\nAdapter:\nLock Adapter:\nSettings:\nCurrent lock:",
55
                implode("\n", [
56
                    "<info>{$vault->getVaultConfiguration()->getTitle()}</info>",
57
                    $vaultConfiguration->getStorageDriver(),
58
                    $vaultConfiguration->getLockAdapter(),
59
                    implode(
60
                        ',',
61
                        array_map(
62
                            function($key, $value) { return "{$key}: {$value}"; },
63
                            array_keys($vaultConfiguration->getSettings()),
64
                            array_values($vaultConfiguration->getSettings())
65
                        )
66
                    ) ?: '-',
67
                    $currentLock ? "Locked {$currentLock->getAcquired()->format('c')} by {$currentLock->getIdentity()}" : '-'
68
                ])
69
            ]);
70
        }
71
72
        $table->render();
73
    }
74
75
    protected function displaySynchronizationHistory(ArchivR $archivR, OutputInterface $output)
76
    {
77
        $output->writeln('');
78
79
        $history = array_reverse($archivR->buildSynchronizationHistory(), true);
80
81
        if (count($history))
82
        {
83
            $output->writeln('Last synchronizations (recent first):');
84
85
            $table = new Table($output);
86
            $table->setHeaders(['Revision', 'Time (Start)', 'Identity', 'Vault(s)']);
87
88
            foreach ($history as $revision => $synchronizations)
89
            {
90
                $time = \DateTime::createFromFormat('U', 0);
91
                $identity = null;
92
93
                foreach ($synchronizations as $vaultTitle => $synchronization)
94
                {
95
                    /** @var Synchronization $synchronization */
96
97
                    $identity = $synchronization->getIdentity();
98
                    $time = max($time, $synchronization->getTime());
99
                }
100
101
                $table->addRow([
102
                    $revision,
103
                    $time->format('r'),
104
                    $identity,
105
                    implode(',', array_unique(array_keys($synchronizations)))
106
                ]);
107
            }
108
109
            $table->render();
110
        }
111
        else
112
        {
113
            $output->writeln('No synchronizations so far.');
114
        }
115
    }
116
117
    protected function displayOutstandingOperations(ArchivR $archivR, OutputInterface $output)
118
    {
119
        $output->writeln('');
120
        $output->write('Current state: ');
121
122
        $operationList = $archivR->buildOperationList();
123
124
        if ($count = count($operationList))
125
        {
126
            $output->writeln(sprintf('<bold>There are %d outstanding operations.</bold>', $count));
127
        }
128
        else
129
        {
130
            $output->writeln('<info>Everything is up to date!</info>');
131
        }
132
    }
133
}
134