Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
PublicationPageCompiler
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
7 / 7
9
100.00% covered (success)
100.00%
1 / 1
 call
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 compilePublicationPage
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 compilePublicationListPage
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 compileView
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getTemplateFilePath
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Publications\Actions;
6
7use Hyde\Framework\Actions\AnonymousViewCompiler;
8use Hyde\Publications\Pages\PublicationListPage;
9use Hyde\Publications\Pages\PublicationPage;
10use Illuminate\Support\Facades\View;
11
12use function basename;
13use function str_ends_with;
14
15/**
16 * @see \Hyde\Publications\Testing\Feature\PublicationPageCompilerTest
17 */
18class PublicationPageCompiler
19{
20    protected PublicationPage|PublicationListPage $page;
21
22    public static function call(PublicationPage|PublicationListPage $page): string
23    {
24        return (new self($page))->__invoke();
25    }
26
27    public function __construct(PublicationPage|PublicationListPage $page)
28    {
29        $this->page = $page;
30    }
31
32    public function __invoke(): string
33    {
34        return $this->page instanceof PublicationPage
35            ? $this->compilePublicationPage()
36            : $this->compilePublicationListPage();
37    }
38
39    protected function compilePublicationPage(): string
40    {
41        return $this->compileView($this->page->type->detailTemplate, [
42            'publication' => $this->page,
43        ]);
44    }
45
46    protected function compilePublicationListPage(): string
47    {
48        return $this->compileView($this->page->type->listTemplate, [
49            'publicationType' => $this->page->type,
50        ]);
51    }
52
53    protected function compileView(string $template, array $data): string
54    {
55        return str_ends_with($template, '.blade.php')
56            ? AnonymousViewCompiler::handle($this->getTemplateFilePath($template), $data)
57            : View::make($template, $data)->render();
58    }
59
60    protected function getTemplateFilePath(string $template): string
61    {
62        $template = basename($template, '.blade.php');
63
64        return "{$this->page->type->getDirectory()}/$template.blade.php";
65    }
66}