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

OutputStream::push()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
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\LinksOverflowException;
14
use GpsLab\Component\Sitemap\Stream\Exception\SizeOverflowException;
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 OutputStream implements Stream
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 int
37
     */
38
    private $counter = 0;
39
40
    /**
41
     * @var int
42
     */
43
    private $used_bytes = 0;
44
45
    /**
46
     * @var string
47
     */
48
    private $end_string = '';
49
50
    /**
51
     * @param SitemapRender $render
52
     */
53
    public function __construct(SitemapRender $render)
54
    {
55
        $this->render = $render;
56
        $this->state = new StreamState();
57
    }
58
59
    public function open()
60
    {
61
        $this->state->open();
62
        $this->send($this->render->start());
63
        // render end string only once
64
        $this->end_string = $this->render->end();
65
    }
66
67
    public function close()
68
    {
69
        $this->state->close();
70
        $this->send($this->end_string);
71
    }
72
73
    /**
74
     * @param Url $url
75
     */
76 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...
77
    {
78
        if (!$this->state->isReady()) {
79
            throw StreamStateException::notReady();
80
        }
81
82
        if ($this->counter >= self::LINKS_LIMIT) {
83
            throw LinksOverflowException::withLimit(self::LINKS_LIMIT);
84
        }
85
86
        $render_url = $this->render->url($url);
87
88
        $expected_bytes = $this->used_bytes + strlen($render_url) + strlen($this->end_string);
89
90
        if ($expected_bytes > self::BYTE_LIMIT) {
91
            throw SizeOverflowException::withLimit(self::BYTE_LIMIT);
92
        }
93
94
        $this->send($render_url);
95
96
        ++$this->counter;
97
    }
98
99
    /**
100
     * @return int
101
     */
102
    public function count()
103
    {
104
        return $this->counter;
105
    }
106
107
    /**
108
     * @param string $string
109
     */
110
    private function send($string)
111
    {
112
        echo $string;
113
        flush();
114
        $this->used_bytes += strlen($string);
115
    }
116
}
117