Passed
Push — master ( ad6b2a...b41ca0 )
by Caen
07:45 queued 14s
created

generatePageEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
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\Facades\Config;
8
use Hyde\Framework\Concerns\InteractsWithDirectories;
9
use Hyde\Pages\DocumentationPage;
10
use Illuminate\Support\Collection;
11
12
use function basename;
13
use function in_array;
14
use function trim;
15
16
/**
17
 * @internal Generate a JSON string that can be used as a search index for documentation pages.
18
 */
19
class GeneratesDocumentationSearchIndex
20
{
21
    use InteractsWithDirectories;
22
23
    protected Collection $index;
24
25
    /**
26
     * @since v2.x This method returns the JSON string instead of saving it to disk and returning the path.
27
     *
28
     * @return string The path to the generated file.
29
     */
30
    public static function handle(): string
31
    {
32
        $service = new static();
33
        $service->run();
34
35
        return $service->index->toJson();
36
    }
37
38
    protected function __construct()
39
    {
40
        $this->index = new Collection();
41
    }
42
43
    protected function run(): void
44
    {
45
        DocumentationPage::all()->each(function (DocumentationPage $page): void {
46
            if (! in_array($page->identifier, $this->getPagesToExcludeFromSearch())) {
47
                $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

47
                $this->index->push(/** @scrutinizer ignore-type */ $this->generatePageEntry($page));
Loading history...
48
            }
49
        });
50
    }
51
52
    /**
53
     * @return array{slug: string, title: string, content: string, destination: string}
54
     */
55
    protected function generatePageEntry(DocumentationPage $page): array
56
    {
57
        return [
58
            'slug' => basename($page->identifier),
59
            'title' => $page->title,
60
            'content' => trim($this->getSearchContentForDocument($page)),
61
            'destination' => $this->formatDestination(basename($page->identifier)),
62
        ];
63
    }
64
65
    protected function getSearchContentForDocument(DocumentationPage $page): string
66
    {
67
        return (new ConvertsMarkdownToPlainText($page->markdown->body()))->execute();
68
    }
69
70
    protected function formatDestination(string $slug): string
71
    {
72
        if (Config::getBool('hyde.pretty_urls', false) === true) {
73
            return $slug === 'index' ? '' : $slug;
74
        }
75
76
        return "$slug.html";
77
    }
78
79
    protected function getPagesToExcludeFromSearch(): array
80
    {
81
        return array_merge(Config::getArray('docs.exclude_from_search', []),
82
            Config::getBool('docs.create_search_page', true) ? ['search'] : []
83
        );
84
    }
85
}
86