Completed
Push — master ( bb050e...36e41a )
by Paweł
11:10
created

ProfileController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 13
dl 0
loc 82
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAction() 0 9 3
B editAction() 0 39 8
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher User Bundle.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\UserBundle\Controller;
18
19
use FOS\UserBundle\Event\FormEvent;
20
use FOS\UserBundle\Event\GetResponseUserEvent;
21
use FOS\UserBundle\FOSUserEvents;
22
use FOS\UserBundle\Model\UserManagerInterface;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
24
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
25
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
26
use SWP\Bundle\UserBundle\Form\Type\ProfileFormType;
27
use SWP\Bundle\UserBundle\Model\UserInterface;
28
use SWP\Component\Common\Response\SingleResourceResponse;
29
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
30
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
33
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
34
use SWP\Component\Common\Response\ResponseContext;
35
36
class ProfileController extends Controller
37
{
38
    /**
39
     * Get user profile.
40
     *
41
     * @ApiDoc(
42
     *     resource=true,
43
     *     description="Get user profile",
44
     *     statusCodes={
45
     *         200="Returned on success.",
46
     *         404="Returned on user not found."
47
     *     }
48
     * )
49
     * @Route("/api/{version}/users/profile/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_user_get_user_profile")
50
     * @Method("GET")
51
     */
52
    public function getAction(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
    {
54
        $requestedUser = $this->container->get('swp.repository.user')->find($id);
55
        if (!is_object($requestedUser) || !$requestedUser instanceof UserInterface) {
56
            throw new NotFoundHttpException('Requested user don\'t exists');
57
        }
58
59
        return new SingleResourceResponse($requestedUser);
60
    }
61
62
    /**
63
     * Update user profile.
64
     *
65
     * @ApiDoc(
66
     *     resource=true,
67
     *     description="Update user profile",
68
     *     statusCodes={
69
     *         201="Returned on success.",
70
     *         400="Returned on failure.",
71
     *         404="Returned on user not found."
72
     *     },
73
     *     input="SWP\Bundle\UserBundle\Form\Type\ProfileFormType"
74
     * )
75
     * @Route("/api/{version}/users/profile/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_user_edit_user_profile")
76
     * @Method("PATCH")
77
     */
78
    public function editAction(Request $request, $id)
79
    {
80
        $requestedUser = $this->container->get('swp.repository.user')->find($id);
81
        if (!is_object($requestedUser) || !$requestedUser instanceof UserInterface) {
82
            throw new NotFoundHttpException('Requested user don\'t exists');
83
        }
84
85
        /** @var UserInterface $currentUser */
86
        $currentUser = $this->getUser();
87
        if (
88
            !$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN') &&
89
            $requestedUser->getId() !== $currentUser->getId()
90
        ) {
91
            throw new AccessDeniedException('This user does not have access to this section.');
92
        }
93
94
        /** @var $dispatcher EventDispatcherInterface */
95
        $dispatcher = $this->get('event_dispatcher');
96
        $event = new GetResponseUserEvent($requestedUser, $request);
97
        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
98
99
        if (null !== $event->getResponse()) {
100
            return $event->getResponse();
101
        }
102
103
        $form = $this->createForm(ProfileFormType::class, $requestedUser, ['method' => $request->getMethod()]);
104
        $form->handleRequest($request);
105
        if ($form->isSubmitted() && $form->isValid()) {
106
            /** @var $userManager UserManagerInterface */
107
            $userManager = $this->get('fos_user.user_manager');
108
            $event = new FormEvent($form, $request);
109
            $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
110
            $userManager->updateUser($requestedUser);
111
112
            return new SingleResourceResponse($requestedUser);
113
        }
114
115
        return new SingleResourceResponse($form, new ResponseContext(400));
116
    }
117
}
118