GenerateSitemap::createAlternateLocales()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Console;
4
5
use Thinktomorrow\Chief\Site\Sitemap\SitemapXmlFile;
6
7
class GenerateSitemap extends BaseCommand
8
{
9
    protected $signature = 'chief:sitemap';
10
    protected $description = 'Generate a sitemap for all locales. Only online and visitable urls are included.';
11
    /**
12
     * @var SitemapXmlFile
13
     */
14
    private $sitemapXmlFile;
15
16
    public function __construct(SitemapXmlFile $sitemapXmlFile)
17
    {
18
        parent::__construct();
19
20
        $this->sitemapXmlFile = $sitemapXmlFile;
21
    }
22
23
    public function handle(): void
24
    {
25
        $locales = config('chief.locales');
26
27
        foreach ($locales as $locale) {
28
            $filepath = public_path('sitemap-' . $locale . '.xml');
29
30
            $this->info('Generating a sitemap for locale: ' . $locale . ' at: ' . $filepath);
31
32
            $this->sitemapXmlFile->create($locale, $filepath, $this->createAlternateLocales($locales, $locale));
33
        }
34
35
        $this->info('Done generating sitemaps.');
36
    }
37
38
    /**
39
     * @param array $locales
40
     * @param $locale
41
     * @return array
42
     */
43
    protected function createAlternateLocales(array $locales, $locale): array
44
    {
45
        if (($key = array_search($locale, $locales)) !== false) {
46
            unset($locales[$key]);
47
        }
48
49
        return $locales;
50
    }
51
}
52