Passed
Push — master ( 9fafbe...a40188 )
by Marcel
07:47
created

addSaasClientAfterSuccessfullRegistration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Fluxter\SaasProviderBundle\EventSubscriber;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Fluxter\SaasProviderBundle\Model\SaasClientUserInterface;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpFoundation\Session\SessionInterface;
9
use Symfony\Component\HttpKernel\Event\KernelEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
use Fluxter\SaasProviderBundle\Service\SaasClientService;
12
use Symfony\Component\HttpFoundation\Response;
13
14
class ClientSubscriber implements EventSubscriberInterface
15
{
16
    /** @var EntityManagerInterface */
17
    private $em;
18
19
    /** @var SessionInterface */
20
    private $session;
21
22
    /** @var SaasClientService */
23
    private $clientService;
24
25
    public function __construct(EntityManagerInterface $em, SessionInterface $session, SaasClientService $clientService)
26
    {
27
        $this->em = $em;
28
        $this->session = $session;
29
        $this->clientService = $clientService;
30
    }
31
32
    public static function getSubscribedEvents()
33
    {
34
        $events = [
35
            KernelEvents::REQUEST => ['checkSaasClient', 101],
36
        ];
37
38
        if (class_exists("FOS\UserBundle\FOSUserEvents")) {
39
            $events[\FOS\UserBundle\FOSUserEvents::REGISTRATION_SUCCESS] = 'addSaasClientAfterSuccessfullRegistration';
0 ignored issues
show
Bug introduced by
The type FOS\UserBundle\FOSUserEvents was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
        }
41
42
        return $events;
43
    }
44
45
    /**
46
     * Add the correct client after successfull registration
47
     *
48
     * @param \FOS\UserBundle\Event\FormEvent $event
0 ignored issues
show
Bug introduced by
The type FOS\UserBundle\Event\FormEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
49
     * @return void
50
     */
51
    public function addSaasClientAfterSuccessfullRegistration($event)
52
    {
53
        /** @var SaasClientUserInterface $user */
54
        $user = $event->getForm()->getData();
55
56
        // Todo get the default role by the saasclient
57
        $defaultRole = null;
58
        $client = $this->clientService->getCurrentClient();
59
60
        $user->addRole($client, $defaultRole);
0 ignored issues
show
Bug introduced by
$defaultRole of type null is incompatible with the type Fluxter\SaasProviderBund...ClientUserRoleInterface expected by parameter $role of Fluxter\SaasProviderBund...serInterface::addRole(). ( Ignorable by Annotation )

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

60
        $user->addRole($client, /** @scrutinizer ignore-type */ $defaultRole);
Loading history...
Bug introduced by
It seems like $client can also be of type null; however, parameter $client of Fluxter\SaasProviderBund...serInterface::addRole() does only seem to accept Fluxter\SaasProviderBund...del\SaasClientInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

60
        $user->addRole(/** @scrutinizer ignore-type */ $client, $defaultRole);
Loading history...
61
    }
62
63
    /**
64
     * Discovers and checks if the current saas client exists by the current domain
65
     * and sets the session variables and updates them if needed.
66
     *
67
     * @param KernelEvent $event
68
     * @return void
69
     */
70
    public function checkSaasClient(KernelEvent $event)
71
    {
72
        $current = $this->clientService->getCurrentClient();
73
        if ($current == null) {
74
            $event->setResponse(new Response('Not found!', 404));
0 ignored issues
show
Bug introduced by
The method setResponse() does not exist on Symfony\Component\HttpKernel\Event\KernelEvent. It seems like you code against a sub-type of Symfony\Component\HttpKernel\Event\KernelEvent such as Symfony\Component\HttpKe...ent\FilterResponseEvent or Symfony\Component\HttpKe...\Event\GetResponseEvent. ( Ignorable by Annotation )

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

74
            $event->/** @scrutinizer ignore-call */ setResponse(new Response('Not found!', 404));
Loading history...
75
            return;           
76
        }
77
    }
78
}
79