Passed
Pull Request — master (#59)
by Alexander
26:34 queued 11:31
created

MainLayout   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 8
eloc 28
c 1
b 1
f 0
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A renderPage() 0 24 5
A __construct() 0 3 1
A render() 0 8 2
1
<?php
2
3
namespace App\LazyRendering\View;
4
5
use App\Asset\AppAsset;
6
use App\Widget\PerformanceMetrics;
7
use Generator;
8
use Psr\Http\Message\RequestInterface;
9
use Yiisoft\Assets\AssetManager;
10
use Yiisoft\Html\Html;
11
12
class MainLayout
13
{
14
    private AssetManager $assetManager;
15
    private ?string $title = null;
16
    private array $metaTags = [
17
        ['charset' => 'UTF-8'],
18
        [
19
            'name'    => 'viewport',
20
            'content' => 'width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0',
21
        ],
22
        ['http-equiv' => 'X-UA-Compatible', 'content' => 'ie=edge'],
23
    ];
24
25
    public function __construct(AssetManager $assetManager)
26
    {
27
        $this->assetManager = $assetManager;
28
    }
29
30
    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

30
    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...
31
    {
32
        ob_start();
33
        foreach ($this->renderPage($content) as $contentPart) {
34
            yield  ob_get_clean() . $contentPart;
0 ignored issues
show
Bug introduced by
Are you sure $contentPart 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

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