Test Failed
Push — master ( 79aedb...b27830 )
by Dev
10:51
created

PageController   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 89
dl 0
loc 189
ccs 0
cts 74
cp 0
rs 9.76
c 4
b 1
f 0
wmc 33

8 Methods

Rating   Name   Duplication   Size   Complexity  
B showFeed() 0 31 8
A __construct() 0 4 1
A getPages() 0 18 1
A showSitemap() 0 14 2
A checkIfUriIsCanonical() 0 13 3
A showMainFeed() 0 26 4
B show() 0 41 11
A preview() 0 18 3
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
    protected $params;
16
17
    public function __construct(
18
        ParameterBagInterface $params
19
    ) {
20
        $this->params = $params;
21
    }
22
23
    public function show(
24
        ?string $slug,
25
        ?string $host,
26
        Request $request
27
    ) {
28
        $app = App::load($host ?? $request, $this->params);
29
        $slug = (null === $slug || '' === $slug) ? 'homepage' : rtrim(strtolower($slug), '/');
30
        $page = Repository::getPageRepository($this->getDoctrine(), $this->params->get('pwc.entity_page'))
31
            ->getPage($slug, $host ?? $app->getHost(), $app->isFirstApp());
32
33
        // Check if page exist
34
        if (null === $page) {
35
            throw $this->createNotFoundException();
36
        }
37
38
        if (!$page->getLocale()) { // avoid bc break
39
            $page->setLocale($this->params->get('pwc.locale'));
40
        }
41
42
        $request->setLocale($page->getLocale());
43
        $this->get('translator')->setLocale($page->getLocale());
44
45
        // Check if page is public
46
        if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_ADMIN')) {
47
            throw $this->createNotFoundException();
48
        }
49
50
        // SEO redirection if we are not on the good URI (for exemple /fr/tagada instead of /tagada)
51
        if ((null === $host || $host == $request->getHost())
52
            && false !== $redirect = $this->checkIfUriIsCanonical($request, $page)) {
53
            return $this->redirect($redirect[0], $redirect[1]);
54
        }
55
56
        // Maybe the page is a redirection
57
        if ($page->getRedirection()) {
58
            return $this->redirect($page->getRedirection(), $page->getRedirectionCode());
59
        }
60
61
        $template = $app->getDefaultTemplate();
62
63
        return $this->render($template, array_merge(['page' => $page], $app->getParamsForRendering()));
64
    }
65
66
    protected function checkIfUriIsCanonical(Request $request, Page $page)
67
    {
68
        $real = $request->getRequestUri();
69
70
        $expected = 'homepage' == $page->getSlug() ?
71
            $this->get('piedweb.page_canonical')->generatePathForHomepage() :
72
            $this->get('piedweb.page_canonical')->generatePathForPage($page->getRealSlug());
73
74
        if ($real != $expected) {
75
            return [$request->getBasePath().$expected, 301];
76
        }
77
78
        return false;
79
    }
80
81
    public function preview(
82
        ?string $slug,
83
        Request $request
84
    ) {
85
        $app = App::load($request, $this->params);
86
        $pageEntity = $this->params->get('pwc.entity_page');
87
88
        $page = (null === $slug || '' === $slug) ?
89
            new $pageEntity()
90
            : $this->getDoctrine()
91
            ->getRepository($pageEntity)
92
            ->findOneBy(['slug' => rtrim(strtolower($slug), '/')]);
93
94
        $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

94
        $page->/** @scrutinizer ignore-call */ 
95
               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...
95
96
        return $this->render(
97
            '@PiedWebCMS/page/preview.html.twig',
98
            array_merge(['page' => $page], $app->getParamsForRendering())
99
        );
100
    }
