ProfileController::editAction()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 20
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
3
namespace Application\Bundle\UserBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
9
use FOS\UserBundle\Model\UserInterface;
10
11
/**
12
 * Class ProfileController.
13
 */
14
class ProfileController extends Controller
15
{
16
    /**
17
     * Show the user.
18
     *
19
     * @return RedirectResponse
20
     */
21
    public function showAction()
22
    {
23
        $user = $this->container->get('security.token_storage')->getToken()->getUser();
24
        if (!is_object($user) || !$user instanceof UserInterface) {
25
            throw new AccessDeniedException('This user does not have access to this section.');
26
        }
27
28
        return new RedirectResponse($this->container->get('router')->generate('cabinet'));
29
    }
30
31
    /**
32
     * Edit the user.
33
     *
34
     * @return RedirectResponse|Response
35
     */
36
    public function editAction()
37
    {
38
        $user = $this->container->get('security.token_storage')->getToken()->getUser();
39
        if (!is_object($user) || !$user instanceof UserInterface) {
40
            throw new AccessDeniedException('This user does not have access to this section.');
41
        }
42
43
        $form = $this->container->get('fos_user.profile.form');
44
        $formHandler = $this->container->get('fos_user.profile.form.handler');
45
46
        $process = $formHandler->process($user);
47
        if ($process) {
48
            $this->setFlash('fos_user_success', 'profile.flash.updated');
49
50
            return new RedirectResponse($this->getRedirectionUrl($user));
51
        }
52
53
        return $this->container->get('templating')->renderResponse(
54
            'FOSUserBundle:Profile:edit.html.'.$this->container->getParameter('fos_user.template.engine'),
55
            ['profileForm' => $form->createView()]
56
        );
57
    }
58
59
    /**
60
     * Generate the redirection url when editing is completed.
61
     *
62
     * @param \FOS\UserBundle\Model\UserInterface $user
63
     *
64
     * @return string
65
     */
66
    protected function getRedirectionUrl(UserInterface $user)
0 ignored issues
show
Unused Code introduced by
The parameter $user 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

66
    protected function getRedirectionUrl(/** @scrutinizer ignore-unused */ UserInterface $user)

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...
67
    {
68
        return $this->container->get('router')->generate('fos_user_profile_show');
69
    }
70
71
    /**
72
     * @param string $action
73
     * @param string $value
74
     */
75
    protected function setFlash($action, $value)
76
    {
77
        $this->container->get('session')->getFlashBag()->set($action, $value);
78
    }
79
}
80