InvitationHandler   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 23.85 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
c 2
b 0
f 1
lcom 1
cbo 5
dl 26
loc 109
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A acceptInvitationById() 13 13 2
A acceptInvitation() 0 14 1
A delete() 0 8 2
A rejectInvitationById() 13 13 2
A rejectInvitation() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
4
namespace Tahoe\Bundle\MultiTenancyBundle\Handler;
5
6
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\EntityRepository;
9
use Tahoe\Bundle\MultiTenancyBundle\Factory\InvitationFactory;
10
use Tahoe\Bundle\MultiTenancyBundle\Model\InvitationInterface;
11
use Tahoe\Bundle\MultiTenancyBundle\Repository\InvitationRepository;
12
13
/**
14
 * Class InvitationHandler
15
 *
16
 * @author Konrad Podgórski <[email protected]>
17
 */
18
class InvitationHandler
19
{
20
21
    /**
22
     * @var EntityManager
23
     */
24
    protected $entityManager;
25
    /**
26
     * @var InvitationFactory
27
     */
28
    protected $invitationFactory;
29
    /**
30
     * @var InvitationRepository
31
     */
32
    protected $invitationRepository;
33
    /**
34
     * @var EntityRepository
35
     */
36
    protected $userRepository;
37
    /**
38
     * @var TenantUserHandler
39
     */
40
    protected $tenantUserHandler;
41
42
    /**
43
     * @param $entityManager
44
     * @param $invitationFactory
45
     * @param $invitationRepository
46
     * @param $tenantUserHandler
47
     * @param $userRepository
48
     */
49
    function __construct(
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...
50
        $entityManager,
51
        $invitationFactory,
52
        $invitationRepository,
53
        $tenantUserHandler,
54
        $userRepository
55
    ) {
56
        $this->entityManager = $entityManager;
57
        $this->invitationFactory = $invitationFactory;
58
        $this->invitationRepository = $invitationRepository;
59
        $this->tenantUserHandler = $tenantUserHandler;
60
        $this->userRepository = $userRepository;
61
    }
62
63 View Code Duplication
    public function acceptInvitationById($invitationId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        $invitation = $this->invitationRepository->find($invitationId);
66
67
        if ($invitation === null) {
68
            throw new \Exception(sprintf(
69
                'Invitation with id %d doesn\'t exist. Maybe it\'s from different tenant?',
70
                $invitationId
71
            ));
72
        }
73
74
        return $this->acceptInvitation($invitation);
75
    }
76
77
    /**
78
     * @param InvitationInterface $invitation
79
     *
80
     * @return \Tahoe\Bundle\MultiTenancyBundle\Entity\TenantUser
81
     */
82
    public function acceptInvitation(InvitationInterface $invitation)
83
    {
84
        $user = $this->userRepository->findOneBy(array('emailCanonical' => $invitation->getEmail()));
85
86
        $tenant = $invitation->getTenant();
87
88
        $tenantUser = $this->tenantUserHandler->addUserToTenant($user, $tenant);
0 ignored issues
show
Documentation introduced by
$user is of type object|null, 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->entityManager->flush();
91
92
        $this->delete($invitation, true);
93
94
        return $tenantUser;
95
    }
96
97
    public function delete($invitation, $withFlush = false)
98
    {
99
        $this->entityManager->remove($invitation);
100
101
        if ($withFlush) {
102
            $this->entityManager->flush();
103
        }
104
    }
105
106 View Code Duplication
    public function rejectInvitationById($invitationId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        $invitation = $this->invitationRepository->find($invitationId);
109
110
        if ($invitation === null) {
111
            throw new \Exception(sprintf(
112
                'Invitation with id %d doesn\'t exist. Maybe it\'s from different tenant?',
113
                $invitationId
114
            ));
115
        }
116
117
        return $this->rejectInvitation($invitation);
118
    }
119
120
    public function rejectInvitation(InvitationInterface $invitation)
121
    {
122
        $this->delete($invitation, true);
123
124
        return true;
125
    }
126
}