Passed
Pull Request — master (#59)
by
unknown
28:20 queued 13:14
created

MainLayout::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace App\LazyRendering\View;
4
5
use App\Asset\AppAsset;
6
use Generator;
7
use Psr\Http\Message\RequestInterface;
8
use Yiisoft\Assets\AssetManager;
9
use Yiisoft\Html\Html;
10
11
class MainLayout
12
{
13
    private AssetManager $assetManager;
14
    private ?string $title = null;
15
    private array $metaTags = [
16
        ['charset' => 'UTF-8'],
17
        [
18
            'name'    => 'viewport',
19
            'content' => 'width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0',
20
        ],
21
        ['http-equiv' => 'X-UA-Compatible', 'content' => 'ie=edge'],
22
    ];
23
24
    public function __construct(AssetManager $assetManager)
25
    {
26
        $this->assetManager = $assetManager;
27
    }
28
29
    public function render(iterable $content, RequestInterface $request): iterable
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

29
    public function render(iterable $content, /** @scrutinizer ignore-unused */ RequestInterface $request): iterable

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
    {
31
        ob_start();
32
        foreach ($this->renderPage($content) as $content) {
33
            yield  ob_get_clean() . $content;
0 ignored issues
show
Bug introduced by
Are you sure $content of type iterable|string can be used in concatenation? ( Ignorable by Annotation )

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

33
            yield  ob_get_clean() . /** @scrutinizer ignore-type */ $content;
Loading history...
34
            ob_start();
35
        }
36
        ob_end_clean();
37
    }
38
39
    protected function renderPage(iterable $content): Generator
40
    {
41
        $this->assetManager->register([AppAsset::class]);
42
43
        echo '<html><head>' . ($this->title !== null ? '<title>' . Html::encode($this->title) . '</title>' : '');
44
        // Meta tags
45
        foreach ($this->metaTags as $value) {
46
            echo Html::tag('meta', '', $value);
47
        }
48
        // CSS files
49
        foreach ($this->assetManager->getCssFiles() as $value) {
50
            echo Html::cssFile($value['url'], $value['attributes']);
51
        }
52
        yield '</head><body><main role="main" class="container py-4">';
53
        // Content
54
        yield from $content;
55
        yield '</main>';
56
        // JS files
57
        foreach ($this->assetManager->getJsFiles() as $value) {
58
            yield Html::script($value['url'], $value['attributes']);
59
        }
60
        yield '</body></html>';
61
    }
62
}
63