Completed
Push — master ( 877556...9739a9 )
by Arne
02:42
created

DisplayIndexComparisonHelper::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Storeman\Cli\Helper;
4
5
use Storeman\Index\Comparison\IndexComparison;
6
use Storeman\Index\Comparison\IndexObjectComparison;
7
use Storeman\Index\IndexObject;
8
use Symfony\Component\Console\Helper\Helper;
9
use Symfony\Component\Console\Helper\HelperInterface;
10
use Symfony\Component\Console\Helper\Table;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class DisplayIndexComparisonHelper extends Helper implements HelperInterface
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function getName(): string
19
    {
20
        return 'displayIndexComparison';
21
    }
22
23
    public function displayIndexComparison(IndexComparison $indexComparison, OutputInterface $output, string $titleA = 'IndexA', string $titleB = 'IndexB')
24
    {
25
        $table = new Table($output);
26
        $table->setStyle('compact');
27
        $table->setHeaders(['Path', $titleA, $titleB]);
28
        $table->addRows(array_map(function(IndexObjectComparison $difference) {
29
30
            return [
31
                $difference->getRelativePath(),
32
                $this->renderIndexObjectColumn($difference->getIndexObjectA()),
33
                $this->renderIndexObjectColumn($difference->getIndexObjectB()),
34
            ];
35
36
        }, iterator_to_array($indexComparison->getIterator())));
37
38
        $table->render();
39
    }
40
41
    protected function renderIndexObjectColumn(?IndexObject $indexObject): string
42
    {
43
        if ($indexObject === null)
44
        {
45
            return '-';
46
        }
47
48
        $parts = [
49
            $indexObject->getTypeName(),
50
            "mtime: " . \DateTime::createFromFormat('U', $indexObject->getMtime())->format('c'),
51
            "ctime: " . \DateTime::createFromFormat('U', $indexObject->getCtime())->format('c'),
52
            "perms: 0{$indexObject->getPermissionsString()}",
53
        ];
54
55
        if ($indexObject->isFile())
56
        {
57
            $parts = array_merge($parts, [
58
                "size: " . (($indexObject->getSize() !== null) ? static::formatMemory($indexObject->getSize()) : '-'),
59
                "blobId: " . ($indexObject->getBlobId() ?: '-'),
60
                "hash(es):" . ($indexObject->getHashes() && $indexObject->getHashes()->count() ? ("\n" . str_replace(', ', "\n", $indexObject->getHashes()->__toString())) : ' -'),
61
            ]);
62
        }
63
        elseif ($indexObject->isLink())
64
        {
65
            $parts = array_merge($parts, [
66
                "target: {$indexObject->getLinkTarget()}"
67
            ]);
68
        }
69
70
        return implode("\n", $parts);
71
    }
72
}
73