Completed
Pull Request — master (#2)
by David
02:09
created

StaticRegistry::getPage()   B

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