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

RenderIndexFileStream   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 139
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getFilename() 0 4 1
A open() 0 6 1
A close() 0 8 1
A push() 0 15 3
A addSubStreamFileToIndex() 0 13 1
A getIndexPartFilename() 0 10 1
A count() 0 4 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\SitemapIndexRender;
13
use GpsLab\Component\Sitemap\Render\SitemapRender;
14
use GpsLab\Component\Sitemap\Stream\Exception\OverflowException;
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 RenderIndexFileStream implements FileStream
20
{
21
    /**
22
     * @var SitemapIndexRender
23
     */
24
    private $render;
25
26
    /**
27
     * @var SitemapRender
28
     */
29
    private $substream;
30
31
    /**
32
     * @var StreamState
33
     */
34
    private $state;
35
36
    /**
37
     * @var string
38
     */
39
    private $host = '';
40
41
    /**
42
     * @var string
43
     */
44
    private $filename = '';
45
46
    /**
47
     * @var int
48
     */
49
    private $index = 0;
50
51
    /**
52
     * @var int
53
     */
54
    private $counter = 0;
55
56
    /**
57
     * @var string
58
     */
59
    private $buffer = '';
60
61
    /**
62
     * @param SitemapIndexRender $render
63
     * @param FileStream         $substream
64
     * @param string             $host
65
     * @param string             $filename
66
     */
67
    public function __construct(SitemapIndexRender $render, FileStream $substream, $host, $filename)
68
    {
69
        $this->render = $render;
70
        $this->substream = $substream;
0 ignored issues
show
Documentation Bug introduced by
It seems like $substream of type object<GpsLab\Component\...emap\Stream\FileStream> is incompatible with the declared type object<GpsLab\Component\...p\Render\SitemapRender> of property $substream.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
        $this->host = $host;
72
        $this->filename = $filename;
73
        $this->state = new StreamState();
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getFilename()
80
    {
81
        return $this->filename;
82
    }
83
84
    public function open()
85
    {
86
        $this->state->open();
87
        $this->substream->open();
0 ignored issues
show
Bug introduced by
The method open() does not seem to exist on object<GpsLab\Component\...p\Render\SitemapRender>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
        $this->buffer = $this->render->start();
89
    }
90
91
    public function close()
92
    {
93
        $this->state->close();
94
        $this->addSubStreamFileToIndex();
95
96
        file_put_contents($this->filename, $this->buffer.$this->render->end());
97
        $this->buffer = '';
98
    }
99
100
    /**
101
     * @param Url $url
102
     */
103
    public function push(Url $url)
104
    {
105
        if (!$this->state->isReady()) {
106
            throw StreamStateException::notReady();
107
        }
108
109
        try {
110
            $this->substream->push($url);
0 ignored issues
show
Bug introduced by
The method push() does not seem to exist on object<GpsLab\Component\...p\Render\SitemapRender>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
        } catch (OverflowException $e) {
112
            $this->addSubStreamFileToIndex();
113
            $this->substream->open();
0 ignored issues
show
Bug introduced by
The method open() does not seem to exist on object<GpsLab\Component\...p\Render\SitemapRender>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
        }
115
116
        ++$this->counter;
117
    }
118
119
    private function addSubStreamFileToIndex()
120
    {
121
        $this->substream->close();
0 ignored issues
show
Bug introduced by
The method close() does not seem to exist on object<GpsLab\Component\...p\Render\SitemapRender>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
123
        $filename = $this->substream->getFilename();
0 ignored issues
show
Bug introduced by
The method getFilename() does not seem to exist on object<GpsLab\Component\...p\Render\SitemapRender>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
        $indexed_filename = $this->getIndexPartFilename($filename, ++$this->index);
125
        $last_mod = (new \DateTimeImmutable())->setTimestamp(filemtime($filename));
126
127
        // rename sitemap file to the index part file
128
        rename($filename, dirname($filename).'/'.$indexed_filename);
129
130
        $this->buffer .= $this->render->sitemap($this->host.$indexed_filename, $last_mod);
1 ignored issue
show
Security Bug introduced by
It seems like $last_mod defined by (new \DateTimeImmutable(...p(filemtime($filename)) on line 125 can also be of type false; however, GpsLab\Component\Sitemap...pIndexRender::sitemap() does only seem to accept null|object<DateTimeImmutable>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
131
    }
132
133
    /**
134
     * @param string $filename
135
     * @param int    $index
136
     *
137
     * @return string
138
     */
139
    private function getIndexPartFilename($filename, $index)
140
    {
141
        // use explode() for correct add index
142
        // sitemap.xml -> sitemap1.xml
143
        // sitemap.xml.gz -> sitemap1.xml.gz
144
145
        list($filename, $extension) = explode('.', basename($filename), 2);
146
147
        return sprintf('%s%s.%s', $filename, $index, $extension);
148
    }
149
150
    /**
151
     * @return int
152
     */
153
    public function count()
154
    {
155
        return $this->counter;
156
    }
157
}
158