JigsawAdapter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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 3
    public function __construct(SitemapXmlFactory $sitemapXmlFactory, UrlSetFactory $urlSetFactory)
19
    {
20 3
        $this->sitemapXmlFactory = $sitemapXmlFactory;
21 3
        $this->urlSetFactory = $urlSetFactory;
22 3
    }
23
24 3
    public function fromCollection(Collection $pages): DOMDocument
25
    {
26
        $multiDimensionalArray = $pages
27
            ->map(function (PageVariable $page) {
28 3
                $date = $this->fetchDataFromPage('lastModified', $page);
29
30 3
                if (is_string($date)) {
31
                    $date = date_create_immutable(date('Y-m-d', $date));
0 ignored issues
show
Bug introduced by
$date of type string is incompatible with the type integer expected by parameter $timestamp of date(). ( Ignorable by Annotation )

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

31
                    $date = date_create_immutable(date('Y-m-d', /** @scrutinizer ignore-type */ $date));
Loading history...
32
                }
33
34
                // Fallback to default post date metadata
35 3
                if ($date === null && $page->date) {
36 2
                    $date = 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

36
                    $date = date_create_immutable(date('Y-m-d', /** @scrutinizer ignore-type */ $page->date));
Loading history...
37
                }
38
39
                return [
40 3
                    'location' => $this->fetchDataFromPage('location', $page) ?? $page->getUrl(),
41 3
                    'lastModified' => $date,
42 3
                    'changeFrequency' => $this->fetchDataFromPage('changeFrequency', $page),
43 3
                    'priority' => $this->fetchDataFromPage('priority', $page),
44
                ];
45 3
            })
46 3
            ->toArray();
47
48 3
        return $this->sitemapXmlFactory->createFromUrlSet(
49 3
            $this->urlSetFactory->createFromMultiDimensionalArray($multiDimensionalArray)
50
        );
51
    }
52
53 3
    private function fetchDataFromPage(string $identifier, PageVariable $page)
54
    {
55
        // Search for sitemap item in page property/config
56 3
        if ($page->sitemap === null || isset($page->sitemap[$identifier]) === false) {
57 2
            return null;
58
        }
59
60 3
        if (is_callable($page->sitemap[$identifier])) {
61 1
            return call_user_func($page->sitemap[$identifier], $page);
62
        }
63
64 2
        return (string) $page->sitemap[$identifier];
65
    }
66
}
67