Completed
Pull Request — master (#17)
by Peter
03:02
created

RenderIndexFileStream::addSubStreamFileToIndex()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
rs 9.6333
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 4.0119
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\SitemapIndexRender;
13
use GpsLab\Component\Sitemap\Stream\Exception\FileAccessException;
14
use GpsLab\Component\Sitemap\Stream\Exception\IndexStreamException;
15
use GpsLab\Component\Sitemap\Stream\Exception\OverflowException;
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 RenderIndexFileStream implements FileStream
21
{
22
    /**
23
     * @var SitemapIndexRender
24
     */
25
    private $render;
26
27
    /**
28
     * @var FileStream
29
     */
30
    private $substream;
31
32
    /**
33
     * @var StreamState
34
     */
35
    private $state;
36
37
    /**
38
     * @var resource|null
39
     */
40
    private $handle;
41
42
    /**
43
     * @var string
44
     */
45
    private $host = '';
46
47
    /**
48
     * @var string
49
     */
50
    private $filename = '';
51
52
    /**
53
     * @var string
54
     */
55
    private $tmp_filename = '';
56
57
    /**
58
     * @var int
59
     */
60
    private $index = 0;
61
62
    /**
63
     * @var int
64
     */
65
    private $counter = 0;
66
67
    /**
68
     * @var bool
69
     */
70
    private $empty_index = true;
71
72
    /**
73
     * @param SitemapIndexRender $render
74
     * @param FileStream         $substream
75
     * @param string             $host
76
     * @param string             $filename
77
     */
78 12
    public function __construct(SitemapIndexRender $render, FileStream $substream, $host, $filename)
79
    {
80 12
        $this->render = $render;
81 12
        $this->substream = $substream;
82 12
        $this->host = $host;
83 12
        $this->filename = $filename;
84 12
        $this->state = new StreamState();
85 12
    }
86
87
    /**
88
     * @return string
89
     */
90 1
    public function getFilename()
91
    {
92 1
        return $this->filename;
93
    }
94
95 9 View Code Duplication
    public function open()
96
    {
97 9
        $this->state->open();
98 9
        $this->substream->open();
99
100 9
        $this->tmp_filename = tempnam(sys_get_temp_dir(), 'sitemap_index');
101 9
        if (($this->handle = @fopen($this->tmp_filename, 'wb')) === false) {
102
            throw FileAccessException::notWritable($this->tmp_filename);
103
        }
104
105 9
        fwrite($this->handle, $this->render->start());
106 9
    }
107
108 12
    public function close()
109
    {
110 12
        $this->state->close();
111 9
        $this->substream->close();
112
113
        // not add empty sitemap part to index
114 9
        if (!$this->empty_index) {
115 5
            $this->addSubStreamFileToIndex();
116
        }
117
118 8
        fwrite($this->handle, $this->render->end());
119 8
        fclose($this->handle);
120
121 8
        $this->moveParts();
122
123
        // move the sitemap index file from the temporary directory to the target
124 8
        if (!rename($this->tmp_filename, $this->filename)) {
125
            unlink($this->tmp_filename);
126
127
            throw FileAccessException::failedOverwrite($this->tmp_filename, $this->filename);
128
        }
129
130 8
        $this->removeOldParts();
131
132 8
        $this->handle = null;
133 8
        $this->tmp_filename = '';
134 8
        $this->counter = 0;
135 8
    }
136
137
    /**
138
     * @param Url $url
139
     */
140 7
    public function push(Url $url)
141
    {
142 7
        if (!$this->state->isReady()) {
143 2
            throw StreamStateException::notReady();
144
        }
145
146
        try {
147 5
            $this->substream->push($url);
148 1
        } catch (OverflowException $e) {
149 1
            $this->substream->close();
150 1
            $this->addSubStreamFileToIndex();
151 1
            $this->substream->open();
152 1
            $this->substream->push($url);
153
        }
154
155 5
        $this->empty_index = false;
156 5
        ++$this->counter;
157 5
    }
158
159 5
    private function addSubStreamFileToIndex()
160
    {
161 5
        $filename = $this->substream->getFilename();
162 5
        $indexed_filename = $this->getIndexPartFilename($filename, ++$this->index);
163
164 5
        if (!file_exists($filename) || ($time = filemtime($filename)) === false) {
165 1
            throw FileAccessException::notReadable($filename);
166
        }
167
168 4
        $last_mod = (new \DateTimeImmutable())->setTimestamp($time);
169
170
        // rename sitemap file to sitemap part
171 4
        $new_filename = sys_get_temp_dir().'/'.$indexed_filename;
172 4
        if (!rename($filename, $new_filename)) {
173
            throw IndexStreamException::failedRename($filename, $new_filename);
174
        }
175
176 4
        fwrite($this->handle, $this->render->sitemap($indexed_filename, $last_mod));
0 ignored issues
show
Security Bug introduced by
It seems like $last_mod defined by (new \DateTimeImmutable())->setTimestamp($time) on line 168 can also be of type false; however, GpsLab\Component\Sitemap...pIndexRender::sitemap() does only seem to accept null|object<DateTimeImmutable>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
177 4
    }
178
179
    /**
180
     * @param string $path
181
     * @param int    $index
182
     *
183
     * @return string
184
     */
185 9
    private function getIndexPartFilename($path, $index)
186
    {
187
        // use explode() for correct add index
188
        // sitemap.xml -> sitemap1.xml
189
        // sitemap.xml.gz -> sitemap1.xml.gz
190
191 9
        list($path, $extension) = explode('.', basename($path), 2) + ['', ''];
192
193 9
        return sprintf('%s%s.%s', $path, $index, $extension);
194
    }
195
196
    /**
197
     * @return int
198
     */
199 4
    public function count()
200
    {
201 4
        return $this->counter;
202
    }
203
204
    /**
205
     * Move parts of the sitemap from the temporary directory to the target
206
     */
207 8
    private function moveParts()
208
    {
209 8
        $filename = $this->substream->getFilename();
210 8
        for ($i = 1; $i <= $this->index; ++$i) {
211 4
            $indexed_filename = $this->getIndexPartFilename($filename, $i);
212 4
            $source = sys_get_temp_dir().'/'.$indexed_filename;
213 4
            $target = dirname($this->filename).'/'.$indexed_filename;
214 4
            if (!rename($source, $target)) {
215
                throw IndexStreamException::failedRename($source, $target);
216
            }
217
        }
218 8
    }
219
220
    /**
221
     * Remove old parts of the sitemap from the target directory
222
     */
223 8
    private function removeOldParts()
224
    {
225 8
        $filename = $this->substream->getFilename();
226 8
        for ($i = $this->index + 1; true; ++$i) {
227 8
            $indexed_filename = $this->getIndexPartFilename($filename, $i);
228 8
            $target = dirname($this->filename).'/'.$indexed_filename;
229 8
            if (file_exists($target)) {
230
                unlink($target);
231
            } else {
232 8
                break;
233
            }
234
        }
235 8
    }
236
}
237