Bzip2Compressor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 3 1
A open() 0 8 2
A write() 0 9 2
A __construct() 0 4 2
A close() 0 3 1
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 Bzip2Compressor extends Factory
17
{
18
    /**
19
     * @var false|resource
20
     */
21
    private $handler;
22
23
    public function __construct()
24
    {
25
        if (! function_exists('bzopen')) {
26
            throw Exception::compressionDriverMissing('bzip2');
27
        }
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function open(string $filename, string $mode = 'w'): bool
34
    {
35
        $this->handler = bzopen($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 = bzwrite($this->handler, $data);
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type boolean; however, parameter $bz of bzwrite() 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 = bzwrite(/** @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
        return '';
63
        /* while (!bzeof($this->handler)) {
64
            // Read buffer-size bytes
65
            $content .= bzread($this->handler, 4096); // read 4kb at a time
66
        } */
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72
    public function close(): bool
73
    {
74
        return bzclose($this->handler);
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type boolean; however, parameter $bz of bzclose() 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

74
        return bzclose(/** @scrutinizer ignore-type */ $this->handler);
Loading history...
75
    }
76
}
77