Completed
Pull Request — master (#13)
by Peter
03:28
created

OutputStream::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * GpsLab component.
6
 *
7
 * @author    Peter Gribanov <[email protected]>
8
 * @copyright Copyright (c) 2011-2019, Peter Gribanov
9
 * @license   http://opensource.org/licenses/MIT
10
 */
11
12
namespace GpsLab\Component\Sitemap\Stream;
13
14
use GpsLab\Component\Sitemap\Render\SitemapRender;
15
use GpsLab\Component\Sitemap\Stream\Exception\LinksOverflowException;
16
use GpsLab\Component\Sitemap\Stream\Exception\SizeOverflowException;
17
use GpsLab\Component\Sitemap\Stream\Exception\StreamStateException;
18
use GpsLab\Component\Sitemap\Stream\State\StreamState;
19
use GpsLab\Component\Sitemap\Url\Url;
20
21
class OutputStream implements Stream
22
{
23
    /**
24
     * @var SitemapRender
25
     */
26
    private $render;
27
28
    /**
29
     * @var StreamState
30
     */
31
    private $state;
32
33
    /**
34
     * @var int
35
     */
36
    private $counter = 0;
37
38
    /**
39
     * @var int
40
     */
41
    private $used_bytes = 0;
42
43
    /**
44
     * @var string
45
     */
46
    private $end_string = '';
47
48
    /**
49
     * @param SitemapRender $render
50
     */
51 9
    public function __construct(SitemapRender $render)
52
    {
53 9
        $this->render = $render;
54 9
        $this->state = new StreamState();
55 9
    }
56
57 7
    public function open(): void
58
    {
59 7
        $this->state->open();
60 7
        $this->send($this->render->start());
61
        // render end string only once
62 7
        $this->end_string = $this->render->end();
63 7
    }
64
65 8
    public function close(): void
66
    {
67 8
        $this->state->close();
68 7
        $this->send($this->end_string);
69 7
        $this->counter = 0;
70 7
        $this->used_bytes = 0;
71 7
    }
72
73
    /**
74
     * @param Url $url
75
     */
76 5 View Code Duplication
    public function push(Url $url): void
77
    {
78 5
        if (!$this->state->isReady()) {
79 2
            throw StreamStateException::notReady();
80
        }
81
82 3
        if ($this->counter >= self::LINKS_LIMIT) {
83 1
            throw LinksOverflowException::withLimit(self::LINKS_LIMIT);
84
        }
85
86 3
        $render_url = $this->render->url($url);
87 3
        $expected_bytes = $this->used_bytes + strlen($render_url) + strlen($this->end_string);
88
89 3
        if ($expected_bytes > self::BYTE_LIMIT) {
90 1
            throw SizeOverflowException::withLimit(self::BYTE_LIMIT);
91
        }
92
93 3
        $this->send($render_url);
94 3
        ++$this->counter;
95 3
    }
96
97
    /**
98
     * @param string $content
99
     */
100 7
    private function send(string $content): void
101
    {
102 7
        echo $content;
103 7
        flush();
104 7
        $this->used_bytes += strlen($content);
105 7
    }
106
}
107