Passed
Push — master ( 3e1455...cd0e92 )
by Peter
02:07
created

Index::fallback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 30
rs 9.6333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Http\Controllers\Website;
6
7
use AbterPhp\Framework\Assets\AssetManager;
8
use AbterPhp\Framework\Http\Controllers\ControllerAbstract;
9
use AbterPhp\Framework\Session\FlashService;
10
use AbterPhp\Framework\Template\Engine;
11
use AbterPhp\Website\Constant\Routes;
12
use AbterPhp\Website\Domain\Entities\Page\Assets;
13
use AbterPhp\Website\Domain\Entities\Page\Meta;
14
use AbterPhp\Website\Domain\Entities\PageLayout\Assets as LayoutAssets;
15
use AbterPhp\Website\Orm\PageRepo;
16
use Opulence\Http\Responses\Response;
17
use Opulence\Http\Responses\ResponseHeaders;
18
use Opulence\Orm\OrmException;
19
use Opulence\Routing\Urls\UrlGenerator;
20
21
class Index extends ControllerAbstract
22
{
23
    /** @var Engine */
24
    protected $templateEngine;
25
26
    /** @var PageRepo */
27
    protected $pageRepo;
28
29
    /** @var UrlGenerator */
30
    protected $urlGenerator;
31
32
    /** @var AssetManager */
33
    protected $assetManager;
34
35
    /** @var string|null */
36
    protected $baseUrl;
37
38
    /** @var string */
39
    protected $siteTitle;
40
41
    /**
42
     * Index constructor.
43
     *
44
     * @param FlashService $flashService
45
     * @param Engine       $templateEngine
46
     * @param PageRepo     $pageRepo
47
     * @param UrlGenerator $urlGenerator
48
     * @param AssetManager $assetManager
49
     * @param string|null  $baseUrl
50
     * @param string       $siteTitle
51
     */
52
    public function __construct(
53
        FlashService $flashService,
54
        Engine $templateEngine,
55
        PageRepo $pageRepo,
56
        UrlGenerator $urlGenerator,
57
        AssetManager $assetManager,
58
        ?string $baseUrl,
59
        string $siteTitle
60
    ) {
61
        $this->templateEngine = $templateEngine;
62
        $this->pageRepo       = $pageRepo;
63
        $this->urlGenerator   = $urlGenerator;
64
        $this->assetManager   = $assetManager;
65
66
        $this->baseUrl   = $baseUrl;
67
        $this->siteTitle = $siteTitle;
68
69
        parent::__construct($flashService);
70
    }
71
72
    /**
73
     * Shows the homepage
74
     *
75
     * @return Response The response
76
     */
77
    public function index(): Response
78
    {
79
        return $this->fallback('index');
80
    }
81
82
    /**
83
     * Shows the homepage
84
     *
85
     * @return Response The response
86
     */
87
    public function fallback(string $identifier): Response
88
    {
89
        $this->view = $this->viewFactory->createView('contents/frontend/page');
90
91
        try {
92
            $page = $this->pageRepo->getWithLayout($identifier);
93
        } catch (OrmException $exc) {
94
            return $this->notFound();
95
        }
96
97
        $vars      = ['title' => $page->getTitle()];
98
        $templates = [
99
            'body'   => $page->getBody(),
100
            'layout' => $page->getLayout(),
101
        ];
102
103
        $body = $this->templateEngine->run('page', $page->getIdentifier(), $templates, $vars);
104
105
        $pageUrl     = $this->urlGenerator->createFromName(Routes::ROUTE_FALLBACK, $identifier);
106
        $homepageUrl = $this->urlGenerator->createFromName(Routes::ROUTE_INDEX);
107
108
        $this->view->setVar('body', $body);
109
        $this->view->setVar('siteTitle', $this->siteTitle);
110
        $this->view->setVar('pageUrl', $pageUrl);
111
        $this->view->setVar('homepageUrl', $homepageUrl);
112
113
        $this->setMetaVars($page->getMeta());
114
        $this->setAssetsVars($page->getAssets());
115
116
        return $this->createResponse($page->getTitle());
117
    }
118
119
    /**
120
     * @param Meta $meta
121
     */
122
    protected function setMetaVars(Meta $meta)
123
    {
124
        $this->view->setVar('metaDescription', $meta->getDescription());
125
        $this->view->setVar('metaKeywords', explode(',', $meta->getKeywords()));
126
        $this->view->setVar('metaCopyright', $meta->getCopyright());
127
        $this->view->setVar('metaAuthor', $meta->getAuthor());
128
        $this->view->setVar('metaRobots', $meta->getRobots());
129
        $this->view->setVar('metaOGDescription', $meta->getOGDescription());
130
        $this->view->setVar('metaOGTitle', $meta->getOGTitle());
131
        $this->view->setVar('metaOGImage', $meta->getOGImage());
132
    }
133
134
    /**
135
     * @param Assets|null $assets
136
     */
137
    protected function setAssetsVars(?Assets $assets)
138
    {
139
        if ($assets === null) {
140
            return;
141
        }
142
143
        $origHeader = $this->view->hasVar('header') ? (string)$this->view->getVar('header') : '';
144
        $origFooter = $this->view->hasVar('footer') ? (string)$this->view->getVar('footer') : '';
145
146
        $this->view->setVar('header', $origHeader . $assets->getHeader());
147
        $this->view->setVar('footer', $origFooter . $assets->getFooter());
148
        $this->view->setVar('page', $assets->getKey());
149
150
        $key = $assets->getKey();
151
        foreach ($assets->getJsFiles() as $jsFile) {
152
            $this->assetManager->addJs($key, $jsFile);
153
        }
154
        foreach ($assets->getCssFiles() as $cssFile) {
155
            $this->assetManager->addCss($key, $cssFile);
156
        }
157
158
        $this->setLayoutAssetsVars($assets->getLayoutAssets());
159
    }
160
161
    /**
162
     * @param LayoutAssets|null $assets
163
     */
164
    protected function setLayoutAssetsVars(?LayoutAssets $assets)
165
    {
166
        if ($assets === null) {
167
            return;
168
        }
169
170
        $origHeader = $this->view->hasVar('header') ? (string)$this->view->getVar('header') : '';
171
        $origFooter = $this->view->hasVar('footer') ? (string)$this->view->getVar('footer') : '';
172
173
        $this->view->setVar('header', $origHeader . $assets->getHeader());
174
        $this->view->setVar('footer', $origFooter . $assets->getFooter());
175
        $this->view->setVar('layout', $assets->getKey());
176
177
        $key = $assets->getKey();
178
        foreach ($assets->getJsFiles() as $jsFile) {
179
            $this->assetManager->addJs($key, $jsFile);
180
        }
181
        foreach ($assets->getCssFiles() as $cssFile) {
182
            $this->assetManager->addCss($key, $cssFile);
183
        }
184
    }
185
186
    /**
187
     * @param string $route
188
     *
189
     * @return string
190
     * @throws \Opulence\Routing\Urls\URLException
191
     */
192
    protected function getCanonicalUrl(string $route): string
193
    {
194
        $path = $this->urlGenerator->createFromName($route);
195
196
        return $this->baseUrl . ltrim($path, '/');
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    protected function getBaseUrl(): string
203
    {
204
        if ($this->baseUrl === null) {
205
            $this->baseUrl = sprintf(
206
                '%s://%s/',
207
                $this->request->getServer()->get('REQUEST_SCHEME'),
208
                $this->request->getServer()->get('SERVER_NAME')
209
            );
210
        }
211
212
        return $this->baseUrl;
213
    }
214
215
    /**
216
     * 404 page
217
     *
218
     * @return Response The response
219
     */
220
    protected function notFound(): Response
221
    {
222
        $this->view = $this->viewFactory->createView('contents/frontend/404');
223
224
        $response = $this->createResponse('404 Page not Found');
225
        $response->setStatusCode(ResponseHeaders::HTTP_NOT_FOUND);
226
227
        return $response;
228
    }
229
}
230