TenantController::switchTenantAction()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 26
rs 8.8571
cc 2
eloc 14
nc 2
nop 1
1
<?php
2
3
4
namespace Tahoe\Bundle\MultiTenancyBundle\Controller;
5
6
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use Tahoe\Bundle\MultiTenancyBundle\Model\MultiTenantTenantInterface;
10
use Tahoe\Bundle\MultiTenancyBundle\Model\MultiTenantUserInterface;
11
use Tahoe\Bundle\MultiTenancyBundle\Service\TenantResolver;
12
13
class TenantController extends Controller
14
{
15
    public function switchDropdownAction()
16
    {
17
        $tenantRepository = $this->container->get('tahoe.multi_tenancy.tenant_user_repository');
18
19
        $tenant = $this->container->get('tahoe.multi_tenancy.tenant_resolver')->getTenant();
20
21
        $tenantUsers = $tenantRepository->findBy(
22
            array(
23
                'user' => $this->getUser()
24
            )
25
        );
26
27
        return $this->render(
28
            'TahoeMultiTenancyBundle:Tenant:_switch_dropdown.html.twig',
29
            array(
30
                'tenant' => $tenant,
31
                'tenantUsers' => $tenantUsers
32
            )
33
        );
34
    }
35
36
    public function switchTenantAction($subdomain)
37
    {
38
        /** @var MultiTenantUserInterface $user */
39
        $user = $this->getUser();
40
        $filters = $this->getDoctrine()->getManager()->getFilters();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getFilters() does only exist in the following implementations of said interface: Doctrine\ORM\Decorator\EntityManagerDecorator, Doctrine\ORM\EntityManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
41
42
        // for a moment we need to disable tenant aware filter to fetch invitations from all tenants
43
        $filters->disable("tenantAware");
44
        $tenant = $this->container->get('tenant_repository')->findOneBy(array(
45
                'subdomain' => $subdomain
46
            ));
47
        $filters->enable("tenantAware");
48
49
        if ($this->get('tahoe.multi_tenancy.tenant_resolver')->getStrategy() == TenantResolver::STRATEGY_FIXED_SUBDOMAIN) {
50
            // we update the tenant in the user and we redirect to the dashboard
51
            $user->setActiveTenant($tenant);
52
            $this->get('doctrine.orm.entity_manager')->flush();
53
54
            return $this->redirect($this->generateUrl('dashboard_index'));
55
        }
56
57
        $url = $this->container
58
            ->get('tahoe.multi_tenancy.tenant_aware_router')->generateUrl($tenant, 'dashboard_index');
59
60
        return $this->redirect($url);
61
    }
62
}
63