GzipCompressor::read()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * This file is part of dimtrovich/db-dumper".
5
 *
6
 * (c) 2024 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\DbDumper\Compressor;
13
14
use Dimtrovich\DbDumper\Exceptions\Exception;
15
16
class GzipCompressor extends Factory
17
{
18
    /**
19
     * @var false|resource
20
     */
21
    private $handler;
22
23
    public function __construct()
24
    {
25
        if (! function_exists('gzopen')) {
26
            throw Exception::compressionDriverMissing('gzip');
27
        }
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function open(string $filename, string $mode = 'wb'): bool
34
    {
35
        $this->handler = gzopen($filename, $mode);
36
        if (false === $this->handler) {
37
            throw Exception::fileNotWritable($filename);
38
        }
39
40
        return true;
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function write(string $data): int
47
    {
48
        $bytesWritten = gzwrite($this->handler, $data);
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type boolean; however, parameter $stream of gzwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        $bytesWritten = gzwrite(/** @scrutinizer ignore-type */ $this->handler, $data);
Loading history...
49
50
        if (false === $bytesWritten) {
51
            throw Exception::failledToWrite();
52
        }
53
54
        return $bytesWritten;
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function read(): string
61
    {
62
        $content = '';
63
64
        while (! gzeof($this->handler)) {
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type boolean; however, parameter $stream of gzeof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        while (! gzeof(/** @scrutinizer ignore-type */ $this->handler)) {
Loading history...
65
            // Read buffer-size bytes
66
            $content .= gzread($this->handler, 4096); // read 4kb at a time
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type boolean; however, parameter $stream of gzread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            $content .= gzread(/** @scrutinizer ignore-type */ $this->handler, 4096); // read 4kb at a time
Loading history...
67
        }
68
69
        return $content;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function close(): bool
76
    {
77
        return gzclose($this->handler);
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type boolean; however, parameter $stream of gzclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        return gzclose(/** @scrutinizer ignore-type */ $this->handler);
Loading history...
78
    }
79
}
80