Completed
Push — master ( ea5778...6a5914 )
by Paweł
12s
created

RegistrationController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 12
dl 0
loc 87
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B registerAction() 0 52 7
A ensureThatRegistrationIsEnabled() 0 12 2
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\FilterUserResponseEvent;
20
use FOS\UserBundle\Event\FormEvent;
21
use FOS\UserBundle\Event\GetResponseUserEvent;
22
use FOS\UserBundle\FOSUserEvents;
23
use FOS\UserBundle\Model\UserInterface;
24
use FOS\UserBundle\Model\UserManagerInterface;
25
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
27
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
28
use SWP\Bundle\UserBundle\Form\Type\RegistrationFormType;
29
use SWP\Component\Common\Response\ResponseContext;
30
use SWP\Component\Common\Response\SingleResourceResponse;
31
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
32
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
33
use Symfony\Component\HttpFoundation\Request;
34
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
35
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36
37
class RegistrationController extends Controller
38
{
39
    /**
40
     * Register new user.
41
     *
42
     * @ApiDoc(
43
     *     resource=true,
44
     *     description="Register new user",
45
     *     statusCodes={
46
     *         201="Returned on success.",
47
     *         400="Returned on failure.",
48
     *         409="Returned on conflict."
49
     *     },
50
     *     input="SWP\Bundle\UserBundle\Form\Type\RegistrationFormType"
51
     * )
52
     * @Route("/api/{version}/users/register/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_register_user")
53
     * @Method("POST")
54
     */
55
    public function registerAction(Request $request)
56
    {
57
        $this->ensureThatRegistrationIsEnabled();
58
59
        /** @var UserManagerInterface $userManager */
60
        $userManager = $this->get('fos_user.user_manager');
61
        /** @var $dispatcher EventDispatcherInterface */
62
        $dispatcher = $this->get('event_dispatcher');
63
        $user = $userManager->createUser();
64
        $user->setEnabled(true);
65
66
        $event = new GetResponseUserEvent($user, $request);
67
        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);
68
        if (null !== $event->getResponse()) {
69
            return $event->getResponse();
70
        }
71
72
        $form = $this->createForm(RegistrationFormType::class, $user);
73
        $form->handleRequest($request);
74
        if ($form->isValid()) {
75
            /** @var UserInterface $formData */
76
            $formData = $form->getData();
77
78
            if (null !== $userManager->findUserByEmail($formData->getEmail())) {
79
                throw new ConflictHttpException(sprintf('User with email "%s" already exists', $formData->getEmail()));
80
            }
81
82
            if (null !== $userManager->findUserByUsername($formData->getUsername())) {
83
                throw new ConflictHttpException(sprintf('User with username "%s" already exists', $formData->getUsername()));
84
            }
85
86
            $event = new FormEvent($form, $request);
87
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
88
89
            $this->get('swp.repository.user')->add($formData);
90
91
            if (null === ($response = $event->getResponse())) {
92
                return new SingleResourceResponse($formData, new ResponseContext(201));
93
            }
94
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
0 ignored issues
show
Documentation introduced by
$response is of type null, but the function expects a object<Symfony\Component\HttpFoundation\Response>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
96
            return $response;
97
        }
98
99
        $event = new FormEvent($form, $request);
100
        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE, $event);
101
        if (null !== $response = $event->getResponse()) {
102
            return $response;
103
        }
104
105
        return new SingleResourceResponse($form, new ResponseContext(400));
106
    }
107
108
    /**
109
     * @throws NotFoundHttpException
110
     */
111
    private function ensureThatRegistrationIsEnabled()
112
    {
113
        $settingName = 'registration_enabled';
114
        $settingsManager = $this->get('swp_settings.manager.settings');
115
        $scopeContext = $this->get('swp_settings.context.scope');
116
117
        $scope = $settingsManager->all()[$settingName]['scope'];
118
        $registrationEnabled = $settingsManager->get($settingName, $scope, $scopeContext->getScopeOwner($scope));
119
        if (!$registrationEnabled) {
120
            throw new NotFoundHttpException('Registration is disabled.');
121
        }
122
    }
123
}
124