GzipstreamCompressor::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 DeflateContext;
15
use Dimtrovich\DbDumper\Exceptions\Exception;
16
17
class GzipstreamCompressor extends Factory
18
{
19
    /**
20
     * @var false|resource
21
     */
22
    private $handler;
23
24
    private DeflateContext $context;
25
26
    /**
27
     * {@inheritDoc}
28
     */
29
    public function open(string $filename, string $mode = 'wb'): bool
30
    {
31
        $this->handler = fopen($filename, $mode);
32
        if (false === $this->handler) {
33
            throw Exception::fileNotWritable($filename);
34
        }
35
36
        $this->context = deflate_init(ZLIB_ENCODING_GZIP, ['level' => 9]);
37
38
        return true;
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function write(string $data): int
45
    {
46
        $bytesWritten = fwrite($this->handler, deflate_add($this->context, $data, ZLIB_NO_FLUSH));
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type boolean; however, parameter $stream of fwrite() 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

46
        $bytesWritten = fwrite(/** @scrutinizer ignore-type */ $this->handler, deflate_add($this->context, $data, ZLIB_NO_FLUSH));
Loading history...
47
48
        if (false === $bytesWritten) {
49
            throw Exception::failledToWrite();
50
        }
51
52
        return $bytesWritten;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function read(): string
59
    {
60
        $content = '';
61
62
        while (! feof($this->handler)) {
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type boolean; however, parameter $stream of feof() 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

62
        while (! feof(/** @scrutinizer ignore-type */ $this->handler)) {
Loading history...
63
            // Read buffer-size bytes
64
            $content .= fread($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 fread() 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
            $content .= fread(/** @scrutinizer ignore-type */ $this->handler, 4096); // read 4kb at a time
Loading history...
65
        }
66
67
        return $content;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function close(): bool
74
    {
75
        fwrite($this->handler, deflate_add($this->context, '', ZLIB_FINISH));
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type boolean; however, parameter $stream of fwrite() 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

75
        fwrite(/** @scrutinizer ignore-type */ $this->handler, deflate_add($this->context, '', ZLIB_FINISH));
Loading history...
76
77
        return fclose($this->handler);
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type boolean; however, parameter $stream of fclose() 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 fclose(/** @scrutinizer ignore-type */ $this->handler);
Loading history...
78
    }
79
}
80