Passed
Push — master ( a51f35...a51f35 )
by Dāvis
05:00
created

Sitemap::setDumper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

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