Passed
Push — master ( 97dd58...4109cd )
by Dev
10:22
created

PageController::showFeed()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 16
nc 13
nop 4
dl 0
loc 32
rs 8.4444
c 0
b 0
f 0
ccs 0
cts 15
cp 0
crap 72
1
<?php
2
3
namespace PiedWeb\CMSBundle\Controller;
4
5
use PiedWeb\CMSBundle\Entity\PageInterface as Page;
6
use PiedWeb\CMSBundle\Service\AppConfigHelper as App;
7
use PiedWeb\CMSBundle\Service\Repository;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
class PageController extends AbstractController
14
{
15
    public function show(
16
        ?string $slug,
17
        ?string $host,
18
        Request $request,
19
        ParameterBagInterface $params
20
    ) {
21
        $app = App::load($host ?? $request, $params);
22
        $slug = (null === $slug || '' === $slug) ? 'homepage' : rtrim(strtolower($slug), '/');
23
        $page = Repository::getPageRepository($this->getDoctrine(), $params->get('pwc.entity_page'))
24
            ->getPage($slug, $host ?? $app->getHost(), $app->isFirstApp());
25
26
        // Check if page exist
27
        if (null === $page) {
28
            throw $this->createNotFoundException();
29
        }
30
31
        if (null !== $page->getLocale()) { // avoid bc break
32
            $page->setLocale($params->get('pwc.locale'));
33
        }
34
35
        $request->setLocale($page->getLocale());
36
        $this->get('translator')->setLocale($page->getLocale());
37
38
        // Check if page is public
39
        if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_ADMIN')) {
40
            throw $this->createNotFoundException();
41
        }
42
43
        // SEO redirection if we are not on the good URI (for exemple /fr/tagada instead of /tagada)
44
        if ((null === $host || $host == $request->getHost())
45
            && false !== $redirect = $this->checkIfUriIsCanonical($request, $page)) {
46
            return $this->redirect($redirect[0], $redirect[1]);
47
        }
48
49
        // Maybe the page is a redirection
50
        if ($page->getRedirection()) {
51
            return $this->redirect($page->getRedirection(), $page->getRedirectionCode());
52
        }
53
54
        $template = $app->getDefaultTemplate();
55
56
        return $this->render($template, array_merge(['page' => $page], $app->getParamsForRendering()));
57
    }
58
59
    protected function checkIfUriIsCanonical(Request $request, Page $page)
60
    {
61
        $real = $request->getRequestUri();
62
63
        $expected = 'homepage' == $page->getSlug() ?
64
            $this->get('piedweb.page_canonical')->generatePathForHomepage() :
65
            $this->get('piedweb.page_canonical')->generatePathForPage($page->getRealSlug());
66
67
        if ($real != $expected) {
68
            return [$request->getBasePath().$expected, 301];
69
        }
70
71
        return false;
72
    }
73
74
    public function preview(
75
        ?string $slug,
76
        Request $request,
77
        ParameterBagInterface $params
78
    ) {
79
        $app = App::load($request, $params);
80
        $pageEntity = $params->get('pwc.entity_page');
81
82
        $page = (null === $slug || '' === $slug) ?
83
            new $pageEntity()
84
            : $this->getDoctrine()
85
            ->getRepository($pageEntity)
86
            ->findOneBy(['slug' => rtrim(strtolower($slug), '/')]);
87
88
        $page->setMainContent($request->request->get('plaintext')); // todo update all fields to avoid errors
0 ignored issues
show
Bug introduced by
The method setMainContent() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        $page->/** @scrutinizer ignore-call */ 
89
               setMainContent($request->request->get('plaintext')); // todo update all fields to avoid errors

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
90
        return $this->render(
91
            '@PiedWebCMS/admin/page_preview.html.twig',
92
            array_merge(['page' => $page], $app->getParamsForRendering()));
93
    }
