GZip::isGzip()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Packagist Mirror.
7
 *
8
 * For the full license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webs\Mirror;
13
14
/**
15
 * Trait to gzip operations.
16
 *
17
 * @author Webysther Nunes <[email protected]>
18
 */
19
trait GZip
20
{
21
    /**
22
     * Check if is gzip, if not compress.
23
     *
24
     * @param string $gzip
25
     *
26
     * @return string
27
     */
28 1
    public function encode(string $gzip):string
29
    {
30 1
        if ($this->isGzip($gzip)) {
31 1
            return $gzip;
32
        }
33
34 1
        return gzencode($gzip);
35
    }
36
37
    /**
38
     * Check if is gzip, if yes uncompress.
39
     *
40
     * @param string $gzip
41
     *
42
     * @return string
43
     */
44 1
    public function decode(string $gzip):string
45
    {
46 1
        if ($this->isGzip($gzip)) {
47 1
            return gzdecode($gzip);
48
        }
49
50 1
        return $gzip;
51
    }
52
53
    /**
54
     * Check if is gzip.
55
     *
56
     * @param string $gzip
57
     *
58
     * @return bool
59
     */
60 1
    public function isGzip(string $gzip):bool
61
    {
62 1
        if (mb_strpos($gzip, "\x1f"."\x8b"."\x08") === 0) {
63 1
            return true;
64
        }
65
66 1
        return false;
67
    }
68
}
69