Passed
Push — master ( e80b48...a06265 )
by Théo
02:43
created

PharInfoRenderer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 52
dl 0
loc 103
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A renderCompression() 0 37 4
A __construct() 0 2 1
A renderMetadata() 0 9 2
A renderContentsSummary() 0 11 2
A renderSignature() 0 25 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
            $io->writeln(
74
                sprintf(
75
                    '<comment>Signature unreadable</comment> %s',
76
                    $signature['hash_type']
77
                )
78
            );
79
80
            return;
81
        }
82
83
        $io->writeln(
84
            sprintf(
85
                '<comment>Signature:</comment> %s',
86
                $signature['hash_type']
87
            )
88
        );
89
        $io->writeln(
90
            sprintf(
91
                '<comment>Signature Hash:</comment> %s',
92
                $signature['hash']
93
            )
94
        );
95
    }
96
97
    public static function renderMetadata(PharInfo $pharInfo, SymfonyStyle $io): void
98
    {
99
        $metadata = $pharInfo->getNormalizedMetadata();
100
101
        if (null === $metadata) {
102
            $io->writeln('<comment>Metadata:</comment> None');
103
        } else {
104
            $io->writeln('<comment>Metadata:</comment>');
105
            $io->writeln($metadata);
106
        }
107
    }
108
109
    public static function renderContentsSummary(PharInfo $pharInfo, SymfonyStyle $io): void
110
    {
111
        $count = array_filter($pharInfo->getCompressionCount());
112
        $totalCount = array_sum($count);
113
114
        $io->writeln(
115
            sprintf(
116
                '<comment>Contents:</comment>%s (%s)',
117
                1 === $totalCount ? ' 1 file' : " $totalCount files",
118
                format_size(
119
                    filesize($pharInfo->getPhar()->getPath())
120
                )
121
            )
122
        );
123
    }
124
125
    private function __construct()
126
    {
127
    }
128
}
129