94
95
    public function showFeed(
96
        ?string $slug,
97
        ?string $host,
98
        Request $request,
99
        ParameterBagInterface $params
100
    ) {
101
        if ('homepage' == $slug) {
102
            return $this->redirect($this->generateUrl('piedweb_cms_page_feed', ['slug' => 'index']), 301);
103
        }
104
105
        $app = App::load($host ?? $request, $params);
106
        $slug = (null === $slug || 'index' === $slug) ? 'homepage' : rtrim(strtolower($slug), '/');
107
        $page = Repository::getPageRepository($this->getDoctrine(), $params->get('pwc.entity_page'))
108
            ->getPage($slug, $app->getHost(), $app->isFirstApp());
109
110
        // Check if page exist
111
        if (null === $page || $page->getChildrenPages()->count() < 1) {
112
            throw $this->createNotFoundException();
113
        }
114
115
        // Check if page is public
116
        if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_ADMIN')) {
117
            throw $this->createNotFoundException();
118
        }
119
120
        $response = new Response();
121
        $response->headers->set('Content-Type', 'text/xml');
122
123
        return $this->render(
124
            '@PiedWebCMS/page/rss.xml.twig',
125
            array_merge(['page' => $page], $app->getParamsForRendering()),
126
            $response
127
        );
128
    }
129
130
    /**
131
     * Show Last created page in an XML Feed.
132
     */
133
    public function showMainFeed(
134
        ?string $host,
135
        Request $request,
136
        ParameterBagInterface $params
137
    ) {
138
        $app = App::load($host ?? $request, $params);
139
        // Retrieve info from homepage, for i18n, assuming it's named with locale
140
        $locale = $request->getLocale() ? rtrim($request->getLocale(), '/') : $params->get('locale');
141
        $LocaleHomepage = Repository::getPageRepository($this->getDoctrine(), $params->get('pwc.entity_page'))
142
            ->getPage($locale, $app->getHost(), $app->isFirstApp());
143
        $page = $LocaleHomepage ?? Repository::getPageRepository($this->getDoctrine(), $params->get('pwc.entity_page'))
144
            ->getPage('homepage', $app->getHost(), $app->isFirstApp());
145
146
        return $this->render('@PiedWebCMS/page/rss.xml.twig', [
147
            'pages' => $this->getPages(5, $request, $params),
148
            'page' => $page,
149
            'feedUri' => 'feed'.($params->get('locale') == $locale ? '' : '.'.$locale).'.xml',
150
            'app_base_url' => $app->getBaseUrl(),
151
            'app_name' => $app->getApp('name'),
152
        ]);
153
    }
154
155
    public function showSitemap(
156
        ?string $host,
157
        Request $request,
158
        ParameterBagInterface $params
159
    ) {
160
        $app = App::load($host ?? $request, $params);
161
        $pages = $this->getPages(null, $request, $params);
162
163
        if (!$pages) {
164
            throw $this->createNotFoundException();
165
        }
166
167
        return $this->render('@PiedWebCMS/page/sitemap.'.$request->getRequestFormat().'.twig', [
168
            'pages' => $pages,
169
            'app_base_url' => $app->getBaseUrl(),
170
        ]);
171
    }
172
173
    protected function getPages(?int $limit = null, Request $request, ParameterBagInterface $params)
174
    {
175
        $requestedLocale = rtrim($request->getLocale(),'/');
176
177
        $app = App::load($request, $params);
178
        $pages = Repository::getPageRepository($this->getDoctrine(), $params->get('pwc.entity_page'))
179
            ->getIndexablePages(
180
                $app->getHost(),
181
                $app->isFirstApp(),
182
                $requestedLocale,
183
                $params->get('locale'),
184
                $limit
185
            )->getQuery()->getResult();
186
187
        //foreach ($pages as $page) echo $page->getMetaRobots().' '.$page->getTitle().'<br>';
188
        //exit('feed updated');
189
190
        return $pages;
191
    }
192
}
193