Passed
Push — master ( 788a27...9cd729 )
by Caen
03:03 queued 12s
created

GenerateSearch::then()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Actions\PostBuildTasks;
4
5
use Hyde\Framework\Concerns\InteractsWithDirectories;
6
use Hyde\Framework\Contracts\AbstractBuildTask;
7
use Hyde\Framework\Services\DiscoveryService;
8
use Hyde\Framework\Services\DocumentationSearchService;
9
10
class GenerateSearch extends AbstractBuildTask
11
{
12
    use InteractsWithDirectories;
13
14
    public static string $description = 'Generating search index';
15
16
    public function run(): void
17
    {
18
        $expected = $this->guesstimateGenerationTime();
19
        if ($expected >= 1) {
20
            $this->line("<fg=gray>This will take an estimated $expected seconds. Terminal may seem non-responsive.</>");
21
        }
22
23
        DocumentationSearchService::generate();
24
25
        if (config('docs.create_search_page', true)) {
26
            $directory = DocumentationSearchService::generateSearchPage();
27
28
            $this->createdSiteFile("$directory/search.html");
29
        }
30
    }
31
32
    public function then(): void
33
    {
34
        $this->writeln(sprintf("\n > Created <info>%s</info> in %s",
35
            $this->normalizePath(DocumentationSearchService::$filePath),
36
            $this->getExecutionTime()
37
        ));
38
    }
39
40
    protected function normalizePath(string $path): string
41
    {
42
        return str_replace('\\', '/', $path);
43
    }
44
45
    /** @internal Estimated processing time per file in ms */
46
    public static float $guesstimationFactor = 52.5;
47
48
    protected function guesstimateGenerationTime(): int|float
49
    {
50
        return (int) round(count(DiscoveryService::getDocumentationPageFiles()) * static::$guesstimationFactor) / 1000;
51
    }
52
}
53