InvitationController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 4
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pendingAction() 0 13 1
B actionAction() 0 27 3
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
10
class InvitationController extends Controller
11
{
12
    public function pendingAction()
13
    {
14
        $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...
15
16
        // for a moment we need to disable tenant aware filter to fetch invitations from all tenants
17
        $filters->disable("tenantAware");
18
        $invitations = $this->container->get('invitation_repository')->findBy(array(
19
                'email' => $this->getUser()->getEmailCanonical()
20
            ));
21
        $filters->enable("tenantAware");
22
23
        return $this->render('@TahoeMultiTenancy/Invitation/index.html.twig', array('invitations' => $invitations));
24
    }
25
26
    public function actionAction(Request $request)
27
    {
28
29
        $action = $request->get('invitation');
30
31
        $invitationHandler = $this->container->get('tahoe.multi_tenancy.invitation_handler');
32
33
        $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...
34
35
        // for a moment we need to disable tenant aware filter to fetch invitations from all tenants
36
        $filters->disable("tenantAware");
37
38
        if (isset($action['accept'])) {
39
            $invitationId = $action['accept'];
40
            $invitationHandler->acceptInvitationById($invitationId);
41
42
        }
43
44
        if (isset($action['reject'])) {
45
            $invitationId = $action['reject'];
46
            $invitationHandler->rejectInvitationById($invitationId);
47
        }
48
        $filters->enable("tenantAware");
49
50
        return $this->redirect($this->generateUrl('pending_invitation'));
51
52
    }
53
}