Passed
Push — master ( 91a3e8...7ce289 )
by Dāvis
05:04
created

Sitemap::setEndIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Sitemap;
4
5
use Sludio\HelperBundle\Sitemap\Dumper\DumperFileInterface;
6
use Sludio\HelperBundle\Sitemap\Dumper\DumperInterface;
7
use Sludio\HelperBundle\Sitemap\Entity\Image;
8
use Sludio\HelperBundle\Sitemap\Entity\SitemapIndex;
9
use Sludio\HelperBundle\Sitemap\Entity\Url;
10
use Sludio\HelperBundle\Sitemap\Entity\Video;
11
use Sludio\HelperBundle\Sitemap\Formatter\FormatterInterface;
12
use Sludio\HelperBundle\Sitemap\Formatter\SitemapIndexFormatterInterface;
13
use Sludio\HelperBundle\Sitemap\Provider\ProviderInterface;
14
use Symfony\Component\HttpFoundation\RequestStack;
15
use Sludio\HelperBundle\Script\Utils\Helper;
16
17
class Sitemap
18
{
19
    protected $providers = [];
20
    protected $dumper;
21
    protected $formatter;
22
    protected $baseHost;
23
    protected $limit = 0;
24
    protected $sitemapIndexes = [];
25
    protected $originalFilename;
26
27
    public function __construct(DumperInterface $dumper, FormatterInterface $formatter, $baseHost = null, $limit = 0, RequestStack $requestStack)
28
    {
29
        $this->dumper = $dumper;
30
        $this->formatter = $formatter;
31
        $this->baseHost = $baseHost;
32
        if ($this->baseHost === null && PHP_SAPI !== 'cli') {
33
            $request = $requestStack->getCurrentRequest();
34
            $this->baseHost = (Helper::useHttps($request) ? 'https' : 'http').'://'.$request->server->get('HTTP_HOST');
35
        }
36
        $this->limit = $limit;
37
        if ($this->isSitemapIndexable()) {
38
            if ($dumper instanceof DumperFileInterface) {
39
                $this->originalFilename = $dumper->getFilename();
40
            }
41
        }
42
    }
43
44
    protected function isSitemapIndexable()
45
    {
46
        return ($this->limit > 0 && $this->dumper instanceof DumperFileInterface && $this->formatter instanceof SitemapIndexFormatterInterface);
47
    }
48
49
    public function addProvider(ProviderInterface $provider)
50
    {
51
        $this->providers[] = $provider;
52
53
        return $this;
54
    }
55
56
    public function setDumper(DumperInterface $dumper)
57
    {
58
        $this->dumper = $dumper;
59
60
        return $this;
61
    }
62
63
    public function build()
64
    {
65
        if ($this->isSitemapIndexable()) {
66
            $this->addSitemapIndex($this->createSitemapIndex());
67
        }
68
69
        $this->dumper->dump($this->formatter->getSitemapStart());
70
71
        foreach ($this->providers as $provider) {
72
            $provider->populate($this);
73
        }
74
75
        $sitemapContent = $this->dumper->dump($this->formatter->getSitemapEnd());
76
77
        if (!$this->isSitemapIndexable()) {
78
            return $sitemapContent;
79
        }
80
81
        if (\count($this->sitemapIndexes)) {
82
            $this->dumper->setFilename($this->originalFilename);
83
84
            $this->dumper->dump($this->formatter->getSitemapIndexStart());
85
            foreach ($this->sitemapIndexes as $sitemapIndex) {
86
                $this->dumper->dump($this->formatter->formatSitemapIndex($sitemapIndex));
87
            }
88
89
            $this->dumper->dump($this->formatter->getSitemapIndexEnd());
90
        }
91
92
        return null;
93
    }
94
95
    protected function addSitemapIndex(SitemapIndex $sitemapIndex)
96
    {
97
        $nbSitemapIndexs = \count($this->sitemapIndexes);
98
99
        if ($nbSitemapIndexs > 0) {
100
            $this->dumper->dump($this->formatter->getSitemapEnd());
101
        }
102
        $sitemapIndexFilename = $this->getSitemapIndexFilename($this->originalFilename);
103
        $this->dumper->setFilename($sitemapIndexFilename);
0 ignored issues
show
Bug introduced by
The method setFilename() does not exist on Sludio\HelperBundle\Sitemap\Dumper\DumperInterface. It seems like you code against a sub-type of Sludio\HelperBundle\Sitemap\Dumper\DumperInterface such as Sludio\HelperBundle\Site...per\DumperFileInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        $this->dumper->/** @scrutinizer ignore-call */ 
104
                       setFilename($sitemapIndexFilename);
Loading history...
104
105
        $this->sitemapIndexes[] = $sitemapIndex;
106
        if ($nbSitemapIndexs > 0) {
107
            $this->dumper->dump($this->formatter->getSitemapStart());
108
        }
109
    }
110
111
    protected function getSitemapIndexFilename($filename)
112
    {
113
        $sitemapIndexFilename = basename($filename);
114
        $index = \count($this->sitemapIndexes) + 1;
115
        $extPosition = strrpos($sitemapIndexFilename, '.');
116
        if ($extPosition !== false) {
0 ignored issues
show
introduced by
The condition $extPosition !== false can never be false.
Loading history...
117
            $sitemapIndexFilename = substr($sitemapIndexFilename, 0, $extPosition).'-'.$index.substr($sitemapIndexFilename, $extPosition);
118
        } else {
119
            $sitemapIndexFilename .= '-'.$index;
120
        }
121
122
        $sitemapIndexFilename = \dirname($filename).DIRECTORY_SEPARATOR.$sitemapIndexFilename;
123
124
        return $sitemapIndexFilename;
125
    }
126
127
    protected function createSitemapIndex()
128
    {
129
        $sitemapIndex = new SitemapIndex();
130
        $sitemapIndex->setLastmod(new \DateTime());
131
132
        return $sitemapIndex;
133
    }
134
135
    private function addIndex()
136
    {
137
        if ($this->isSitemapIndexable() && $this->getCurrentSitemapIndex()->getUrlCount() >= $this->limit) {
138
            $this->addSitemapIndex($this->createSitemapIndex());
139
        }
140
    }
141
142
    private function parseBaseHost(Url $url, $loc)
143
    {
144
        if ($this->baseHost !== null) {
145
            if ($this->needHost($loc)) {
146
                $url->setLoc($this->baseHost.$loc);
147
            }
148
149
            $this->parseVideo($url);
150
            $this->parseImage($url);
151
        }
152
    }
153
154
    private function setEndIndex()
155
    {
156
        if ($this->isSitemapIndexable()) {
157
            $this->getCurrentSitemapIndex()->incrementUrl();
158
        }
159
    }
160
161
    public function add(Url $url)
162
    {
163
        $this->addIndex();
164
165
        $loc = $url->getLoc();
166
        if (empty($loc)) {
167
            throw new \InvalidArgumentException('The url MUST have a loc attribute');
168
        }
169
170
        $this->parseBaseHost($url, $loc);
171
        $this->dumper->dump($this->formatter->formatUrl($url));
172
        $this->setEndIndex();
173
174
        return $this;
175
    }
176
177
    protected function parseVideo(Url $url)
178
    {
179
        foreach ($url->getVideos() as $video) {
180
            /** @var Video $video */
181
            if ($this->needHost($video->getThumbnailLoc())) {
182
                $video->setThumbnailLoc($this->baseHost.$video->getThumbnailLoc());
183
            }
184
185
            if ($this->needHost($video->getContentLoc())) {
186
                $video->setContentLoc($this->baseHost.$video->getContentLoc());
187
            }
188
189
            $player = $video->getPlayerLoc();
190
            if ($player !== null && $this->needHost($player['loc'])) {
191
                $video->setPlayerLoc($this->baseHost.$player['loc'], $player['allow_embed'], $player['autoplay']);
192
            }
193
194
            $gallery = $video->getGalleryLoc();
195
            if ($gallery !== null && $this->needHost($gallery['loc'])) {
196
                $video->setGalleryLoc($this->baseHost.$gallery['loc'], $gallery['title']);
197
            }
198
        }
199
    }
200
201
    protected function parseImage(Url $url)
202
    {
203
        foreach ($url->getImages() as $image) {
204
            /** @var Image $image */
205
            if ($this->needHost($image->getLoc())) {
206
                $image->setLoc($this->baseHost.$image->getLoc());
207
            }
208
209
            if ($this->needHost($image->getLicense())) {
210
                $image->setLicense($this->baseHost.$image->getLicense());
211
            }
212
        }
213
    }
214
215
    protected function getCurrentSitemapIndex()
216
    {
217
        return end($this->sitemapIndexes);
218
    }
219
220
    protected function needHost($url)
221
    {
222
        if ($url === null) {
223
            return false;
224
        }
225
226
        return 0 !== strpos($url, 'http');
227
    }
228
}
229