dimtrovich /
php-db-dumper
| 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
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
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
Loading history...
|
|||||
| 63 | // Read buffer-size bytes |
||||
| 64 | $content .= fread($this->handler, 4096); // read 4kb at a time |
||||
|
0 ignored issues
–
show
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
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
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
Loading history...
|
|||||
| 76 | |||||
| 77 | return fclose($this->handler); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 78 | } |
||||
| 79 | } |
||||
| 80 |