Completed
Pull Request — master (#13)
by Peter
05:35
created

RenderGzipFileStream::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 2
Ratio 50 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 2
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * GpsLab component.
6
 *
7
 * @author    Peter Gribanov <[email protected]>
8
 * @copyright Copyright (c) 2011, Peter Gribanov
9
 * @license   http://opensource.org/licenses/MIT
10
 */
11
12
namespace GpsLab\Component\Sitemap\Stream;
13
14
use GpsLab\Component\Sitemap\Render\SitemapRender;
15
use GpsLab\Component\Sitemap\Stream\Exception\CompressionLevelException;
16
use GpsLab\Component\Sitemap\Stream\Exception\FileAccessException;
17
use GpsLab\Component\Sitemap\Stream\Exception\LinksOverflowException;
18
use GpsLab\Component\Sitemap\Stream\Exception\StreamStateException;
19
use GpsLab\Component\Sitemap\Stream\State\StreamState;
20
use GpsLab\Component\Sitemap\Url\Url;
21
22
class RenderGzipFileStream implements FileStream
23
{
24
    /**
25
     * @var SitemapRender
26
     */
27
    private $render;
28
29
    /**
30
     * @var StreamState
31
     */
32
    private $state;
33
34
    /**
35
     * @var resource|null
36
     */
37
    private $handle;
38
39
    /**
40
     * @var string
41
     */
42
    private $filename = '';
43
44
    /**
45
     * @var int
46
     */
47
    private $compression_level = 9;
48
49
    /**
50
     * @var int
51
     */
52
    private $counter = 0;
53
54
    /**
55
     * @var string
56
     */
57
    private $end_string = '';
58
59
    /**
60
     * @param SitemapRender $render
61
     * @param string        $filename
62
     * @param int           $compression_level
63
     */
64 13
    public function __construct(SitemapRender $render, string $filename, int $compression_level = 9)
65
    {
66 13
        if ($compression_level < 1 || $compression_level > 9) {
67 3
            throw CompressionLevelException::invalid($compression_level, 1, 9);
68
        }
69
70 13
        $this->render = $render;
71 13
        $this->state = new StreamState();
72 13
        $this->filename = $filename;
73 13
        $this->compression_level = $compression_level;
74 13
    }
75
76
    /**
77
     * @return string
78
     */
79 1
    public function getFilename(): string
80
    {
81 1
        return $this->filename;
82
    }
83
84 7 View Code Duplication
    public function open(): void
85
    {
86 7
        $this->state->open();
87
88 7
        $mode = 'wb'.$this->compression_level;
89 7
        if ((file_exists($this->filename) && !is_writable($this->filename)) ||
90 7
            ($this->handle = @gzopen($this->filename, $mode)) === false
91
        ) {
92 1
            throw FileAccessException::notWritable($this->filename);
93
        }
94
95 6
        $this->write($this->render->start());
96
        // render end string only once
97 6
        $this->end_string = $this->render->end();
98 6
    }
99
100 7 View Code Duplication
    public function close(): void
101
    {
102 7
        $this->state->close();
103 6
        $this->write($this->end_string);
104 6
        gzclose($this->handle);
105 6
        $this->counter = 0;
106 6
    }
107
108
    /**
109
     * @param Url $url
110
     */
111 4
    public function push(Url $url): void
112
    {
113 4
        if (!$this->state->isReady()) {
114 2
            throw StreamStateException::notReady();
115
        }
116
117 2
        if ($this->counter >= self::LINKS_LIMIT) {
118 1
            throw LinksOverflowException::withLimit(self::LINKS_LIMIT);
119
        }
120
121 2
        $render_url = $this->render->url($url);
122
123 2
        $this->write($render_url);
124 2
        ++$this->counter;
125 2
    }
126
127
    /**
128
     * @param string $string
129
     */
130 6
    private function write(string $string): void
131
    {
132 6
        gzwrite($this->handle, $string);
133 6
    }
134
}
135