Completed
Push — develop ( 33eed9...a73e2c )
by Carsten
26s queued 21s
created

AcceptInvitationHandler   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 93.88%

Importance

Changes 0
Metric Value
wmc 18
eloc 51
dl 0
loc 148
ccs 46
cts 49
cp 0.9388
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setOrganizationRepository() 0 5 1
A setAuthenticationService() 0 5 1
B process() 0 47 9
A setUserRepository() 0 5 1
A getUserRepository() 0 7 2
A getOrganizationRepository() 0 7 2
A getAuthenticationService() 0 7 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Organizations\Controller\Plugin;
12
13
use Auth\AuthenticationService;
14
use Organizations\Repository\Organization as OrganizationRepository;
15
use Auth\Repository\User as UserRepository;
16
use Core\Exception\MissingDependencyException;
17
use Organizations\Entity\EmployeeInterface;
18
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
19
20
/**
21
 * ${CARET}
22
 *
23
 * @author Mathias Gelhausen <[email protected]>
24
 * @todo write test
25
 */
26
class AcceptInvitationHandler extends AbstractPlugin
27
{
28
    const ERROR_ORGANIZATION_NOT_FOUND = 'ErrorOrganizationNotFound';
29
    const ERROR_TOKEN_INVALID = 'ErrorTokenInvalid';
30
    const OK_SET_PW = 'OK_SetPw';
31
    const OK = 'OK';
32
33
    protected $organizationRepository;
34
    protected $userRepository;
35
    protected $authenticationService;
36
37
    /**
38
     * Sets the authentication service.
39
     *
40
     * @param AuthenticationService $authenticationService
41
     *
42
     * @return self
43
     */
44 6
    public function setAuthenticationService(AuthenticationService $authenticationService)
45
    {
46 6
        $this->authenticationService = $authenticationService;
47
48 6
        return $this;
49
    }
50
51
    /**
52
     * Gets the Authentication Service.
53
     *
54
     * @return AuthenticationService
55
     * @throws MissingDependencyException
56
     */
57 4
    public function getAuthenticationService()
58
    {
59 4
        if (!$this->authenticationService) {
60
            throw new MissingDependencyException('\Auth\AuthenticationService', $this);
61
        }
62
63 4
        return $this->authenticationService;
64
    }
65
66
    /**
67
     * Sets the organizations repository.
68
     *
69
     * @param OrganizationRepository $organizationRepository
70
     *
71
     * @return self
72
     */
73 6
    public function setOrganizationRepository(OrganizationRepository $organizationRepository)
74
    {
75 6
        $this->organizationRepository = $organizationRepository;
76
77 6
        return $this;
78
    }
79
80
    /**
81
     * Gets the organization repository
82
     *
83
     * @return OrganizationRepository
84
     * @throws MissingDependencyException
85
     */
86 6
    public function getOrganizationRepository()
87
    {
88 6
        if (!$this->organizationRepository) {
89
            throw new MissingDependencyException('\Organizations\Repository\Organization', $this);
90
        }
91
92 6
        return $this->organizationRepository;
93
    }
94
95
    /**
96
     * Sets the user repository.
97
     *
98
     * @param UserRepository $userRepository
99
     *
100
     * @return self
101
     */
102 5
    public function setUserRepository(UserRepository $userRepository)
103
    {
104 5
        $this->userRepository = $userRepository;
105
106 5
        return $this;
107
    }
108
109
    /**
110
     * Gets the user repository.
111
     *
112
     * @return UserRepository
113
     * @throws MissingDependencyException
114
     */
115 5
    public function getUserRepository()
116
    {
117 5
        if (!$this->userRepository) {
118
            throw new MissingDependencyException('\Auth\Repository\User', $this);
119
        }
120
121 5
        return $this->userRepository;
122
    }
123
124
125
126
127 5
    public function process($token, $organizationId)
128
    {
129 5
        $organizationRepository   = $this->getOrganizationRepository();
130 5
        $organization = $organizationRepository->find($organizationId); /* @var $organization \Organizations\Entity\OrganizationInterface */
131
132 5
        if (!$organization) {
0 ignored issues
show
introduced by
$organization is of type Organizations\Entity\OrganizationInterface, thus it always evaluated to true.
Loading history...
133 1
            return self::ERROR_ORGANIZATION_NOT_FOUND;
134
        }
135
136 4
        $userRepository = $this->getUserRepository();
137 4
        $user = $userRepository->findByToken($token); /* @var $user \Auth\Entity\User */
138
139 4
        if (!$user) {
0 ignored issues
show
introduced by
$user is of type Auth\Entity\User, thus it always evaluated to true.
Loading history...
140 1
            return self::ERROR_TOKEN_INVALID;
141
        }
142
143 3
        if ($user->isDraft()) {
144 1
            $user->setIsDraft(false);
145 1
            $user->getInfo()->setEmailVerified(true);
146 1
            $mustSetPassword = true;
147
        } else {
148 2
            $mustSetPassword = false;
149 2
            $userOrg = $user->getOrganization(); /* @var $userOrg \Organizations\Entity\OrganizationReference */
150 2
            if ($userOrg->hasAssociation()) {
151 1
                $userEmp = $userOrg->getEmployee($user->getId());
152 1
                $userEmp->setStatus(EmployeeInterface::STATUS_UNASSIGNED);
0 ignored issues
show
Bug introduced by
The method setStatus() does not exist on Organizations\Entity\OrganizationReference. ( Ignorable by Annotation )

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

152
                $userEmp->/** @scrutinizer ignore-call */ 
153
                          setStatus(EmployeeInterface::STATUS_UNASSIGNED);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
            }
154
        }
155
156 3
        $employee = $organization->getEmployee($user->getId());
157 3
        $employee->setStatus(EmployeeInterface::STATUS_ASSIGNED);
158
        
159
160 3
        foreach ($organizationRepository->findPendingOrganizationsByEmployee($user->getId()) as $pendingOrg) {
161
            /* @var $pendingOrg \Organizations\Entity\OrganizationInterface */
162 3
            if ($pendingOrg->getId() == $organization->getId()) {
163 3
                continue;
164
            }
165
166 3
            $pendingOrgEmp = $pendingOrg->getEmployee($user->getId());
167 3
            if (!$pendingOrgEmp->isUnassigned(/*strict*/ true)) {
0 ignored issues
show
Unused Code introduced by
The call to Organizations\Entity\Emp...terface::isUnassigned() has too many arguments starting with true. ( Ignorable by Annotation )

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

167
            if (!$pendingOrgEmp->/** @scrutinizer ignore-call */ isUnassigned(/*strict*/ true)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
168 3
                $pendingOrgEmp->setStatus(EmployeeInterface::STATUS_REJECTED);
169
            }
170
        }
171
172 3
        $this->getAuthenticationService()->getStorage()->write($user->getId());
173 3
        return $mustSetPassword ? self::OK_SET_PW : self::OK;
174
    }
175
}
176