InfoCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
                    'Identifier:',
57
                    'Title:',
58
                    'Storage adapter:',
59
                    'Vault layout:',
60
                    'Lock adapter:',
61
                    'Index merger:',
62
                    'Operation list builder:',
63
                    'Settings:',
64
                    'Current lock:',
65
                ]),
66
                implode("\n", [
67
                    $vault->getHash(),
68
                    "<info>{$vault->getVaultConfiguration()->getTitle()}</info>",
69
                    $vaultConfiguration->getAdapter(),
70
                    $vaultConfiguration->getVaultLayout(),
71
                    $vaultConfiguration->getLockAdapter(),
72
                    $vaultConfiguration->getIndexMerger(),
73
                    $vaultConfiguration->getOperationListBuilder(),
74
                    implode(
75
                        ',',
76
                        array_map(
77
                            function($key, $value) { return "{$key}: {$value}"; },
78
                            array_keys($vaultConfiguration->getSettings()),
79
                            array_values($vaultConfiguration->getSettings())
80
                        )
81
                    ) ?: '-',
82
                    $currentLock ? "Locked {$currentLock->getAcquired()->format('c')} by {$currentLock->getIdentity()}" : '-'
83
                ])
84
            ]);
85
        }
86
87
        $table->render();
88
    }
89
90
    protected function displaySynchronizationHistory(Storeman $storeman, OutputInterface $output)
91
    {
92
        $output->writeln('');
93
94
        $history = array_reverse($this->buildSynchronizationHistory($storeman), true);
95
96
        if (count($history))
97
        {
98
            $output->writeln('Last synchronizations (recent first):');
99
100
            $table = new Table($output);
101
            $table->setHeaders(['Revision', 'Time (Start)', 'Identity', 'Vault(s)']);
102
103
            foreach ($history as $revision => $synchronizations)
104
            {
105
                $time = \DateTime::createFromFormat('U', 0);
106
                $identity = null;
107
108
                foreach ($synchronizations as $vaultTitle => $synchronization)
109
                {
110
                    /** @var Synchronization $synchronization */
111
112
                    $identity = $synchronization->getIdentity();
113
                    $time = max($time, $synchronization->getTime());
114
                }
115
116
                $table->addRow([
117
                    $revision,
118
                    $time->format('r'),
119
                    $identity,
120
                    implode("\n", array_unique(array_keys($synchronizations)))
121
                ]);
122
            }
123
124
            $table->render();
125
        }
126
        else
127
        {
128
            $output->writeln('No synchronizations so far.');
129
        }
130
    }
131
132
    /**
133
     * Builds and returns a history of all synchronizations on record for this archive.
134
     *
135
     * @param Storeman $storeman
136
     * @return Synchronization[][]
137
     */
138
    protected function buildSynchronizationHistory(Storeman $storeman): array
139
    {
140
        $return = [];
141
142
        foreach ($storeman->getVaultContainer() as $vault)
143
        {
144
            /** @var Vault $vault */
145
146
            $list = $vault->getVaultLayout()->getSynchronizations();
147
148
            foreach ($list as $synchronization)
149
            {
150
                /** @var Synchronization $synchronization */
151
152
                $return[$synchronization->getRevision()][$vault->getIdentifier()] = $synchronization;
153
            }
154
        }
155
156
        ksort($return);
157
158
        return $return;
159
    }
160
}
161