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

OutputStream   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 21.28 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.06%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 20
loc 94
ccs 33
cts 34
cp 0.9706
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A open() 0 7 1
A close() 0 7 1
A count() 0 4 1
A send() 0 6 1
A push() 20 20 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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