Completed
Push — master ( a1e512...b9cad8 )
by Tom
04:27
created

SitemapFactory::makeFromSheetList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Astrotomic\Stancy\Factories;
4
5
use Astrotomic\Stancy\Contracts\Page as PageContract;
6
use Astrotomic\Stancy\Contracts\PageFactory as PageFactoryContract;
7
use Astrotomic\Stancy\Contracts\SitemapFactory as SitemapFactoryContract;
8
use Illuminate\Support\Str;
9
use Spatie\Sheets\Facades\Sheets;
10
use Spatie\Sheets\Sheet;
11
use Spatie\Sitemap\Sitemap;
12
13
class SitemapFactory implements SitemapFactoryContract
14
{
15
    /** @var PageFactoryContract */
16
    protected $pageFactory;
17
18 4
    public function __construct(PageFactoryContract $pageFactory)
19
    {
20 4
        $this->pageFactory = $pageFactory;
21 4
    }
22
23
    /**
24
     * @param PageContract[] $pages
25
     *
26
     * @return Sitemap
27
     */
28 2
    public function makeFromPages(array $pages): Sitemap
29
    {
30 2
        $sitemap = Sitemap::create();
31
32 2
        foreach($pages as $page) {
0 ignored issues
show
introduced by
You should change the following
<fg=red>- foreach($pages as $page) {
</><fg=green>+ foreach ($pages as $page) {
</>
Loading history...
33 2
            $sitemap->add($page->toSitemapItem());
34
        }
35
36 2
        return $sitemap;
37
    }
38
39
    /**
40
     * @param string[] $list
41
     *
42
     * @return Sitemap
43
     */
44 2
    public function makeFromSheetList(array $list): Sitemap
45
    {
46 2
        $pages = [];
47
48 2
        foreach($list as $entry) {
0 ignored issues
show
introduced by
You should change the following
<fg=red>- foreach($list as $entry) {
</><fg=green>+ foreach ($list as $entry) {
</>
Loading history...
49 2
            if (Str::contains($entry, ':')) {
50 1
                [$collection, $path] = explode(':', $entry);
51
52 1
                $pages[] = $this->sheetToPage(Sheets::collection($collection)->get($path));
0 ignored issues
show
Bug introduced by
It seems like Spatie\Sheets\Facades\Sh...collection)->get($path) can also be of type null; however, parameter $sheet of Astrotomic\Stancy\Factor...pFactory::sheetToPage() does only seem to accept Spatie\Sheets\Sheet, 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

52
                $pages[] = $this->sheetToPage(/** @scrutinizer ignore-type */ Sheets::collection($collection)->get($path));
Loading history...
53
54 1
                continue;
55
            }
56
57 1
            $pages = array_merge($pages, $this->sheetsToPages(Sheets::collection($entry)->all()->all()));
58
        }
59
60 2
        return $this->makeFromPages($pages);
61
    }
62
63 1
    public function makeFromSheetCollectionName(string $name): Sitemap
64
    {
65 1
        return $this->makeFromSheetList([$name]);
66
    }
67
68
    /**
69
     * @param Sheet[] $sheets
70
     *
71
     * @return PageContract[]
72
     */
73 1
    protected function sheetsToPages(array $sheets): array
74
    {
75
        return array_map(function(Sheet $sheet): PageContract {
0 ignored issues
show
introduced by
You should change the following
<fg=red>- return array_map(function(Sheet $sheet): PageContract {
</><fg=green>+ return array_map(function (Sheet $sheet): PageContract {
</>
Loading history...
76 1
            return $this->sheetToPage($sheet);
77 1
        }, $sheets);
78
    }
79
80 2
    protected function sheetToPage(Sheet $sheet): PageContract
81
    {
82 2
        return $this->pageFactory->makeFromSheet($sheet);
83
    }
84
}
85