Test Failed
Push — master ( 304e55...90978a )
by Dev
11:34
created

PageController::getPageRepository()   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
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
14
class PageController extends AbstractController
15
{
16
    /**
17
     * @var ParameterBagInterface
18
     */
19
    protected $params;
20
21
    /**
22
     * @var App
23
     */
24
    protected $app;
25
26
    public function __construct(
27
        ParameterBagInterface $params
28
    ) {
29
        $this->params = $params;
30
        $this->app = App::load(null, $this->params);
31
    }
32
33
    public function show(?string $slug, ?string $host, Request $request): Response
34
    {
35
        $page = $this->getPage($slug, $host, $request);
36
37
        // SEO redirection if we are not on the good URI (for exemple /fr/tagada instead of /tagada)
38
        if ((null === $host || $host == $request->getHost())
39
            && false !== $redirect = $this->checkIfUriIsCanonical($request, $page)) {
40
            return $this->redirect($redirect[0], $redirect[1]);
41
        }
42
43
        // Maybe the page is a redirection
44
        if ($page->getRedirection()) {
45
            return $this->redirect($page->getRedirection(), $page->getRedirectionCode());
46
        }
47
48
        $template = $this->app->getDefaultTemplate();
49
50
        return $this->render($template, array_merge(['page' => $page], $this->app->getParamsForRendering()));
51
    }
52
53
    public function preview(?string $slug, ?string $host, Request $request): Response
54
    {
55
        $page = $this->getPage($slug, $host, $request);
56
        $page->setMainContent($request->request->get('plaintext'));
57
        // todo update all fields to avoid errors with autosave
58
        // (getting them via json?!)
59
        // And not getPage but create a new Page !!! (else, error on unexisting Page)
60
61
        return $this->render(
62
            '@PiedWebCMS/page/preview.html.twig',
63
            array_merge(['page' => $page], $this->app->getParamsForRendering())
64
        );
65
    }
66
67
    public function showFeed(?string $slug, ?string $host, Request $request)
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

67
    public function showFeed(?string $slug, ?string $host, /** @scrutinizer ignore-unused */ Request $request)

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...
68
    {
69
        $page = $this->getPage($slug, $host);
0 ignored issues
show
Bug introduced by
The call to PiedWeb\CMSBundle\Contro...geController::getPage() has too few arguments starting with request. ( Ignorable by Annotation )

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

69
        /** @scrutinizer ignore-call */ 
70
        $page = $this->getPage($slug, $host);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
70
71
        if ('homepage' == $slug) {
72
            return $this->redirect($this->generateUrl('piedweb_cms_page_feed', ['slug' => 'index']), 301);
73
        }
74
75
        $response = new Response();
76
        $response->headers->set('Content-Type', 'text/xml');
77
78
        return $this->render(
79
            '@PiedWebCMS/page/rss.xml.twig',
80
            array_merge(['page' => $page], $this->app->getParamsForRendering()),
81
            $response
82
        );
83
    }
84
85
    /**
86
     * Show Last created page in an XML Feed.
87
     */
88
    public function showMainFeed(?string $host, Request $request)
89
    {
90
        $this->app->switchCurrentApp($host);
91
        $locale = $request->getLocale() ? rtrim($request->getLocale(), '/') : $this->params->get('locale');
92
        $LocaleHomepage = $this->getPage($locale, $host, $request, false);
93
        $slug = 'homepage';
94
        $page = $LocaleHomepage ? $LocaleHomepage : $this->getPage($slug, $host, $request);
95
96
        $params = [
97
            'pages' => $this->getPages(5, $request),
98
            'page' => $page,
99
            'feedUri' => ($this->params->get('locale') == $locale ? '' : $locale.'/').'feed.xml',
100
        ];
101
102
        return $this->render('@PiedWebCMS/page/rss.xml.twig', array_merge($params, $this->app->getParamsForRendering()));
103
    }
104
105
    public function showSitemap($_format, ?string $host, Request $request)
106
    {
107
        $this->app->switchCurrentApp($host);
108
        $pages = $this->getPages(null, $request);
109
110
        if (!$pages) {
111
            throw $this->createNotFoundException();
112
        }
113
114
        return $this->render('@PiedWebCMS/page/sitemap.'.$_format.'.twig', [
115
            'pages' => $pages,
116
            'app_base_url' => $this->app->getBaseUrl(),
117
        ]);
118
    }
119
120
    protected function getPages(?int $limit = null, Request $request)
121
    {
122
        $requestedLocale = rtrim($request->getLocale(), '/');
123
        //var_dump($requestedLocale);exit;
124
125
        $pages = $this->getPageRepository()->getIndexablePages(
126
                $this->app->getHost(),
127
                $this->app->isFirstApp(),
128
                $requestedLocale,
129
                $this->params->get('locale'),
130
                $limit
131
            )->getQuery()->getResult();
132
133
        //foreach ($pages as $page) echo $page->getMetaRobots().' '.$page->getTitle().'<br>';
134
        //exit('feed updated');
135
136
        return $pages;
137
    }
138
139
    /**
140
     * @return PageRepository
141
     */
142
    protected function getPageRepository()
143
    {
144
        return Repository::getPageRepository($this->getDoctrine(), $this->params->get('pwc.entity_page'));
145
    }
146
147
    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

147
    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...
148
    {
149
        $this->app->switchCurrentApp($host);
150
        $slug = $this->noramlizeSlug($slug);
151
        $page = $this->getPageRepository()->getPage($slug, $this->app->getHost(), $this->app->isFirstApp());
152
153
        // Check if page exist
154
        if (null === $page) {
155
            if ($throwException) {
156
                throw $this->createNotFoundException();
157
            } else {
158
                return null;
159
            }
160
        }
161
162
        if (!$page->getLocale()) { // avoid bc break
163
            $page->setLocale($this->params->get('pwc.locale'));
164
        }
165
166
        //if (null !== $request) { $request->setLocale($page->getLocale()); }
167
        $this->get('translator')->setLocale($page->getLocale());
168
169
        // Check if page is public
170
        if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_EDITOR')) {
171
            if ($throwException) {
172
                throw $this->createNotFoundException();
173
            } else {
174
                return null;
175
            }
176
        }
177
178
        return $page;
179
    }
180
181
    protected function noramlizeSlug($slug)
182
    {
183
        return (null === $slug || '' === $slug) ? 'homepage' : rtrim(strtolower($slug), '/');
184
    }
185
186
    protected function checkIfUriIsCanonical(Request $request, Page $page)
187
    {
188
        $real = $request->getRequestUri();
189
190
        $expected = 'homepage' == $page->getSlug() ?
191
            $this->get('piedweb.page_canonical')->generatePathForHomepage() :
192
            $this->get('piedweb.page_canonical')->generatePathForPage($page->getRealSlug());
193
194
        if ($real != $expected) {
195
            return [$request->getBasePath().$expected, 301];
196
        }
197
198
        return false;
199
    }
200
}
201