Completed
Pull Request — master (#89)
by Peter
02:19
created

RenderFileStream::open()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

Changes 0
Metric Value
dl 23
loc 23
c 0
b 0
f 0
ccs 11
cts 13
cp 0.8462
rs 9.552
cc 3
nc 3
nop 0
crap 3.0327
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\SizeOverflowException;
16
use GpsLab\Component\Sitemap\Stream\Exception\StreamStateException;
17
use GpsLab\Component\Sitemap\Stream\State\StreamState;
18
use GpsLab\Component\Sitemap\Url\Url;
19
20
class RenderFileStream implements FileStream
21
{
22
    /**
23
     * @var SitemapRender
24
     */
25
    private $render;
26
27
    /**
28
     * @var StreamState
29
     */
30
    private $state;
31
32
    /**
33
     * @var resource|null
34
     */
35
    private $handle;
36
37
    /**
38
     * @var string
39
     */
40
    private $filename = '';
41
42
    /**
43
     * @var string
44
     */
45
    private $tmp_filename = '';
46
47
    /**
48
     * @var int
49
     */
50
    private $counter = 0;
51
52
    /**
53
     * @var string
54
     */
55
    private $end_string = '';
56
57
    /**
58
     * @var int
59
     */
60
    private $used_bytes = 0;
61
62
    /**
63
     * @param SitemapRender $render
64
     * @param string        $filename
65
     */
66 22
    public function __construct(SitemapRender $render, $filename)
67
    {
68 22
        $this->render = $render;
69 22
        $this->state = new StreamState();
70 22
        $this->filename = $filename;
71 22
    }
72
73
    /**
74
     * @return string
75
     */
76 9
    public function getFilename()
77
    {
78 9
        return $this->filename;
79
    }
80
81
    /**
82
     * @return void
83
     */
84 16 View Code Duplication
    public function open()
85
    {
86 16
        $this->state->open();
87
88 16
        $tmp_filename = tempnam(sys_get_temp_dir(), 'sitemap');
89
90 16
        if ($tmp_filename === false) {
91
            throw FileAccessException::failedCreateUnique(sys_get_temp_dir(), 'sitemap');
92
        }
93
94 16
        $handle = @fopen($tmp_filename, 'wb');
95
96 16
        if ($handle === false) {
97
            throw FileAccessException::notWritable($tmp_filename);
98
        }
99
100 16
        $this->tmp_filename = $tmp_filename;
101 16
        $this->handle = $handle;
102
103 16
        $this->write($this->render->start());
104
        // render end string only once
105 16
        $this->end_string = $this->render->end();
106 16
    }
107
108
    /**
109
     * @return void
110
     */
111 19 View Code Duplication
    public function close()
112
    {
113 19
        $this->state->close();
114 16
        $this->write($this->end_string);
115 16
        fclose($this->handle);
116
117 16
        if (!rename($this->tmp_filename, $this->filename)) {
118
            unlink($this->tmp_filename);
119
120
            throw FileAccessException::failedOverwrite($this->tmp_filename, $this->filename);
121
        }
122
123 16
        $this->handle = null;
124 16
        $this->tmp_filename = '';
125 16
        $this->counter = 0;
126 16
        $this->used_bytes = 0;
127 16
    }
128
129
    /**
130
     * @param Url $url
131
     *
132
     * @return void
133
     */
134 10 View Code Duplication
    public function push(Url $url)
135
    {
136 10
        if (!$this->state->isReady()) {
137 2
            throw StreamStateException::notReady();
138
        }
139
140 8
        if ($this->counter >= self::LINKS_LIMIT) {
141 2
            throw LinksOverflowException::withLimit(self::LINKS_LIMIT);
142
        }
143
144 8
        $render_url = $this->render->url($url);
145
146 8
        $expected_bytes = $this->used_bytes + strlen($render_url) + strlen($this->end_string);
147 8
        if ($expected_bytes > self::BYTE_LIMIT) {
148 1
            throw SizeOverflowException::withLimit(self::BYTE_LIMIT);
149
        }
150
151 8
        $this->write($render_url);
152 8
        ++$this->counter;
153 8
    }
154
155
    /**
156
     * @return int
157
     */
158 2
    public function count()
159
    {
160 2
        return $this->counter;
161
    }
162
163
    /**
164
     * @param string $string
165
     *
166
     * @return void
167
     */
168 16
    private function write($string)
169
    {
170 16
        fwrite($this->handle, $string);
171 16
        $this->used_bytes += strlen($string);
172 16
    }
173
}
174