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

RenderFileStream::close()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 17
loc 17
ccs 10
cts 12
cp 0.8333
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0185
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 16 View Code Duplication
    public function open()
82
    {
83 16
        $this->state->open();
84
85 16
        $this->tmp_filename = tempnam(sys_get_temp_dir(), 'sitemap');
86
87 16
        if (($this->handle = @fopen($this->tmp_filename, 'wb')) === false) {
88
            throw FileAccessException::notWritable($this->tmp_filename);
89
        }
90
91 16
        $this->write($this->render->start());
92
        // render end string only once
93 16
        $this->end_string = $this->render->end();
94 16
    }
95
96 19 View Code Duplication
    public function close()
97
    {
98 19
        $this->state->close();
99 16
        $this->write($this->end_string);
100 16
        fclose($this->handle);
101
102 16
        if (!rename($this->tmp_filename, $this->filename)) {
103
            unlink($this->tmp_filename);
104
105
            throw FileAccessException::failedOverwrite($this->tmp_filename, $this->filename);
106
        }
107
108 16
        $this->handle = null;
109 16
        $this->tmp_filename = '';
110 16
        $this->counter = 0;
111 16
        $this->used_bytes = 0;
112 16
    }
113
114
    /**
115
     * @param Url $url
116
     */
117 10 View Code Duplication
    public function push(Url $url)
118
    {
119 10
        if (!$this->state->isReady()) {
120 2
            throw StreamStateException::notReady();
121
        }
122
123 8
        if ($this->counter >= self::LINKS_LIMIT) {
124 2
            throw LinksOverflowException::withLimit(self::LINKS_LIMIT);
125
        }
126
127 8
        $render_url = $this->render->url($url);
128
129 8
        $expected_bytes = $this->used_bytes + strlen($render_url) + strlen($this->end_string);
130 8
        if ($expected_bytes > self::BYTE_LIMIT) {
131 1
            throw SizeOverflowException::withLimit(self::BYTE_LIMIT);
132
        }
133
134 8
        $this->write($render_url);
135 8
        ++$this->counter;
136 8
    }
137
138
    /**
139
     * @return int
140
     */
141 2
    public function count()
142
    {
143 2
        return $this->counter;
144
    }
145
146
    /**
147
     * @param string $string
148
     */
149 16
    private function write($string)
150
    {
151 16
        fwrite($this->handle, $string);
152 16
        $this->used_bytes += strlen($string);
153 16
    }
154
}
155