Completed
Branch master (53fdb9)
by Mārtiņš
04:03 queued 01:53
created

SignUp   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 9.28 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 9
loc 97
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B createPasswordIdentity() 0 32 2
B bindIdentityToUser() 0 25 2
B verifyPasswordIdentity() 9 31 2

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
namespace Palladium\Service;
4
5
/**
6
 * Code for creating new identities
7
 */
8
9
use Palladium\Mapper\Authentication as Mapper;
10
use Palladium\Exception\IdentityDuplicated;
11
use Palladium\Exception\UserNotFound;
12
use Palladium\Exception\IdentityNotFound;
13
use Palladium\Exception\TokenNotFound;
14
15
16
class SignUp extends Locator
17
{
18
19
    public function createPasswordIdentity($identifier, $key)
20
    {
21
        $identity = new \Entity\Authentication\PasswordIdentity;
22
23
        $identity->setIdentifier($identifier);
24
        $identity->setKey($key);
25
        $identity->setStatus(\Entity\Authentication\Identity::STATUS_NEW);
26
27
        $identity->generateToken();
28
        $identity->setTokenAction(\Entity\Authentication\Identity::ACTION_VERIFY);
29
        $identity->setTokenEndOfLife(time() + \Entity\Authentication\Identity::TOKEN_LIFESPAN);
30
31
        $identity->validate();
32
33
        $mapper = $this->mapperFactory->create(Mapper\PasswordIdentity::class);
34
35
        if ($mapper->exists($identity)) {
36
            $this->logger->warning('email already registered', [
37
                'input' => [
38
                    'identifier' => $identifier,
39
                ],
40
            ]);
41
42
            throw new IdentityDuplicated;
43
        }
44
45
        $mapper->store($identity);
46
47
        // process not ended, no point in logging
48
49
        return $identity;
50
    }
51
52
53
    public function bindIdentityToUser(\Entity\Authentication\Identity $identity, \Entity\Community\User $user)
54
    {
55
        if ($user->getId() === null) {
56
            throw new UserNotFound;
57
        }
58
59
        $identity->setUserId($user->getId());
60
61
        $identity->validate();
62
63
        $mapper = $this->mapperFactory->create(Mapper\IdentityUser::class);
64
        $mapper->store($identity);
65
66
        $this->logger->info('new identity registered', [
67
            'input' => [
68
                'identifier' => $identity->getIdentifier(),
69
            ],
70
            'account' => [
71
                'user' => $identity->getUserId(),
72
                'identity' => $identity->getId(),
73
            ],
74
        ]);
75
76
        // @TODO: add mail later
77
    }
78
79
80
    public function verifyPasswordIdentity($token)
81
    {
82
        $identity = new \Entity\Authentication\PasswordIdentity;
83
        $this->retrieveIdenityByToken($identity, $token, \Entity\Authentication\Identity::ACTION_VERIFY);
84
85 View Code Duplication
        if ($identity->getId() === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
86
            $this->logger->warning('no identity with given verification token', [
87
                'input' => [
88
                    'token' => $token,
89
                ],
90
            ]);
91
92
            throw new TokenNotFound;
93
        }
94
95
        $identity->setStatus(\Entity\Authentication\Identity::STATUS_ACTIVE);
96
        $identity->clearToken();
97
98
        $mapper = $this->mapperFactory->create(Mapper\PasswordIdentity::class);
99
        $mapper->store($identity);
100
101
        $this->logger->info('identity verified', [
102
            'input' => [
103
                'token' => $token,
104
            ],
105
            'account' => [
106
                'user' => $identity->getUserId(),
107
                'identity' => $identity->getId(),
108
            ],
109
        ]);
110
    }
111
112
}
113