BaseController::displaySuccess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the TheAlternativeZurich/events project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Controller\Base;
13
14
use App\Entity\User;
15
use App\Security\UserToken;
16
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
17
use Symfony\Component\HttpFoundation\Session\SessionInterface;
18
19
class BaseController extends AbstractController
20
{
21
    /**
22
     * @return User|null
23
     */
24
    protected function getUser()
25
    {
26
        $token = $this->container->get('security.token_storage')->getToken();
27
28
        if (!($token instanceof UserToken)) {
29
            return null;
30
        }
31
32
        $userRepository = $this->getDoctrine()->getRepository(User::class);
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

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

32
        $userRepository = /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getRepository(User::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
33
34
        return $userRepository->findOneBy(['email' => $token->getUser()]);
35
    }
36
37
    public static function getSubscribedServices()
38
    {
39
        return parent::getSubscribedServices() + ['session' => '?'.SessionInterface::class];
40
    }
41
42
    /**
43
     * @param string $message the translation message to display
44
     * @param string $link
45
     */
46
    protected function displayError($message, $link = null)
47
    {
48
        $this->displayFlash('danger', $message, $link);
49
    }
50
51
    /**
52
     * @param string $message the translation message to display
53
     * @param string $link
54
     */
55
    protected function displaySuccess($message, $link = null)
56
    {
57
        $this->displayFlash('success', $message, $link);
58
    }
59
60
    /**
61
     * @param string $message the translation message to display
62
     * @param string $link
63
     */
64
    protected function displayDanger($message, $link = null)
65
    {
66
        $this->displayFlash('danger', $message, $link);
67
    }
68
69
    /**
70
     * @param string $message the translation message to display
71
     * @param string $link
72
     */
73
    protected function displayInfo($message, $link = null)
74
    {
75
        $this->displayFlash('info', $message, $link);
76
    }
77
78
    /**
79
     * @param $type
80
     * @param $message
81
     * @param string $link
82
     */
83
    private function displayFlash($type, $message, $link = null)
84
    {
85
        if (null !== $link) {
86
            $message = '<a href="'.$link.'">'.$message.'</a>';
87
        }
88
        $this->get('session')->getFlashBag()->set($type, $message);
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...stractController::get() has been deprecated: since Symfony 5.4, use method or constructor injection in your controller instead ( Ignorable by Annotation )

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

88
        /** @scrutinizer ignore-deprecated */ $this->get('session')->getFlashBag()->set($type, $message);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
89
    }
90
}
91