Completed
Push — master ( 96f47f...3b2885 )
by Mārtiņš
02:06
created

Registration   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 127
Duplicated Lines 8.66 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 59.65%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 11
loc 127
ccs 34
cts 57
cp 0.5965
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A retrieveIdenityByToken() 11 11 1
A __construct() 0 5 1
B createEmailIdentity() 0 29 2
A prepareNewIdentity() 0 8 1
A bindIdentityToUser() 0 22 2
B verifyEmailIdentity() 0 33 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 as Mapper;
10
use Palladium\Entity as Entity;
11
use Palladium\Exception\IdentityDuplicated;
12
use Palladium\Exception\UserNotFound;
13
use Palladium\Exception\IdentityNotFound;
14
use Palladium\Exception\TokenNotFound;
15
16
use Palladium\Contract\CanCreateMapper;
17
use Palladium\Contract\HasId;
18
use Psr\Log\LoggerInterface;
19
20
class Registration
21
{
22
23
    private $mapperFactory;
24
    private $logger;
25
26
27 4
    public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger)
28
    {
29 4
        $this->mapperFactory = $mapperFactory;
30 4
        $this->logger = $logger;
31 4
    }
32
33
34 2
    public function createEmailIdentity($identifier, $password)
35
    {
36 2
        $identity = new Entity\EmailIdentity;
37
38 2
        $identity->setIdentifier($identifier);
39 2
        $identity->setPassword($password);
40
41 2
        $this->prepareNewIdentity($identity);
42
43 2
        $identity->validate();
44
45 2
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
46
47 2
        if ($mapper->exists($identity)) {
48 1
            $this->logger->warning('email already registered', [
49
                'input' => [
50 1
                    'identifier' => $identifier,
51
                ],
52
            ]);
53
54 1
            throw new IdentityDuplicated;
55
        }
56
57 1
        $mapper->store($identity);
58
59
        // process not ended, no point in logging
60
61 1
        return $identity;
62
    }
63
64
65 2
    public function prepareNewIdentity(Entity\EmailIdentity $identity)
66
    {
67 2
        $identity->setStatus(Entity\Identity::STATUS_NEW);
68
69 2
        $identity->generateToken();
70 2
        $identity->setTokenAction(Entity\Identity::ACTION_VERIFY);
71 2
        $identity->setTokenEndOfLife(time() + Entity\Identity::TOKEN_LIFESPAN);
72 2
    }
73
74
75 2
    public function bindIdentityToUser(Entity\Identity $identity, HasId $user)
76
    {
77 2
        if ($user->getId() === null) {
78 1
            throw new UserNotFound;
79
        }
80
81 1
        $identity->setUserId($user->getId());
82
83 1
        $mapper = $this->mapperFactory->create(Mapper\IdentityUser::class);
84 1
        $mapper->store($identity);
85
86 1
        $this->logger->info('new identity registered', [
87
            'input' => [
88 1
            ],
89
            'account' => [
90 1
                'user' => $identity->getUserId(),
91 1
                'identity' => $identity->getId(),
92
            ],
93
        ]);
94
95
        // @TODO: add mail later
96 1
    }
97
98
99
    public function verifyEmailIdentity($token)
100
    {
101
        $identity = new Entity\EmailIdentity;
102
        $this->retrieveIdenityByToken($identity, $token, Entity\Identity::ACTION_VERIFY);
103
104
        if ($identity->getId() === null) {
105
            $this->logger->warning('no identity with given verification token', [
106
                'input' => [
107
                    'token' => $token,
108
                ],
109
            ]);
110
111
            throw new TokenNotFound;
112
        }
113
114
        $identity->setStatus(Entity\Identity::STATUS_ACTIVE);
115
        $identity->clearToken();
116
117
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
118
        $mapper->store($identity);
119
120
        $this->logger->info('identity verified', [
121
            'input' => [
122
                'token' => $token,
123
            ],
124
            'account' => [
125
                'user' => $identity->getUserId(),
126
                'identity' => $identity->getId(),
127
            ],
128
        ]);
129
130
        return $identity;
131
    }
132
133
134 View Code Duplication
    private function retrieveIdenityByToken(Entity\Identity $identity, $token, $action = Entity\Identity::ACTION_ANY)
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...
135
    {
136
        $identity->setToken($token);
137
        $identity->setTokenAction($action);
138
        $identity->setTokenEndOfLife(time());
139
140
        $mapper = $this->mapperFactory->create(Mapper\Identity::class);
141
        $mapper->fetch($identity);
142
143
        return $identity;
144
    }
145
146
}
147