Completed
Push — master ( 65b5b4...2290e5 )
by Arne
05:25
created

InfoCommand::buildSynchronizationHistory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
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 Symfony\Component\Console\Helper\Table;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class InfoCommand extends AbstractCommand
14
{
15
    protected function configure()
16
    {
17
        parent::configure();
18
19
        $this->setName('info');
20
        $this->setDescription('Displays information about a vault and its local representation.');
21
    }
22
23
    protected function executeConfigured(InputInterface $input, OutputInterface $output, Storeman $storeman): int
24
    {
25
        $output->writeln(Application::LOGO);
26
27
        $this->displayGeneralInfo($storeman, $output);
28
        $this->displaySynchronizationHistory($storeman, $output);
29
30
        return 0;
31
    }
32
33
    protected function displayGeneralInfo(Storeman $storeman, OutputInterface $output)
34
    {
35
        $config = $storeman->getConfiguration();
36
37
        $table = new Table($output);
38
        $table->setStyle('compact');
39
        $table->addRow(['Base path:', sprintf('<info>%s</info>', $config->getPath())]);
40
        $table->addRow(['Excluded:', implode(',', $config->getExclude()) ?: '-']);
41
        $table->addRow(['Identity:', $config->getIdentity()]);
42
        $table->addRow(['Index builder:', $config->getIndexBuilder()]);
43
44
        foreach ($storeman->getVaultContainer() as $index => $vault)
45
        {
46
            /** @var Vault $vault */
47
48
            $vaultConfiguration = $vault->getVaultConfiguration();
49
50
            $currentLock = $vault->getLockAdapter()->getLock(Vault::LOCK_SYNC);
51
52
            $table->addRow([' ']); // blank line
53
            $table->addRow([
54
                "Vault #{$index}",
55
                implode("\n", [
56
                    'Title:',
57
                    'Storage adapter:',
58
                    'Vault layout:',
59
                    'Lock adapter:',
60
                    'Index merger:',
61
                    'Operation list builder:',
62
                    'Settings:',
63
                    'Current lock:',
64
                ]),
65
                implode("\n", [
66
                    "<info>{$vault->getVaultConfiguration()->getTitle()}</info>",
67
                    $vaultConfiguration->getAdapter(),
68
                    $vaultConfiguration->getVaultLayout(),
69
                    $vaultConfiguration->getLockAdapter(),
70
                    $vaultConfiguration->getIndexMerger(),
71
                    $vaultConfiguration->getOperationListBuilder(),
72
                    implode(
73
                        ',',
74
                        array_map(
75
                            function($key, $value) { return "{$key}: {$value}"; },
76
                            array_keys($vaultConfiguration->getSettings()),
77
                            array_values($vaultConfiguration->getSettings())
78
                        )
79
                    ) ?: '-',
80
                    $currentLock ? "Locked {$currentLock->getAcquired()->format('c')} by {$currentLock->getIdentity()}" : '-'
81
                ])
82
            ]);
83
        }
84
85
        $table->render();
86
    }
87
88
    protected function displaySynchronizationHistory(Storeman $storeman, OutputInterface $output)
89
    {
90
        $output->writeln('');
91
92
        $history = array_reverse($this->buildSynchronizationHistory($storeman), true);
93
94
        if (count($history))
95
        {
96
            $output->writeln('Last synchronizations (recent first):');
97
98
            $table = new Table($output);
99
            $table->setHeaders(['Revision', 'Time (Start)', 'Identity', 'Vault(s)']);
100
101
            foreach ($history as $revision => $synchronizations)
102
            {
103
                $time = \DateTime::createFromFormat('U', 0);
104
                $identity = null;
105
106
                foreach ($synchronizations as $vaultTitle => $synchronization)
107
                {
108
                    /** @var Synchronization $synchronization */
109
110
                    $identity = $synchronization->getIdentity();
111
                    $time = max($time, $synchronization->getTime());
112
                }
113
114
                $table->addRow([
115
                    $revision,
116
                    $time->format('r'),
117
                    $identity,
118
                    implode(',', array_unique(array_keys($synchronizations)))
119
                ]);
120
            }
121
122
            $table->render();
123
        }
124
        else
125
        {
126
            $output->writeln('No synchronizations so far.');
127
        }
128
    }
129
130
    /**
131
     * Builds and returns a history of all synchronizations on record for this archive.
132
     *
133
     * @param Storeman $storeman
134
     * @return Synchronization[][]
135
     */
136
    protected function buildSynchronizationHistory(Storeman $storeman): array
137
    {
138
        $return = [];
139
140
        foreach ($storeman->getVaultContainer() as $vault)
141
        {
142
            /** @var Vault $vault */
143
144
            $vaultConfig = $vault->getVaultConfiguration();
145
            $list = $vault->loadSynchronizationList();
146
147
            foreach ($list as $synchronization)
148
            {
149
                /** @var Synchronization $synchronization */
150
151
                $return[$synchronization->getRevision()][$vaultConfig->getTitle()] = $synchronization;
152
            }
153
        }
154
155
        ksort($return);
156
157
        return $return;
158
    }
159
}
160