Passed
Push — master ( af63b7...70ccfd )
by Níckolas Daniel
04:46
created

JigsawAdapter::fromCollection()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8.0093

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 38
ccs 18
cts 19
cp 0.9474
rs 8.4444
cc 8
nc 1
nop 1
crap 8.0093
1
<?php
2
3
namespace PODEntender\SitemapGenerator\Adapter\Jigsaw;
4
5
use PODEntender\SitemapGenerator\Factory\SitemapXmlFactory;
6
use PODEntender\SitemapGenerator\Factory\UrlSetFactory;
7
8
use Illuminate\Support\Collection;
9
use TightenCo\Jigsaw\PageVariable;
10
use DOMDocument;
11
12
class JigsawAdapter
13
{
14
    private $sitemapXmlFactory;
15
16
    private $urlSetFactory;
17
18 1
    public function __construct(SitemapXmlFactory $sitemapXmlFactory, UrlSetFactory $urlSetFactory)
19
    {
20 1
        $this->sitemapXmlFactory = $sitemapXmlFactory;
21 1
        $this->urlSetFactory = $urlSetFactory;
22 1
    }
23
24 1
    public function fromCollection(Collection $pages): DOMDocument
25
    {
26
        $multiDimensionalArray = $pages
27
            ->map(function (PageVariable $page) {
28 1
                $sitemapUrl = $page->getUrl();
29
30 1
                $parsedUrl = parse_url($sitemapUrl);
31
32
                // If index page, don't append trailing forward slash
33 1
                if (false === isset($parsedUrl['path'])) {
34
                    $sitemapUrl = rtrim($sitemapUrl, '/');
35
                } else {
36 1
                    $pathInfo = pathinfo($parsedUrl['path']);
37
38
                    // If .html, send it right away. If path-like, make sure last character is a forward slash
39 1
                    $sitemapUrl = isset($pathInfo['extension']) ? $sitemapUrl : rtrim($sitemapUrl, '/') . '/';
40
                }
41
42 1
                $output = ['location' => $sitemapUrl];
43
44 1
                if ($page->date) {
45 1
                    $output['lastModified'] = date_create_immutable(date('Y-m-d', $page->date));
0 ignored issues
show
Bug introduced by
It seems like $page->date can also be of type Illuminate\Support\HigherOrderCollectionProxy; however, parameter $timestamp of date() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

45
                    $output['lastModified'] = date_create_immutable(date('Y-m-d', /** @scrutinizer ignore-type */ $page->date));
Loading history...
46
                }
47
48 1
                if ($page->get('sitemap') && isset($page->sitemap['changeFrequency'])) {
49 1
                    $output['changeFrequency'] = $page->sitemap['changeFrequency'];
50
                }
51
52 1
                if ($page->get('sitemap') && isset($page->sitemap['priority'])) {
53 1
                    $output['priority'] = $page->sitemap['priority'];
54
                }
55
56 1
                return $output;
57 1
            })
58 1
            ->toArray();
59
60 1
        return $this->sitemapXmlFactory->createFromUrlSet(
61 1
            $this->urlSetFactory->createFromMultiDimensionalArray($multiDimensionalArray)
62
        );
63
    }
64
}
65