Completed
Pull Request — master (#90)
by Peter
07:18 queued 06:07
created

OutputStream::push()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
dl 20
loc 20
ccs 11
cts 12
cp 0.9167
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.0092
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
    /**
22
     * @var SitemapRender
23
     */
24
    private $render;
25
26
    /**
27
     * @var StreamState
28
     */
29
    private $state;
30
31
    /**
32
     * @var int
33
     */
34
    private $counter = 0;
35
36
    /**
37
     * @var int
38
     */
39
    private $used_bytes = 0;
40
41
    /**
42
     * @var string
43
     */
44
    private $end_string = '';
45
46
    /**
47
     * @param SitemapRender $render
48
     */
49 10
    public function __construct(SitemapRender $render)
50
    {
51 10
        $this->render = $render;
52 10
        $this->state = new StreamState();
53 10
    }
54
55 7
    public function open()
56
    {
57 7
        $this->state->open();
58 7
        $this->send($this->render->start());
59
        // render end string only once
60 7
        $this->end_string = $this->render->end();
61 7
    }
62
63 8
    public function close()
64
    {
65 8
        $this->state->close();
66 7
        $this->send($this->end_string);
67 7
        $this->counter = 0;
68 7
        $this->used_bytes = 0;
69 7
    }
70
71
    /**
72
     * @param Url $url
73
     */
74 5 View Code Duplication
    public function push(Url $url)
75
    {
76 5
        if (!$this->state->isReady()) {
77 2
            throw StreamStateException::notReady();
78
        }
79
80 3
        if ($this->counter >= self::LINKS_LIMIT) {
81 1
            throw LinksOverflowException::withLimit(self::LINKS_LIMIT);
82
        }
83
84 3
        $render_url = $this->render->url($url);
85 3
        $expected_bytes = $this->used_bytes + strlen($render_url) + strlen($this->end_string);
86
87 3
        if ($expected_bytes > self::BYTE_LIMIT) {
88
            throw SizeOverflowException::withLimit(self::BYTE_LIMIT);
89
        }
90
91 3
        $this->send($render_url);
92 3
        ++$this->counter;
93 3
    }
94
95
    /**
96
     * @return int
97
     */
98 2
    public function count()
99
    {
100 2
        return $this->counter;
101
    }
102
103
    /**
104
     * @param string $string
105
     */
106 7
    private function send($string)
107
    {
108 7
        echo $string;
109 7
        flush();
110 7
        $this->used_bytes += strlen($string);
111 7
    }
112
}
113