StaticRegistry::getPage()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 22
nc 3
nop 1
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheCodingMachine\CMS\StaticRegistry\Registry;
5
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use TheCodingMachine\CMS\Block\Block;
9
use TheCodingMachine\CMS\Block\BlockInterface;
10
use TheCodingMachine\CMS\Page\PageRegistryInterface;
11
use TheCodingMachine\CMS\StaticRegistry\Twig\CmsPageExtension;
12
use TheCodingMachine\CMS\StaticRegistry\Twig\Context;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\CMS\StaticRegistry\Twig\Context was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use TheCodingMachine\CMS\Theme\TwigThemeDescriptor;
14
15
class StaticRegistry implements PageRegistryInterface
16
{
17
    /**
18
     * @var PageRegistry
19
     */
20
    private $pageRegistry;
21
    /**
22
     * @var ThemeRegistry
23
     */
24
    private $themeRegistry;
25
26
    public function __construct(PageRegistry $pageRegistry, ThemeRegistry $themeRegistry)
27
    {
28
        $this->pageRegistry = $pageRegistry;
29
        $this->themeRegistry = $themeRegistry;
30
    }
31
32
    public function getPage(ServerRequestInterface $request): ?BlockInterface
33
    {
34
        $uri = $request->getUri();
35
        $domain = $uri->getHost();
36
        $url = $uri->getPath();
37
38
        try {
39
            $page = $this->pageRegistry->getPage($url, $domain);
40
        } catch (PageNotFoundException $e) {
41
            return null;
42
        }
43
44
        $themeDescriptor = $this->themeRegistry->getThemeDescriptor($page->getTheme());
45
46
        if ($page->getTemplate() !== null) {
47
            $context = $page->getContext();
48
            $context['content'][] = $page->getContent();
49
            $contentBlock = new Block(new TwigThemeDescriptor($page->getTemplate(), [
50
                'theme' => $themeDescriptor->getPath()
51
            ]), $context);
52
        } else {
53
            $contentBlock = $page->getContent();
54
        }
55
56
        $pageBlock = new Block($themeDescriptor, [
57
            'content' => [ $contentBlock ],
58
            'title' => $page->getTitle(),
59
            'url' => $page->getUrl(),
60
            'menu' => $this->pageRegistry->getRootMenuItem()
61
        ]);
62
63
        return $pageBlock;
64
    }
65
}
66