Completed
Push — master ( ce7b38...6ec792 )
by Peter
04:34
created

GzipCompressor::compress()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 21
Code Lines 12

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 21
loc 21
ccs 0
cts 17
cp 0
rs 8.7624
cc 6
eloc 12
nc 8
nop 2
crap 42
1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
namespace GpsLab\Component\Sitemap\Compressor;
10
11 View Code Duplication
class GzipCompressor implements CompressorInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
    /**
14
     * @param string $source
15
     * @param string $target
16
     *
17
     * @return bool
18
     */
19
    public function compress($source, $target = '')
20
    {
21
        $target = $target ?: $source.'.gz';
22
        $rh = @fopen($source, 'rb');
23
        $gz = @gzopen($target, 'w9');
24
25
        if ($rh === false || $gz === false) {
26
            return false;
27
        }
28
29
        while (!feof($rh)) {
30
            if (gzwrite($gz, fread($rh, 1024)) === false) {
31
                return false;
32
            }
33
        }
34
35
        fclose($rh);
36
        gzclose($gz);
37
38
        return true;
39
    }
40
}
41