Completed
Push — master ( 0d3592...0bb29a )
by Peter
06:28
created

RenderBzip2FileStream::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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
    const LINKS_LIMIT = 50000;
22
23
    const BYTE_LIMIT = 52428800; // 50 Mb
24
25
    /**
26
     * @var SitemapRender
27
     */
28
    private $render;
29
30
    /**
31
     * @var StreamState
32
     */
33
    private $state;
34
35
    /**
36
     * @var resource|null
37
     */
38
    private $handle;
39
40
    /**
41
     * @var string
42
     */
43
    private $filename = '';
44
45
    /**
46
     * @var int
47
     */
48
    private $counter = 0;
49
50
    /**
51
     * @var string
52
     */
53
    private $end_string = '';
54
55
    /**
56
     * @var int
57
     */
58
//    private $used_bytes = 0;
59
60
    /**
61
     * @param SitemapRender $render
62
     * @param string        $filename
63
     */
64
    public function __construct(SitemapRender $render, $filename)
65
    {
66
        $this->render = $render;
67
        $this->state = new StreamState();
68
        $this->filename = $filename;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getFilename()
75
    {
76
        return $this->filename;
77
    }
78
79 View Code Duplication
    public function open()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        $this->state->open();
82
83
        if ((file_exists($this->filename) && !is_writable($this->filename)) ||
84
            ($this->handle = @bzopen($this->filename, 'w')) === false
85
        ) {
86
            throw FileAccessException::notWritable($this->filename);
87
        }
88
89
        $this->write($this->render->start());
90
        // render end string only once
91
        $this->end_string = $this->render->end();
92
    }
93
94
    public function close()
95
    {
96
        $this->state->close();
97
        $this->write($this->end_string);
98
        bzclose($this->handle);
99
    }
100
101
    /**
102
     * @param Url $url
103
     */
104 View Code Duplication
    public function push(Url $url)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        if (!$this->state->isReady()) {
107
            throw StreamStateException::notReady();
108
        }
109
110
        if ($this->counter >= self::LINKS_LIMIT) {
111
            throw LinksOverflowException::withLimit(self::LINKS_LIMIT);
112
        }
113
114
        $render_url = $this->render->url($url);
115
116
        // impossible calculate expected size
117
//        $expected_bytes = $this->used_bytes + strlen($render_url) + strlen($this->end_string);
118
//        if ($expected_bytes > self::BYTE_LIMIT) {
119
//            throw SizeOverflowException::withLimit(self::BYTE_LIMIT);
120
//        }
121
122
        $this->write($render_url);
123
        ++$this->counter;
124
    }
125
126
    /**
127
     * @return int
128
     */
129
    public function count()
130
    {
131
        return $this->counter;
132
    }
133
134
    /**
135
     * @param string $string
136
     */
137
    private function write($string)
138
    {
139
        bzwrite($this->handle, $string);
140
//        $this->used_bytes += strlen($string);
141
    }
142
}
143