Passed
Push — master ( 896e22...61bd56 )
by Dāvis
03:36
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\DumperInterface;
6
use Sludio\HelperBundle\Sitemap\Dumper\DumperFileInterface;
7
use Sludio\HelperBundle\Sitemap\Entity\Url;
8
use Sludio\HelperBundle\Sitemap\Entity\SitemapIndex;
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\Request;
13
14
class Sitemap
15
{
16
    protected $providers = [];
17
    protected $dumper = null;
18
    protected $formatter = null;
19
    protected $baseHost = null;
20
    protected $limit = 0;
21
    protected $sitemapIndexes = [];
22
    protected $originalFilename = null;
23
24
    public function __construct(DumperInterface $dumper, FormatterInterface $formatter, $baseHost = null, $limit = 0)
25
    {
26
        $this->dumper = $dumper;
27
        $this->formatter = $formatter;
28
        $this->baseHost = $baseHost;
29
        if ($this->baseHost === null && php_sapi_name() !== 'cli') {
30
            $request = Request::createFromGlobals();
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
            $this->originalFilename = $dumper->getFilename();
0 ignored issues
show
Bug introduced by
The method getFilename() 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

36
            /** @scrutinizer ignore-call */ 
37
            $this->originalFilename = $dumper->getFilename();
Loading history...
37
        }
38
    }
39
40
    public function addProvider(ProviderInterface $provider)
41
    {
42
        $this->providers[] = $provider;
43
44
        return $this;
45
    }
46
47
    public function setDumper(DumperInterface $dumper)
48
    {
49
        $this->dumper = $dumper;
50
51
        return $this;
52
    }
53
54
    public function build()
55
    {
56
        if ($this->isSitemapIndexable()) {
57
            $this->addSitemapIndex($this->createSitemapIndex());
58
        }
59
60
        $this->dumper->dump($this->formatter->getSitemapStart());
61
62
        foreach ($this->providers as $provider) {
63
            $provider->populate($this);
64
        }
65
66
        $sitemapContent = $this->dumper->dump($this->formatter->getSitemapEnd());
67
68
        if (!$this->isSitemapIndexable()) {
69
            return $sitemapContent;
70
        }
71
72
        if (count($this->sitemapIndexes)) {
73
            $this->dumper->setFilename($this->originalFilename);
74
            $this->dumper->dump($this->formatter->getSitemapIndexStart());
75
            foreach ($this->sitemapIndexes as $sitemapIndex) {
76
                $this->dumper->dump($this->formatter->formatSitemapIndex($sitemapIndex));
77
            }
78
79
            $this->dumper->dump($this->formatter->getSitemapIndexEnd());
80
        }
81
    }
82
83
    public function add(Url $url)
84
    {
85
        if ($this->isSitemapIndexable() && $this->getCurrentSitemapIndex()->getUrlCount() >= $this->limit) {
86
            $this->addSitemapIndex($this->createSitemapIndex());
87
        }
88
89
        $loc = $url->getLoc();
90
        if (empty($loc)) {
91
            throw new \InvalidArgumentException('The url MUST have a loc attribute');
92
        }
93
94
        if ($this->baseHost !== null) {
95
            if ($this->needHost($loc)) {
96
                $url->setLoc($this->baseHost.$loc);
97
            }
98
99
            foreach ($url->getVideos() as $video) {
100
                if ($this->needHost($video->getThumbnailLoc())) {
101
                    $video->setThumbnailLoc($this->baseHost.$video->getThumbnailLoc());
102
                }
103
104
                if ($this->needHost($video->getContentLoc())) {
105
                    $video->setContentLoc($this->baseHost.$video->getContentLoc());
106
                }
107
108
                $player = $video->getPlayerLoc();
109
                if ($player !== null && $this->needHost($player['loc'])) {
110
                    $video->setPlayerLoc($this->baseHost.$player['loc'], $player['allow_embed'], $player['autoplay']);
111
                }
112
113
                $gallery = $video->getGalleryLoc();
114
                if ($gallery !== null && $this->needHost($gallery['loc'])) {
115
                    $video->setGalleryLoc($this->baseHost.$gallery['loc'], $gallery['title']);
116
                }
117
            }
118
119
            foreach ($url->getImages() as $image) {
120
                if ($this->needHost($image->getLoc())) {
121
                    $image->setLoc($this->baseHost.$image->getLoc());
122
                }
123
124
                if ($this->needHost($image->getLicense())) {
125
                    $image->setLicense($this->baseHost.$image->getLicense());
126
                }
127
            }
128
        }
129
130
        $this->dumper->dump($this->formatter->formatUrl($url));
131
132
        if ($this->isSitemapIndexable()) {
133
            $this->getCurrentSitemapIndex()->incrementUrl();
134
        }
135
136
        return $this;
137
    }
138
139
    protected function needHost($url)
140
    {
141
        if ($url === null) {
142
            return false;
143
        }
144
145
        return substr($url, 0, 4) !== 'http';
146
    }
147
148
    protected function isSitemapIndexable()
149
    {
150
        return ($this->limit > 0 && $this->dumper instanceof DumperFileInterface && $this->formatter instanceof SitemapIndexFormatterInterface);
151
    }
152
153
    protected function createSitemapIndex()
154
    {
155
        $sitemapIndexFilename = $this->getSitemapIndexFilename($this->originalFilename);
156
        $sitemapIndex = new SitemapIndex();
157
        $loc = DIRECTORY_SEPARATOR.basename($sitemapIndexFilename);
0 ignored issues
show
Unused Code introduced by
The assignment to $loc is dead and can be removed.
Loading history...
158
159
        $sitemapIndex->setLastMod(new \DateTime());
160
161
        return $sitemapIndex;
162
    }
163
164
    protected function addSitemapIndex(SitemapIndex $sitemapIndex)
165
    {
166
        $nbSitemapIndexs = count($this->sitemapIndexes);
167
168
        if ($nbSitemapIndexs > 0) {
169
            $this->dumper->dump($this->formatter->getSitemapEnd());
170
        }
171
        $sitemapIndexFilename = $this->getSitemapIndexFilename($this->originalFilename);
172
        $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

172
        $this->dumper->/** @scrutinizer ignore-call */ 
173
                       setFilename($sitemapIndexFilename);
Loading history...
173
174
        $this->sitemapIndexes[] = $sitemapIndex;
175
        if ($nbSitemapIndexs > 0) {
176
            $this->dumper->dump($this->formatter->getSitemapStart());
177
        }
178
    }
179
180
    protected function getCurrentSitemapIndex()
181
    {
182
        return end($this->sitemapIndexes);
183
    }
184
185
    protected function getSitemapIndexFilename($filename)
186
    {
187
        $sitemapIndexFilename = basename($filename);
188
        $index = count($this->sitemapIndexes) + 1;
189
        $extPosition = strrpos($sitemapIndexFilename, ".");
190
        if ($extPosition !== false) {
191
            $sitemapIndexFilename = substr($sitemapIndexFilename, 0, $extPosition).'-'.$index.substr($sitemapIndexFilename, $extPosition);
192
        } else {
193
            $sitemapIndexFilename .= '-'.$index;
194
        }
195
196
        $sitemapIndexFilename = dirname($filename).DIRECTORY_SEPARATOR.$sitemapIndexFilename;
197
198
        return $sitemapIndexFilename;
199
    }
200
}