BuildService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A compileStaticPages() 0 4 1
A __construct() 0 5 1
A getClassPluralName() 0 7 2
A getPageTypes() 0 9 2
A compilePagesForClass() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Services;
6
7
use Hyde\Hyde;
8
use Hyde\Pages\InMemoryPage;
9
use Hyde\Foundation\Facades\Routes;
10
use Hyde\Foundation\Kernel\RouteCollection;
11
use Hyde\Framework\Actions\StaticPageBuilder;
12
use Hyde\Pages\Concerns\HydePage;
13
use Hyde\Support\Models\Route;
14
use Illuminate\Console\Concerns\InteractsWithIO;
15
use Illuminate\Console\OutputStyle;
16
17
use function class_basename;
18
use function preg_replace;
19
use function collect;
20
21
/**
22
 * Moves logic from the build command to a service.
23
 *
24
 * Handles the build loop which generates the static site.
25
 *
26
 * @see \Hyde\Console\Commands\BuildSiteCommand
27
 */
28
class BuildService
29
{
30
    use InteractsWithIO;
31
32
    protected RouteCollection $router;
33
34
    public function __construct(OutputStyle $output)
35
    {
36
        $this->output = $output;
37
38
        $this->router = Hyde::routes();
0 ignored issues
show
Bug introduced by
The method routes() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

38
        /** @scrutinizer ignore-call */ 
39
        $this->router = Hyde::routes();
Loading history...
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
    /**
49
     * @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...
50
     */
51
    protected function compilePagesForClass(string $pageClass): void
52
    {
53
        $this->comment("Creating {$this->getClassPluralName($pageClass)}...");
54
55
        $collection = Routes::getRoutes($pageClass);
0 ignored issues
show
Bug introduced by
The method getRoutes() does not exist on Hyde\Foundation\Facades\Routes. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

55
        /** @scrutinizer ignore-call */ 
56
        $collection = Routes::getRoutes($pageClass);
Loading history...
56
57
        $this->withProgressBar($collection, function (Route $route): void {
58
            StaticPageBuilder::handle($route->getPage());
59
        });
60
61
        $this->newLine(2);
62
    }
63
64
    protected function getClassPluralName(string $pageClass): string
65
    {
66
        if ($pageClass === InMemoryPage::class) {
67
            return 'Dynamic Pages';
68
        }
69
70
        return preg_replace('/([a-z])([A-Z])/', '$1 $2', class_basename($pageClass)).'s';
71
    }
72
73
    /** @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...
74
    protected function getPageTypes(): array
75
    {
76
        return Hyde::pages()->map(function (HydePage $page): string {
0 ignored issues
show
Bug introduced by
The method pages() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

76
        return Hyde::/** @scrutinizer ignore-call */ pages()->map(function (HydePage $page): string {
Loading history...
77
            if ($page instanceof InMemoryPage) {
78
                return InMemoryPage::class;
79
            }
80
81
            return $page::class;
82
        })->unique()->values()->toArray();
83
    }
84
}
85