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

AdminBundle/Controller/DefaultController.php (2 issues)

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\AdminBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\AdminBundle\Entity\DashboardConfiguration;
7
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
8
use Kunstmaan\AdminBundle\Form\DashboardConfigurationType;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * The default controller is used to render the main screen the users see when they log in to the admin
17
 */
18
class DefaultController extends Controller
19
{
20
    /**
21
     * The index action will render the main screen the users see when they log in in to the admin
22
     *
23
     * @Route("/", name="KunstmaanAdminBundle_homepage")
24
     * @Template("@KunstmaanAdmin/Default/index.html.twig")
25
     *
26
     * @return array
0 ignored issues
show
Should the return type not be RedirectResponse|array<s...DashboardConfiguration>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
27
     */
28
    public function indexAction()
29
    {
30
        if ($this->container->hasParameter('kunstmaan_admin.dashboard_route')) {
31
            return $this->redirect($this->generateUrl($this->getParameter('kunstmaan_admin.dashboard_route')));
32
        }
33
34
        /* @var DashboardConfiguration $dashboardConfiguration */
35
        $dashboardConfiguration = $this->getDoctrine()
36
            ->getManager()
37
            ->getRepository(DashboardConfiguration::class)
38
            ->findOneBy([]);
39
40
        return ['dashboardConfiguration' => $dashboardConfiguration];
41
    }
42
43
    /**
44
     * The admin of the index page
45
     *
46
     * @Route("/adminindex", name="KunstmaanAdminBundle_homepage_admin")
47
     * @Template("@KunstmaanAdmin/Default/editIndex.html.twig")
48
     *
49
     * @return array
0 ignored issues
show
Should the return type not be RedirectResponse|array<s...DashboardConfiguration>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
50
     */
51
    public function editIndexAction(Request $request)
52
    {
53
        /* @var $em EntityManager */
54
        $em = $this->getDoctrine()->getManager();
55
56
        /* @var DashboardConfiguration $dashboardConfiguration */
57
        $dashboardConfiguration = $em
58
            ->getRepository(DashboardConfiguration::class)
59
            ->findOneBy([]);
60
61
        if (\is_null($dashboardConfiguration)) {
62
            $dashboardConfiguration = new DashboardConfiguration();
63
        }
64
        $form = $this->createForm(DashboardConfigurationType::class, $dashboardConfiguration);
65
66
        if ($request->isMethod('POST')) {
67
            $form->handleRequest($request);
68
            if ($form->isSubmitted() && $form->isValid()) {
69
                $em->persist($dashboardConfiguration);
70
                $em->flush($dashboardConfiguration);
71
72
                $this->addFlash(
73
                    FlashTypes::SUCCESS,
74
                    $this->get('translator')->trans('kuma_admin.edit.flash.success')
75
                );
76
77
                return new RedirectResponse($this->generateUrl('KunstmaanAdminBundle_homepage'));
78
            }
79
        }
80
81
        return [
82
            'form' => $form->createView(),
83
            'dashboardConfiguration' => $dashboardConfiguration,
84
        ];
85
    }
86
}
87