RegistrationSubscriber   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 1 Features 3
Metric Value
wmc 5
c 4
b 1
f 3
lcom 1
cbo 5
dl 0
loc 74
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setRouter() 0 4 1
A getSubscribedEvents() 0 7 1
A onRegistrationSuccess() 0 18 1
A onRegistrationCompleted() 0 13 1
1
<?php
2
3
namespace Tahoe\Bundle\MultiTenancyBundle\EventSubscriber;
4
5
use FOS\UserBundle\FOSUserEvents;
6
use FOS\UserBundle\Event\FilterUserResponseEvent;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use FOS\UserBundle\Event\FormEvent;
9
use Symfony\Component\Form\FormInterface;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12
use Tahoe\Bundle\MultiTenancyBundle\Manager\RegistrationManager;
13
use Tahoe\Bundle\MultiTenancyBundle\Service\TenantAwareRouter;
14
15
/**
16
 * Class RegistrationSubscriber
17
 *
18
 * Responsible for creating tenant during registration, it also add just created user as an tenant admin
19
 *
20
 * @author Konrad Podgórski <[email protected]>
21
 */
22
class RegistrationSubscriber implements EventSubscriberInterface, RegistrationSubscriberInterface
23
{
24
    /**
25
     * @var FormInterface
26
     */
27
    protected $_form;
28
29
    /**
30
     * @var RedirectResponse
31
     */
32
    protected $redirectResponse;
33
34
    protected $registrationManager;
35
36
    protected $tenantAwareRouter;
37
38
    function __construct(RegistrationManager $registrationManager)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
    {
40
        $this->registrationManager = $registrationManager;
41
    }
42
43
    public function setRouter($router)
44
    {
45
        $this->tenantAwareRouter = $router;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public static function getSubscribedEvents()
52
    {
53
        return array(
54
            FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
55
            FOSUserEvents::REGISTRATION_COMPLETED => 'onRegistrationCompleted',
56
        );
57
    }
58
59
    /**
60
     * Used only to get form reference because it's not available in the next event, onRegistrationCompleted
61
     * @param FormEvent $event
62
     */
63
    public function onRegistrationSuccess(FormEvent $event)
64
    {
65
        /**
66
         * Disclaimer: Subscriber does all it's magic in onRegistrationCompleted method,
67
         * however in onRegistrationCompleted we don't have access form (so we can get tenant name and subdomain)
68
         * and http response (so we can redirect user to his new tenant instance)
69
         *
70
         * That's why we are using other event that is fired before onRegistrationCompleted and we grab references to
71
         * form and response objects that will be used in that next event.
72
         */
73
74
        $this->_form = $event->getForm();
75
76
        // we get reference to the redirect response that will be used in another event
77
        $this->redirectResponse = new RedirectResponse('dummy');
78
        // FOS User Registration controller check if response is set in event, if so it will just use it.
79
        $event->setResponse($this->redirectResponse);
80
    }
81
82
    public function onRegistrationCompleted(FilterUserResponseEvent $event)
83
    {
84
        $user = $event->getUser();
85
        $tenantName = $this->_form->get('tenantName')->getData();
86
        $tenantSubdomain = $this->_form->get('tenantSubdomain')->getData();
87
88
        $tenant = $this->registrationManager->createTenant($user, $tenantName, $tenantSubdomain);
0 ignored issues
show
Documentation introduced by
$user is of type object<FOS\UserBundle\Model\UserInterface>, but the function expects a object<Tahoe\Bundle\Mult...ltiTenantUserInterface>.

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...
89
90
        // this referenced redirect response will be used
91
        $this->redirectResponse->setTargetUrl($this->tenantAwareRouter->generateUrl($tenant));
92
93
        unset($this->_form);
94
    }
95
}