Passed
Push — master ( 1afa4d...c3dc3d )
by Caen
02:58 queued 13s
created

GenerateSearch::guesstimateGenerationTime()   A

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