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