Passed
Push — master ( 32a4a2...da9bd5 )
by Caen
04:12 queued 14s
created

GeneratesDocumentationSearchIndex::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Actions;
6
7
use Hyde\Hyde;
8
use Hyde\Facades\Config;
9
use Hyde\Facades\Filesystem;
10
use Hyde\Framework\Concerns\InteractsWithDirectories;
11
use Hyde\Pages\DocumentationPage;
12
use Illuminate\Support\Collection;
13
use function basename;
14
use function in_array;
15
use function trim;
16
17
/**
18
 * @internal Generate a JSON file that can be used as a search index for documentation pages.
19
 *
20
 * @see \Hyde\Framework\Testing\Feature\Services\DocumentationSearchServiceTest
21
 */
22
class GeneratesDocumentationSearchIndex
23
{
24
    use InteractsWithDirectories;
25
26
    protected Collection $index;
27
    protected string $path;
28
29
    /**
30
     * Generate the search index and save it to disk.
31
     *
32
     * @return string The path to the generated file.
33
     */
34
    public static function handle(): string
35
    {
36
        $service = new static();
37
        $service->run();
38
        $service->save();
39
40
        return $service->path;
41
    }
42
43
    protected function __construct()
44
    {
45
        $this->index = new Collection();
46
        $this->path = $this->getPath();
47
    }
48
49
    protected function run(): void
50
    {
51
        DocumentationPage::all()->each(function (DocumentationPage $page): void {
52
            if (! in_array($page->identifier, Config::getArray('docs.exclude_from_search', []))) {
53
                $this->index->push($this->generatePageEntry($page));
0 ignored issues
show
Bug introduced by
$this->generatePageEntry($page) of type array<string,string> is incompatible with the type Illuminate\Support\TValue expected by parameter $values of Illuminate\Support\Collection::push(). ( Ignorable by Annotation )

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

53
                $this->index->push(/** @scrutinizer ignore-type */ $this->generatePageEntry($page));
Loading history...
54
            }
55
        });
56
    }
57
58
    /**
59
     * @return array{slug: string, title: string, content: string, destination: string}
60
     */
61
    protected function generatePageEntry(DocumentationPage $page): array
62
    {
63
        return [
64
            'slug' => basename($page->identifier),
65
            'title' => $page->title,
66
            'content' => trim($this->getSearchContentForDocument($page)),
67
            'destination' => $this->formatDestination(basename($page->identifier)),
68
        ];
69
    }
70
71
    protected function save(): void
72
    {
73
        $this->needsParentDirectory($this->path);
74
75
        Filesystem::putContents($this->path, $this->index->toJson());
76
    }
77
78
    protected function getSearchContentForDocument(DocumentationPage $page): string
79
    {
80
        return (new ConvertsMarkdownToPlainText($page->markdown->body()))->execute();
81
    }
82
83
    protected function formatDestination(string $slug): string
84
    {
85
        if (Config::getBool('hyde.pretty_urls', false) === true) {
86
            return $slug === 'index' ? '' : $slug;
87
        }
88
89
        return "$slug.html";
90
    }
91
92
    protected function getPath(): string
93
    {
94
        return Hyde::sitePath(DocumentationPage::outputDirectory().'/search.json');
95
    }
96
}
97