101
102
    public function showFeed(
103
        ?string $slug,
104
        ?string $host,
105
        Request $request
106
    ) {
107
        if ('homepage' == $slug) {
108
            return $this->redirect($this->generateUrl('piedweb_cms_page_feed', ['slug' => 'index']), 301);
109
        }
110
111
        $app = App::load($host ?? $request, $this->params);
112
        $slug = (null === $slug || 'index' === $slug) ? 'homepage' : rtrim(strtolower($slug), '/');
113
        $page = Repository::getPageRepository($this->getDoctrine(), $this->params->get('pwc.entity_page'))
114
            ->getPage($slug, $app->getHost(), $app->isFirstApp());
115
116
        // Check if page exist
117
        if (null === $page || $page->getChildrenPages()->count() < 1) {
118
            throw $this->createNotFoundException();
119
        }
120
121
        // Check if page is public
122
        if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_ADMIN')) {
123
            throw $this->createNotFoundException();
124
        }
125
126
        $response = new Response();
127
        $response->headers->set('Content-Type', 'text/xml');
128
129
        return $this->render(
130
            '@PiedWebCMS/page/rss.xml.twig',
131
            array_merge(['page' => $page], $app->getParamsForRendering()),
132
            $response
133
        );
134
    }
135
136
    /**
137
     * Show Last created page in an XML Feed.
138
     */
139
    public function showMainFeed(
140
        ?string $host,
141
        Request $request
142
    ) {
143
        $app = App::load($host ?? $request, $this->params);
144
        // Retrieve info from homepage, for i18n, assuming it's named with locale
145
        $locale = $request->getLocale() ? rtrim($request->getLocale(), '/') : $this->params->get('locale');
146
        $LocaleHomepage = Repository::getPageRepository($this->getDoctrine(), $this->params->get('pwc.entity_page'))
147
            ->getPage($locale, $app->getHost(), $app->isFirstApp());
148
        $page = $LocaleHomepage ?? Repository::getPageRepository(
149
            $this->getDoctrine(),
150
            $this->params->get('pwc.entity_page')
151
        )
152
            ->getPage('homepage', $app->getHost(), $app->isFirstApp());
153
154
        if (!$page) {
155
            throw $this->createNotFoundException();
156
        }
157
158
        $this->params = [
159
            'pages' => $this->getPages(5, $request),
160
            'page' => $page,
161
            'feedUri' => ($this->params->get('locale') == $locale ? '' : $locale.'/').'feed.xml',
162
        ];
163
164
        return $this->render('@PiedWebCMS/page/rss.xml.twig', array_merge($this->params, $app->getParamsForRendering()));
0 ignored issues
show
Bug introduced by
$this->params of type Symfony\Component\Depend...g\ParameterBagInterface is incompatible with the type array expected by parameter $array1 of array_merge(). ( Ignorable by Annotation )

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

164
        return $this->render('@PiedWebCMS/page/rss.xml.twig', array_merge(/** @scrutinizer ignore-type */ $this->params, $app->getParamsForRendering()));
Loading history...
165
    }
166
167
    public function showSitemap(
168
        ?string $host,
169
        Request $request
170
    ) {
171
        $app = App::load($host ?? $request, $this->params);
172
        $pages = $this->getPages(null, $request);
173
174
        if (!$pages) {
175
            throw $this->createNotFoundException();
176
        }
177
178
        return $this->render('@PiedWebCMS/page/sitemap.'.$request->getRequestFormat().'.twig', [
179
            'pages' => $pages,
180
            'app_base_url' => $app->getBaseUrl(),
181
        ]);
182
    }
183
184
    protected function getPages(?int $limit = null, Request $request)
185
    {
186
        $requestedLocale = rtrim($request->getLocale(), '/');
187
188
        $app = App::load($request, $this->params);
189
        $pages = Repository::getPageRepository($this->getDoctrine(), $this->params->get('pwc.entity_page'))
190
            ->getIndexablePages(
191
                $app->getHost(),
192
                $app->isFirstApp(),
193
                $requestedLocale,
194
                $this->params->get('locale'),
195
                $limit
196
            )->getQuery()->getResult();
197
198
        //foreach ($pages as $page) echo $page->getMetaRobots().' '.$page->getTitle().'<br>';
199
        //exit('feed updated');
200
201
        return $pages;
202
    }
203
}
204