Passed
Push — master ( 99cab5...2782fc )
by Caen
04:06 queued 13s
created

BuildService::getClassPluralName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Services;
6
7
use Hyde\Facades\Site;
8
use Hyde\Foundation\Kernel\RouteCollection;
9
use Hyde\Framework\Actions\StaticPageBuilder;
10
use Hyde\Framework\Concerns\InteractsWithDirectories;
11
use Hyde\Hyde;
12
use Hyde\Pages\Concerns\HydePage;
13
use Hyde\Support\Models\Route;
14
use Illuminate\Console\Concerns\InteractsWithIO;
15
use Illuminate\Console\OutputStyle;
16
use Illuminate\Support\Str;
17
use function collect;
18
19
/**
20
 * Moves logic from the build command to a service.
21
 *
22
 * Handles the build loop which generates the static site.
23
 *
24
 * @see \Hyde\Console\Commands\BuildSiteCommand
25
 * @see \Hyde\Framework\Testing\Feature\StaticSiteServiceTest
26
 */
27
class BuildService
28
{
29
    use InteractsWithIO;
30
    use InteractsWithDirectories;
31
32
    protected RouteCollection $router;
33
34
    public function __construct(OutputStyle $output)
35
    {
36
        $this->output = $output;
37
38
        $this->router = Hyde::routes();
39
    }
40
41
    public function compileStaticPages(): void
42
    {
43
        collect($this->getPageTypes())->each(function (string $pageClass): void {
0 ignored issues
show
Bug introduced by
$this->getPageTypes() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        collect(/** @scrutinizer ignore-type */ $this->getPageTypes())->each(function (string $pageClass): void {
Loading history...
44
            $this->compilePagesForClass($pageClass);
45
        });
46
    }
47
48
    public function transferMediaAssets(): void
49
    {
50
        $this->needsDirectory(Hyde::siteMediaPath());
51
52
        $this->comment('Transferring Media Assets...');
53
        $this->withProgressBar(DiscoveryService::getMediaAssetFiles(), function (string $filepath): void {
54
            $sitePath = Hyde::siteMediaPath(Str::after($filepath, Hyde::mediaPath()));
55
            $this->needsParentDirectory($sitePath);
56
            copy($filepath, $sitePath);
57
        });
58
59
        $this->newLine(2);
60
    }
61
62
    /**
63
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $pageClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
64
     */
65
    protected function compilePagesForClass(string $pageClass): void
66
    {
67
        $this->comment("Creating {$this->getClassPluralName($pageClass)}...");
68
69
        $collection = $this->router->getRoutes($pageClass);
70
71
        $this->withProgressBar($collection, function (Route $route): void {
72
            (new StaticPageBuilder($route->getPage()))->__invoke();
73
        });
74
75
        $this->newLine(2);
76
    }
77
78
    protected function getClassPluralName(string $pageClass): string
79
    {
80
        return preg_replace('/([a-z])([A-Z])/', '$1 $2', class_basename($pageClass)).'s';
81
    }
82
83
    /** @return array<class-string<\Hyde\Pages\Concerns\HydePage>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<\Hyde...ges\Concerns\HydePage>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<\Hyde\Pages\Concerns\HydePage>>.
Loading history...
84
    protected function getPageTypes(): array
85
    {
86
        return Hyde::pages()->map(function (HydePage $page): string {
87
            return $page::class;
88
        })->unique()->values()->toArray();
89
    }
90
}
91