NoneCompressor::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 NoneCompressor extends Factory
17
{
18
    /**
19
     * @var false|resource
20
     */
21
    private $handler;
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    public function open(string $filename, string $mode = 'wb'): bool
27
    {
28
        $this->handler = fopen($filename, $mode);
29
        if (false === $this->handler) {
30
            throw Exception::fileNotWritable($filename);
31
        }
32
33
        return true;
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function write(string $data): int
40
    {
41
        $bytesWritten = fwrite($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 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

41
        $bytesWritten = fwrite(/** @scrutinizer ignore-type */ $this->handler, $data);
Loading history...
42
43
        if (false === $bytesWritten) {
44
            throw Exception::failledToWrite();
45
        }
46
47
        return $bytesWritten;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function read(): string
54
    {
55
        $content = '';
56
57
        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

57
        while (! feof(/** @scrutinizer ignore-type */ $this->handler)) {
Loading history...
58
            // Read buffer-size bytes
59
            $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

59
            $content .= fread(/** @scrutinizer ignore-type */ $this->handler, 4096); // read 4kb at a time
Loading history...
60
        }
61
62
        return $content;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function close(): bool
69
    {
70
        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

70
        return fclose(/** @scrutinizer ignore-type */ $this->handler);
Loading history...
71
    }
72
}
73