Sitemap::parseImage()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 5
nop 1
dl 0
loc 10
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::getSchema($request).$request->server->get('HTTP_HOST');
35
        }
36
        $this->limit = $limit;
37
        if ($this->isSitemapIndexable() && $dumper instanceof DumperFileInterface) {
38
            $this->originalFilename = $dumper->getFilename();
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) {
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
    private function addIndex()
134
    {
135
        if ($this->isSitemapIndexable() && $this->getCurrentSitemapIndex()->getUrlCount() >= $this->limit) {
136
            $this->addSitemapIndex($this->createSitemapIndex());
137
        }
138
    }
139
140
    private function parseBaseHost(Url $url, $loc)
141
    {
142
        if ($this->baseHost !== null) {
143
            if ($this->needHost($loc)) {
144
                $url->setLoc($this->baseHost.$loc);
145
            }
146
147
            $this->parseVideo($url);
148
            $this->parseImage($url);
149
        }
150
    }
151
152
    private function setEndIndex()
153
    {
154
        if ($this->isSitemapIndexable()) {
155
            $this->getCurrentSitemapIndex()->incrementUrl();
156
        }
157
    }
158
159
    public function add(Url $url)
160
    {
161
        $this->addIndex();
162
163
        $loc = $url->getLoc();
164
        if (empty($loc)) {
165
            throw new \InvalidArgumentException('The url MUST have a loc attribute');
166
        }
167
168
        $this->parseBaseHost($url, $loc);
169
        $this->dumper->dump($this->formatter->formatUrl($url));
170
        $this->setEndIndex();
171
172
        return $this;
173
    }
174
175
    protected function parseVideo(Url $url)
176
    {
177
        foreach ($url->getVideos() as $video) {
178
            /** @var Video $video */
179
            if ($this->needHost($video->getThumbnailLoc())) {
180
                $video->setThumbnailLoc($this->baseHost.$video->getThumbnailLoc());
181
            }
182
183
            if ($this->needHost($video->getContentLoc())) {
184
                $video->setContentLoc($this->baseHost.$video->getContentLoc());
185
            }
186
187
            $player = $video->getPlayerLoc();
188
            if ($player !== null && $this->needHost($player['loc'])) {
189
                $video->setPlayerLoc($this->baseHost.$player['loc'], $player['allow_embed'], $player['autoplay']);
190
            }
191
192
            $gallery = $video->getGalleryLoc();
193
            if ($gallery !== null && $this->needHost($gallery['loc'])) {
194
                $video->setGalleryLoc($this->baseHost.$gallery['loc'], $gallery['title']);
195
            }
196
        }
197
    }
198
199
    protected function parseImage(Url $url)
200
    {
201
        foreach ($url->getImages() as $image) {
202
            /** @var Image $image */
203
            if ($this->needHost($image->getLoc())) {
204
                $image->setLoc($this->baseHost.$image->getLoc());
205
            }
206
207
            if ($this->needHost($image->getLicense())) {
208
                $image->setLicense($this->baseHost.$image->getLicense());
209
            }
210
        }
211
    }
212
213
    protected function getCurrentSitemapIndex()
214
    {
215
        return end($this->sitemapIndexes);
216
    }
217
218
    protected function needHost($url)
219
    {
220
        if ($url === null) {
221
            return false;
222
        }
223
224
        return 0 !== strpos($url, 'http');
225
    }
226
}
227