Passed
Push — master ( d94c8e...28d464 )
by Caen
02:56 queued 12s
created

HydeBuildSearchCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 20
c 2
b 1
f 0
dl 0
loc 54
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A guesstimateGenerationTime() 0 3 1
A handle() 0 25 3
1
<?php
2
3
namespace Hyde\Framework\Commands;
4
5
use Hyde\Framework\Actions\GeneratesDocumentationSearchIndexFile;
6
use Hyde\Framework\Concerns\ActionCommand;
7
use Hyde\Framework\Hyde;
8
use Hyde\Framework\Services\DiscoveryService;
9
10
/**
11
 * Hyde Command to run the Build Process for the DocumentationSearchIndex.
12
 *
13
 * @todo Add configuration option to enable/disable this feature.
14
 *
15
 * @see \Hyde\Framework\Testing\Feature\Commands\HydeBuildSearchCommandTest
16
 */
17
class HydeBuildSearchCommand extends ActionCommand
18
{
19
    /**
20
     * The signature of the command.
21
     *
22
     * @var string
23
     */
24
    protected $signature = 'build:search';
25
26
    /**
27
     * The description of the command.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Generate the docs/search.json';
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @return int
37
     */
38
    public function handle(): int
39
    {
40
        $this->action('Generating documentation site search index', function () {
41
            $expected = $this->guesstimateGenerationTime();
42
            if ($expected > 0) {
43
                $this->line("<fg=gray> > This will take an estimated $expected seconds. Terminal may seem non-responsive.</>");
44
            }
45
46
            GeneratesDocumentationSearchIndexFile::run();
47
        }, sprintf('Created <info>%s</info>', GeneratesDocumentationSearchIndexFile::$filePath));
48
49
        if (config('docs.create_search_page', true)) {
50
            $this->action('Generating search page', function () {
51
                file_put_contents(
52
                    Hyde::path(sprintf('_site/%s/search.html',
53
                        config('docs.output_directory', 'docs')
54
                    )),
55
                    view('hyde::pages.documentation-search')->render()
56
                );
57
            }, sprintf('Created <info>_site/%s/search.html</info>',
58
                config('docs.output_directory', 'docs')
59
            ));
60
        }
61
62
        return 0;
63
    }
64
65
    /** @internal Estimated processing time per file in ms */
66
    public static float $guesstimationFactor = 52.5;
67
68
    protected function guesstimateGenerationTime(): int|float
69
    {
70
        return (int) round(count(DiscoveryService::getDocumentationPageFiles()) * static::$guesstimationFactor) / 1000;
71
    }
72
}
73