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

PharInfo::calculateCompressionCount()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 35
rs 9.3554
c 0
b 0
f 0
cc 5
nc 2
nop 0
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\PharInfo;
16
17
use function array_flip;
18
use function KevinGH\Box\get_phar_compression_algorithms;
19
use Phar;
20
use PharData;
21
use PharFileInfo;
22
use RecursiveIteratorIterator;
23
use UnexpectedValueException;
24
25
final class PharInfo
26
{
27
    private static $ALGORITHMS;
28
29
    private $phar;
30
31
    /** @var null|array */
32
    private $compressionCount;
33
    /** @var null|string */
34
    private $hash;
35
36
    public function __construct(string $pharFile)
37
    {
38
        if (null === self::$ALGORITHMS) {
39
            self::$ALGORITHMS = array_flip(get_phar_compression_algorithms());
40
            self::$ALGORITHMS[Phar::NONE] = 'None';
41
        }
42
43
        try {
44
            $this->phar = new Phar($pharFile);
45
        } catch (UnexpectedValueException $exception) {
46
            $this->phar = new PharData($pharFile);
47
        }
48
    }
49
50
    public function equals(self $pharInfo): bool
51
    {
52
        return
53
            $pharInfo->getCompressionCount() === $this->getCompressionCount()
54
            && $pharInfo->getNormalizedMetadata() === $this->getNormalizedMetadata()
55
        ;
56
    }
57
58
    public function getCompressionCount(): array
59
    {
60
        if (null === $this->compressionCount || $this->hash !== $this->getPharHash()) {
61
            $this->compressionCount = $this->calculateCompressionCount();
62
            $this->hash = $this->getPharHash();
63
        }
64
65
        return $this->compressionCount;
66
    }
67
68
    /**
69
     * @return Phar|PharData
70
     */
71
    public function getPhar()
72
    {
73
        return $this->phar;
74
    }
75
76
    public function getRoot(): string
77
    {
78
        // Do not cache the result
79
        return 'phar://'.str_replace('\\', '/', realpath($this->phar->getPath())).'/';
80
    }
81
82
    public function getVersion(): string
83
    {
84
        // Do not cache the result
85
        return '' !== $this->phar->getVersion() ? $this->phar->getVersion() : 'No information found';
86
    }
87
88
    public function getNormalizedMetadata(): ?string
89
    {
90
        // Do not cache the result
91
        $metadata = var_export($this->phar->getMetadata(), true);
92
93
        return 'NULL' === $metadata ? null : $metadata;
94
    }
95
96
    private function getPharHash(): string
97
    {
98
        return $this->phar->getSignature()['hash'];
99
    }
100
101
    private function calculateCompressionCount(): array
102
    {
103
        $count = array_fill_keys(
104
            self::$ALGORITHMS,
105
            0
106
        );
107
108
        if ($this->phar instanceof PharData) {
109
            $count[self::$ALGORITHMS[$this->phar->isCompressed()]] = 1;
110
111
            return $count;
112
        }
113
114
        $countFile = static function (array $count, PharFileInfo $file): array {
115
            if (false === $file->isCompressed()) {
116
                ++$count['None'];
117
118
                return $count;
119
            }
120
121
            foreach (self::$ALGORITHMS as $compressionAlgorithmCode => $compressionAlgorithmName) {
122
                if ($file->isCompressed($compressionAlgorithmCode)) {
123
                    ++$count[$compressionAlgorithmName];
124
125
                    return $count;
126
                }
127
            }
128
129
            return $count;
130
        };
131
132
        return array_reduce(
133
            iterator_to_array(new RecursiveIteratorIterator($this->phar), true),
134
            $countFile,
135
            $count
136
        );
137
    }
138
}
139