Passed
Push — master ( 0f8d9d...85764f )
by Caen
03:45 queued 15s
created

DocumentationSearchService::getFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Services;
6
7
use Hyde\Framework\Actions\ConvertsMarkdownToPlainText;
8
use Hyde\Framework\Concerns\InteractsWithDirectories;
9
use Hyde\Hyde;
10
use Hyde\Pages\DocumentationPage;
11
use Illuminate\Support\Collection;
12
13
/**
14
 * @internal Generate a JSON file that can be used as a search index for documentation pages.
15
 *
16
 * @see \Hyde\Framework\Testing\Feature\Services\DocumentationSearchServiceTest
17
 */
18
final class DocumentationSearchService
19
{
20
    use InteractsWithDirectories;
21
22
    public Collection $searchIndex;
23
    protected string $filePath;
24
25
    public static function generate(): self
26
    {
27
        return (new self())->execute();
28
    }
29
30
    public function __construct()
31
    {
32
        $this->searchIndex = new Collection();
33
        $this->filePath = $this->getFilePath();
34
    }
35
36
    public function execute(): self
37
    {
38
        return $this->run()->save();
39
    }
40
41
    public function run(): self
42
    {
43
        /** @var \Hyde\Pages\DocumentationPage $page */
44
        foreach (DocumentationPage::all() as $page) {
45
            if (! in_array($page->identifier, config('docs.exclude_from_search', []))) {
46
                $this->searchIndex->push(
47
                    $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
                    /** @scrutinizer ignore-type */ $this->generatePageEntry($page)
Loading history...
48
                );
49
            }
50
        }
51
52
        return $this;
53
    }
54
55
    /**
56
     * @return array{slug: string, title: string, content: string, destination: string}
57
     */
58
    public function generatePageEntry(DocumentationPage $page): array
59
    {
60
        return [
61
            'slug' => basename($page->identifier),
62
            'title' => $page->title,
63
            'content' => trim($this->getSearchContentForDocument($page)),
64
            'destination' => $this->formatDestination(basename($page->identifier)),
65
        ];
66
    }
67
68
    protected function save(): self
69
    {
70
        $this->needsParentDirectory(Hyde::path($this->filePath));
71
72
        file_put_contents(Hyde::path($this->filePath), $this->searchIndex->toJson());
73
74
        return $this;
75
    }
76
77
    protected function getSearchContentForDocument(DocumentationPage $page): string
78
    {
79
        return (new ConvertsMarkdownToPlainText($page->markdown->body()))->execute();
80
    }
81
82
    protected function formatDestination(string $slug): string
83
    {
84
        if (config('site.pretty_urls', false) === true) {
85
            return $slug === 'index' ? '' : $slug;
86
        }
87
88
        return "$slug.html";
89
    }
90
91
    public static function getFilePath(): string
92
    {
93
        return Hyde::pathToRelative(Hyde::sitePath(
94
            DocumentationPage::outputDirectory().'/search.json'
95
        ));
96
    }
97
}
98