Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
HydeBuildSitemapCommand
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 runPreflightCheck
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getExecutionTimeInMs
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Hyde\Framework\Commands;
4
5use Hyde\Framework\Hyde;
6use Hyde\Framework\Services\SitemapService;
7use LaravelZero\Framework\Commands\Command;
8
9/**
10 * Hyde Command to run the Build Process for the Sitemap.
11 *
12 * @see \Tests\Feature\Commands\HydeBuildSitemapCommandTest
13 */
14class HydeBuildSitemapCommand extends Command
15{
16    /**
17     * The signature of the command.
18     *
19     * @var string
20     */
21    protected $signature = 'build:sitemap';
22
23    /**
24     * The description of the command.
25     *
26     * @var string
27     */
28    protected $description = 'Generate the sitemap.xml';
29
30    /**
31     * Execute the console command.
32     *
33     * @return int
34     */
35    public function handle(): int
36    {
37        $actionTime = microtime(true);
38
39        if (! $this->runPreflightCheck()) {
40            return 1;
41        }
42
43        $this->comment('Generating sitemap...');
44        file_put_contents(Hyde::getSiteOutputPath('sitemap.xml'), SitemapService::generateSitemap());
45        $this->line(' > Created <info>sitemap.xml</> in '.$this->getExecutionTimeInMs($actionTime)."ms\n");
46
47        return 0;
48    }
49
50    protected function runPreflightCheck(): bool
51    {
52        if (! SitemapService::canGenerateSitemap()) {
53            $this->error('Cannot generate sitemap.xml, please check your configuration.');
54
55            return false;
56        }
57
58        return true;
59    }
60
61    protected function getExecutionTimeInMs(float $timeStart): string
62    {
63        return number_format(((microtime(true) - $timeStart) * 1000), 2);
64    }
65}