Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

SeoBundle/Controller/Admin/SettingsController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\SeoBundle\Controller\Admin;
4
5
use Kunstmaan\AdminBundle\Controller\BaseSettingsController;
6
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
7
use Kunstmaan\SeoBundle\Entity\Robots;
8
use Kunstmaan\SeoBundle\Form\RobotsType;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Routing\Annotation\Route;
13
14
class SettingsController extends BaseSettingsController
15
{
16
    /**
17
     * Generates the robots administration form and fills it with a default value if needed.
18
     *
19
     * @Route(path="/", name="KunstmaanSeoBundle_settings_robots")
20
     * @Template(template="@KunstmaanSeo/Admin/Settings/robotsSettings.html.twig")
21
     *
22
     * @return array|RedirectResponse
0 ignored issues
show
Consider making the return type a bit more specific; maybe use RedirectResponse|array<s...omponent\Form\FormView>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
23
     */
24
    public function robotsSettingsAction(Request $request)
25
    {
26
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
27
28
        $em = $this->getDoctrine()->getManager();
29
        $repo = $this->getDoctrine()->getRepository(Robots::class);
30
        $robot = $repo->findOneBy([]);
31
        $default = $this->container->getParameter('robots_default');
32
        $isSaved = true;
33
34
        if (!$robot) {
35
            $robot = new Robots();
36
        }
37
38
        if ($robot->getRobotsTxt() == null) {
39
            $robot->setRobotsTxt($default);
40
            $isSaved = false;
41
        }
42
43
        $form = $this->createForm(RobotsType::class, $robot, [
44
            'action' => $this->generateUrl('KunstmaanSeoBundle_settings_robots'),
45
        ]);
46
        if ($request->isMethod('POST')) {
47
            $form->handleRequest($request);
48
            if ($form->isSubmitted() && $form->isValid()) {
49
                $em->persist($robot);
50
                $em->flush();
51
52
                return new RedirectResponse($this->generateUrl('KunstmaanSeoBundle_settings_robots'));
53
            }
54
        }
55
56
        if (!$isSaved) {
57
            $this->addFlash(
58
                FlashTypes::WARNING,
59
                $this->container->get('translator')->trans('seo.robots.warning')
60
            );
61
        }
62
63
        return [
64
            'form' => $form->createView(),
65
        ];
66
    }
67
}
68