Completed
Push — master ( b2e536...a4e6dd )
by Arne
02:05
created

InfoCommand::displayOutstandingOperations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
3
namespace Storeman\Cli\Command;
4
5
use Storeman\Storeman;
6
use Storeman\Cli\Application;
7
use Storeman\Synchronization;
8
use Storeman\Vault;
9
use Storeman\VaultConfiguration;
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 AbstractCommand
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, Storeman $storeman): int
25
    {
26
        $output->writeln(Application::LOGO);
27
28
        $this->displayGeneralInfo($storeman, $output);
29
        $this->displaySynchronizationHistory($storeman, $output);
30
31
        return 0;
32
    }
33
34
    protected function displayGeneralInfo(Storeman $storeman, OutputInterface $output)
35
    {
36
        $config = $storeman->getConfiguration();
37
38
        $table = new Table($output);
39
        $table->setStyle('compact');
40
        $table->addRow(['Base path:', sprintf('<info>%s</info>', $config->getPath())]);
41
        $table->addRow(['Excluded:', implode(',', $config->getExclude()) ?: '-']);
42
        $table->addRow(['Identity:', $config->getIdentity()]);
43
44
        foreach (array_values($config->getVaults()) as $index => $vaultConfiguration)
45
        {
46
            /** @var VaultConfiguration $vaultConfiguration */
47
48
            $vault = $storeman->getVault($vaultConfiguration->getTitle());
49
            $currentLock = $vault->getLockAdapter()->getLock(Vault::LOCK_SYNC);
50
51
            $table->addRow([' ']); // blank line
52
            $table->addRow([
53
                "Vault #{$index}",
54
                "Title:\nAdapter:\nLayout:\nLock Adapter:\nSettings:\nCurrent lock:",
55
                implode("\n", [
56
                    "<info>{$vault->getVaultConfiguration()->getTitle()}</info>",
57
                    $vaultConfiguration->getAdapter(),
58
                    $vaultConfiguration->getVaultLayout(),
59
                    $vaultConfiguration->getLockAdapter(),
60
                    implode(
61
                        ',',
62
                        array_map(
63
                            function($key, $value) { return "{$key}: {$value}"; },
64
                            array_keys($vaultConfiguration->getSettings()),
65
                            array_values($vaultConfiguration->getSettings())
66
                        )
67
                    ) ?: '-',
68
                    $currentLock ? "Locked {$currentLock->getAcquired()->format('c')} by {$currentLock->getIdentity()}" : '-'
69
                ])
70
            ]);
71
        }
72
73
        $table->render();
74
    }
75
76
    protected function displaySynchronizationHistory(Storeman $storeman, OutputInterface $output)
77
    {
78
        $output->writeln('');
79
80
        $history = array_reverse($storeman->buildSynchronizationHistory(), true);
81
82
        if (count($history))
83
        {
84
            $output->writeln('Last synchronizations (recent first):');
85
86
            $table = new Table($output);
87
            $table->setHeaders(['Revision', 'Time (Start)', 'Identity', 'Vault(s)']);
88
89
            foreach ($history as $revision => $synchronizations)
90
            {
91
                $time = \DateTime::createFromFormat('U', 0);
92
                $identity = null;
93
94
                foreach ($synchronizations as $vaultTitle => $synchronization)
95
                {
96
                    /** @var Synchronization $synchronization */
97
98
                    $identity = $synchronization->getIdentity();
99
                    $time = max($time, $synchronization->getTime());
100
                }
101
102
                $table->addRow([
103
                    $revision,
104
                    $time->format('r'),
105
                    $identity,
106
                    implode(',', array_unique(array_keys($synchronizations)))
107
                ]);
108
            }
109
110
            $table->render();
111
        }
112
        else
113
        {
114
            $output->writeln('No synchronizations so far.');
115
        }
116
    }
117
}
118