Completed
Push — master ( d47b87...74095d )
by Peter
29s queued 11s
created

RenderBzip2FileStream::open()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 9
cp 0.8889
rs 9.7666
c 0
b 0
f 0
cc 4
nc 2
nop 0
crap 4.0218
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
10
namespace GpsLab\Component\Sitemap\Stream;
11
12
use GpsLab\Component\Sitemap\Render\SitemapRender;
13
use GpsLab\Component\Sitemap\Stream\Exception\FileAccessException;
14
use GpsLab\Component\Sitemap\Stream\Exception\LinksOverflowException;
15
use GpsLab\Component\Sitemap\Stream\Exception\StreamStateException;
16
use GpsLab\Component\Sitemap\Stream\State\StreamState;
17
use GpsLab\Component\Sitemap\Url\Url;
18
19
class RenderBzip2FileStream implements FileStream
20
{
21
    /**
22
     * @var SitemapRender
23
     */
24
    private $render;
25
26
    /**
27
     * @var StreamState
28
     */
29
    private $state;
30
31
    /**
32
     * @var resource|null
33
     */
34
    private $handle;
35
36
    /**
37
     * @var string
38
     */
39
    private $filename = '';
40
41
    /**
42
     * @var string
43
     */
44
    private $tmp_filename = '';
45
46
    /**
47
     * @var int
48
     */
49
    private $counter = 0;
50
51
    /**
52
     * @var string
53
     */
54
    private $end_string = '';
55
56
    /**
57
     * @param SitemapRender $render
58
     * @param string        $filename
59
     */
60 10
    public function __construct(SitemapRender $render, $filename)
61
    {
62 10
        $this->render = $render;
63 10
        $this->state = new StreamState();
64 10
        $this->filename = $filename;
65 10
    }
66
67
    /**
68
     * @return string
69
     */
70 1
    public function getFilename()
71
    {
72 1
        return $this->filename;
73
    }
74
75 7
    public function open()
76
    {
77 7
        $this->state->open();
78
79 7
        $this->tmp_filename = tempnam(sys_get_temp_dir(), 'sitemap');
80 7
        if ((file_exists($this->filename) && !is_writable($this->filename)) ||
81 7
            ($this->handle = @bzopen($this->tmp_filename, 'w')) === false
82
        ) {
83
            throw FileAccessException::notWritable($this->filename);
84
        }
85
86 7
        $this->write($this->render->start());
87
        // render end string only once
88 7
        $this->end_string = $this->render->end();
89 7
    }
90
91 10 View Code Duplication
    public function close()
92
    {
93 10
        $this->state->close();
94 7
        $this->write($this->end_string);
95 7
        bzclose($this->handle);
96
97 7
        if (!rename($this->tmp_filename, $this->filename)) {
98
            unlink($this->tmp_filename);
99
100
            throw FileAccessException::failedOverwrite($this->tmp_filename, $this->filename);
101
        }
102
103 7
        $this->handle = null;
104 7
        $this->tmp_filename = '';
105 7
        $this->counter = 0;
106 7
    }
107
108
    /**
109
     * @param Url $url
110
     */
111 5 View Code Duplication
    public function push(Url $url)
112
    {
113 5
        if (!$this->state->isReady()) {
114 2
            throw StreamStateException::notReady();
115
        }
116
117 3
        if ($this->counter >= self::LINKS_LIMIT) {
118 1
            throw LinksOverflowException::withLimit(self::LINKS_LIMIT);
119
        }
120
121 3
        $render_url = $this->render->url($url);
122
123 3
        $this->write($render_url);
124 3
        ++$this->counter;
125 3
    }
126
127
    /**
128
     * @return int
129
     */
130 2
    public function count()
131
    {
132 2
        return $this->counter;
133
    }
134
135
    /**
136
     * @param string $string
137
     */
138 7
    private function write($string)
139
    {
140 7
        bzwrite($this->handle, $string);
141 7
    }
142
}
143