Passed
Pull Request — master (#375)
by Théo
02:19
created

PharInfoRenderer::renderContentsSummary()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Console;
16
17
use function KevinGH\Box\format_size;
18
use KevinGH\Box\PharInfo\PharInfo;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
21
/**
22
 * @private
23
 */
24
final class PharInfoRenderer
25
{
26
    public static function renderCompression(PharInfo $pharInfo, SymfonyStyle $io): void
27
    {
28
        $count = array_filter($pharInfo->getCompressionCount());
29
        $totalCount = array_sum($count);
30
31
        if (1 === count($count)) {
32
            $io->writeln(
33
                sprintf(
34
                    '<comment>Compression:</comment> %s',
35
                    key($count)
36
                )
37
            );
38
39
            return;
40
        }
41
42
        $io->writeln('<comment>Compression:</comment>');
43
44
        end($count);
45
        $lastAlgorithmName = key($count);
46
47
        $totalPercentage = 100;
48
49
        foreach ($count as $algorithmName => $nbrOfFiles) {
50
            if ($lastAlgorithmName === $algorithmName) {
51
                $percentage = $totalPercentage;
52
            } else {
53
                $percentage = round($nbrOfFiles * 100 / $totalCount, 2);
54
55
                $totalPercentage -= $percentage;
56
            }
57
58
            $io->writeln(
59
                sprintf(
60
                    '  - %s (%0.2f%%)',
61
                    $algorithmName,
62
                    $percentage
63
                )
64
            );
65
        }
66
    }
67
68
    public static function renderSignature(PharInfo $pharInfo, SymfonyStyle $io): void
69
    {
70
        $signature = $pharInfo->getPhar()->getSignature();
71
72
        if (false === $signature) {
73
            return;
74
        }
75
76
        $io->writeln(
77
            sprintf(
78
                '<comment>Signature:</comment> %s',
79
                $signature['hash_type']
80
            )
81
        );
82
        $io->writeln(
83
            sprintf(
84
                '<comment>Signature Hash:</comment> %s',
85
                $signature['hash']
86
            )
87
        );
88
    }
89
90
    public static function renderMetadata(PharInfo $pharInfo, SymfonyStyle $io): void
91
    {
92
        $metadata = $pharInfo->getNormalizedMetadata();
93
94
        if (null === $metadata) {
95
            $io->writeln('<comment>Metadata:</comment> None');
96
        } else {
97
            $io->writeln('<comment>Metadata:</comment>');
98
            $io->writeln($metadata);
99
        }
100
    }
101
102
    public static function renderContentsSummary(PharInfo $pharInfo, SymfonyStyle $io): void
103
    {
104
        $count = array_filter($pharInfo->getCompressionCount());
105
        $totalCount = array_sum($count);
106
107
        $io->writeln(
108
            sprintf(
109
                '<comment>Contents:</comment>%s (%s)',
110
                1 === $totalCount ? ' 1 file' : " $totalCount files",
111
                format_size(
112
                    filesize($pharInfo->getPhar()->getPath())
113
                )
114
            )
115
        );
116
    }
117
118
    private function __construct()
119
    {
120
    }
121
}
122