Test Failed
Push — master ( b4ced5...11e5de )
by Dev
12:09
created

PageController::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace PiedWeb\CMSBundle\Controller;
4
5
use PiedWeb\CMSBundle\Entity\PageInterface as Page;
6
use PiedWeb\CMSBundle\Repository\PageRepository;
7
use PiedWeb\CMSBundle\Service\AppConfigHelper as App;
8
use PiedWeb\CMSBundle\Service\Repository;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Twig\Environment as Twig;
14
15
class PageController extends AbstractController
16
{
17
    /**
18
     * @var ParameterBagInterface
19
     */
20
    protected $params;
21
22
    /**
23
     * @var App
24
     */
25
    protected $app;
26
    protected $twig;
27
28
    public function __construct(
29
        ParameterBagInterface $params,
30
        Twig $twig
31
    ) {
32
        $this->params = $params;
33
        $this->twig = $twig;
34
        $this->app = App::load(null, $this->params);
35
    }
36
37
    public function show(?string $slug, ?string $host, Request $request): Response
38
    {
39
        $page = $this->getPage($slug, $host, $request);
40
41
        // SEO redirection if we are not on the good URI (for exemple /fr/tagada instead of /tagada)
42
        if ((null === $host || $host == $request->getHost())
43
            && false !== $redirect = $this->checkIfUriIsCanonical($request, $page)) {
44
            return $this->redirect($redirect[0], $redirect[1]);
45
        }
46
47
        // Maybe the page is a redirection
48
        if ($page->getRedirection()) {
49
            return $this->redirect($page->getRedirection(), $page->getRedirectionCode());
50
        }
51
52
        return $this->render(
53
            $this->getTemplate($page->getTemplate() ? $page->getTemplate() : '/page/page.html.twig'),
54
            array_merge(['page' => $page], $this->app->getParamsForRendering())
55
        );
56
    }
57
58
    public function preview(?string $slug, ?string $host, Request $request): Response
59
    {
60
        $page = $this->getPage($slug, $host, $request);
61
        $page->setMainContent($request->request->get('plaintext'));
62
        // todo update all fields to avoid errors with autosave
63
        // (getting them via json?!)
64
        // And not getPage but create a new Page !!! (else, error on unexisting Page)
65
66
        return $this->render(
67
            $this->getTemplate('/page/preview.html.twig'),
68
            array_merge(['page' => $page], $this->app->getParamsForRendering())
69
        );
70
    }
71
72
    protected function getTemplate($path)
73
    {
74
        return $this->app->getTemplate($path, $this->twig);
75
    }
76
77
    public function showFeed(?string $slug, ?string $host, Request $request)
78
    {
79
        $page = $this->getPage($slug, $host, $request);
80
81
        if ('homepage' == $slug) {
82
            return $this->redirect($this->generateUrl('piedweb_cms_page_feed', ['slug' => 'index']), 301);
83
        }
84
85
        $response = new Response();
86
        $response->headers->set('Content-Type', 'text/xml');
87
88
        return $this->render(
89
            $this->getTemplate('/page/rss.xml.twig'),
90
            array_merge(['page' => $page], $this->app->getParamsForRendering()),
91
            $response
92
        );
93
    }
94
95
    /**
96
     * Show Last created page in an XML Feed.
97
     */
98
    public function showMainFeed(?string $host, Request $request)
99
    {
100
        $this->app->switchCurrentApp($host);
101
        $locale = $request->getLocale() ? rtrim($request->getLocale(), '/') : $this->params->get('locale');
102
        $LocaleHomepage = $this->getPage($locale, $host, $request, false);
103
        $slug = 'homepage';
104
        $page = $LocaleHomepage ?: $this->getPage($slug, $host, $request);
105
106
        $params = [
107
            'pages' => $this->getPages(5, $request),
108
            'page' => $page,
109
            'feedUri' => ($this->params->get('locale') == $locale ? '' : $locale.'/').'feed.xml',
110
        ];
111
112
        return $this->render(
113
            $this->getTemplate('/page/rss.xml.twig'),
114
            array_merge($params, $this->app->getParamsForRendering())
115
        );
116
    }
117
118
    public function showSitemap($_format, ?string $host, Request $request)
119
    {
120
        $this->app->switchCurrentApp($host);
121
        $pages = $this->getPages(null, $request);
122
123
        if (!$pages) {
124
            throw $this->createNotFoundException();
125
        }
126
127
        return $this->render(
128
            $this->getTemplate('/page/sitemap.'.$_format.'.twig'),
129
            [
130
                'pages' => $pages,
131
                'app_base_url' => $this->app->getBaseUrl(),
132
            ]
133
        );
134
    }
135
136
    protected function getPages(?int $limit = null, Request $request)
137
    {
138
        $requestedLocale = rtrim($request->getLocale(), '/');
139
        //var_dump($requestedLocale);exit;
140
141
        $pages = $this->getPageRepository()->getIndexablePages(
142
            $this->app->getHost(),
143
            $this->app->isFirstApp(),
144
            $requestedLocale,
145
            $this->params->get('locale'),
146
            $limit
147
        )->getQuery()->getResult();
148
149
        //foreach ($pages as $page) echo $page->getMetaRobots().' '.$page->getTitle().'<br>';
150
        //exit('feed updated');
151
152
        return $pages;
153
    }
154
155
    /**
156
     * @return PageRepository
157
     */
158
    protected function getPageRepository()
159
    {
160
        return Repository::getPageRepository($this->getDoctrine(), $this->params->get('pwc.entity_page'));
161
    }
162
163
    protected function getPage(?string &$slug, ?string $host = null, ?Request $request, $throwException = true): ?Page
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

163
    protected function getPage(?string &$slug, ?string $host = null, /** @scrutinizer ignore-unused */ ?Request $request, $throwException = true): ?Page

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...
164
    {
165
        $this->app->switchCurrentApp($host);
166
        $slug = $this->noramlizeSlug($slug);
167
        $page = $this->getPageRepository()->getPage($slug, $this->app->getHost(), $this->app->isFirstApp());
168
169
        // Check if page exist
170
        if (null === $page) {
171
            if ($throwException) {
172
                throw $this->createNotFoundException();
173
            } else {
174
                return null;
175
            }
176
        }
177
178
        if (!$page->getLocale()) { // avoid bc break
179
            $page->setLocale($this->params->get('pwc.locale'));
180
        }
181
182
        //if (null !== $request) { $request->setLocale($page->getLocale()); }
183
        $this->get('translator')->setLocale($page->getLocale());
184
185
        // Check if page is public
186
        if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_EDITOR')) {
187
            if ($throwException) {
188
                throw $this->createNotFoundException();
189
            } else {
190
                return null;
191
            }
192
        }
193
194
        return $page;
195
    }
196
197
    protected function noramlizeSlug($slug)
198
    {
199
        return (null === $slug || '' === $slug) ? 'homepage' : rtrim(strtolower($slug), '/');
200
    }
201
202
    protected function checkIfUriIsCanonical(Request $request, Page $page)
203
    {
204
        $real = $request->getRequestUri();
205
206
        $expected = 'homepage' == $page->getSlug() ?
207
            $this->get('piedweb.page_canonical')->generatePathForHomepage() :
208
            $this->get('piedweb.page_canonical')->generatePathForPage($page->getRealSlug());
209
210
        if ($real != $expected) {
211
            return [$request->getBasePath().$expected, 301];
212
        }
213
214
        return false;
215
    }
216
}
217