GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SitemapCommand::replaceIdWithFilename()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace AloiaCms\Console;
4
5
use AloiaCms\Models\Contracts\ModelInterface;
6
use AloiaCms\Models\Traits\Postable;
7
use AloiaCms\Models\Traits\Updatable;
8
use Illuminate\Console\Command;
9
use Illuminate\Support\Facades\Config;
10
use SitemapGenerator\SitemapGenerator;
11
12
class SitemapCommand extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'aloiacms:sitemap';
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Generate a sitemap for the specified models';
26
27
    /**
28
     * Execute the console command.
29
     *
30
     * @return void
31
     */
32
    public function handle()
33
    {
34
        $models = Config::get('aloiacms.seo.sitemap', []);
35
36
        $app_url = Config::get('app.url', 'http://locahost:8000');
37
38
        $sitemap_path = Config::get('aloiacms.seo.sitemap_path');
39
40
        $generator = new SitemapGenerator($app_url);
41
42
        foreach ($models as $config) {
43
            // Determine whether we can use the built-in traits for fetching a "last modified at" date.
44
            $traits = class_uses_recursive($config['model']);
45
            $has_update_date = in_array(Updatable::class, $traits);
46
            $has_post_date = in_array(Postable::class, $traits);
47
48
            // Call the all() method to fetch all models of this type
49
            foreach (call_user_func([$config['model'], 'all']) as $model) {
50
                $generator->add(
51
                    $this->replaceIdWithFilename($model, $config['path']),
52
                    $config['priority'],
53
                    $this->getLastModifiedAt($has_update_date, $has_post_date, $model),
54
                    $config['change_frequency'],
55
                );
56
            }
57
        }
58
59
        file_put_contents($sitemap_path, $generator->toXML());
60
61
        $relative_path = str_replace(base_path() . "/", "", $sitemap_path);
62
63
        $this->info("Create a sitemap at: {$relative_path}");
64
    }
65
66
    /**
67
     *  Get the last modification date, defaults to today's date.
68
     *
69
     * @param bool $has_update_date
70
     * @param bool $has_post_date
71
     * @param mixed $model
72
     * @return string
73
     */
74
    private function getLastModifiedAt(bool $has_update_date, bool $has_post_date, mixed $model): string
75
    {
76
        $last_modified_at = "";
77
78
        if ($has_update_date && !is_null($model->getUpdateDate())) {
79
            $last_modified_at = $model->getUpdateDate()->format('Y-m-d');
80
        }
81
82
        if ($has_post_date && empty($last_modified_at) && !is_null($model->getPostDate())) {
83
            $last_modified_at = $model->getPostDate()->format('Y-m-d');
84
        }
85
86
        if (!empty($last_modified_at)) {
87
            return $last_modified_at;
88
        }
89
90
        return date('Y-m-d');
91
    }
92
93
    /**
94
     * Generate the URL for the given model and path
95
     *
96
     * @param ModelInterface $model
97
     * @param string $path
98
     * @return string
99
     */
100
    protected function replaceIdWithFilename(ModelInterface $model, string $path): string
101
    {
102
        return str_replace("{id}", $model->filename(), $path);
103
    }
104
}
105