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

Recovery   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 82
Duplicated Lines 12.2 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 10
loc 82
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B markForReset() 0 43 3
B resetIdentityPassword() 10 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
 * Application logic for password reset handling
7
 */
8
9
use Palladium\Mapper\Authentication as Mapper;
10
use Palladium\Entity\Authentication as Entity;
11
use Palladium\Exception\IdentityNotFound;
12
use Palladium\Exception\IdentityNotVerified;
13
14
class Recovery extends Locator
15
{
16
17
    public function markForReset($identifier)
18
    {
19
        $identity = $this->retrievePasswordIdenityByIdentifier($identifier);
20
21
        if ($identity->getId() === null) {
22
            $this->logger->warning('acount not found', [
23
                'input' => [
24
                    'identifier' => $identifier,
25
                ],
26
            ]);
27
28
            throw new IdentityNotFound;
29
        }
30
31
        if ($identity->getStatus() === Entity\Identity::STATUS_NEW) {
32
            $this->logger->warning('account not verified', [
33
                'input' => [
34
                    'identifier' => $identifier,
35
                ],
36
                'account' => [
37
                    'user' => $identity->getUserId(),
38
                    'identity' => $identity->getId(),
39
                ],
40
            ]);
41
42
            throw new IdentityNotVerified;
43
        }
44
45
        $identity->generateToken();
46
        $identity->setTokenAction(Entity\Identity::ACTION_RESET);
47
        $identity->setTokenEndOfLife(time() + Entity\Identity::TOKEN_LIFESPAN);
48
49
        $mapper = $this->mapperFactory->create(Mapper\PasswordIdentity::class);
50
        $mapper->store($identity);
51
52
        $this->logger->info('request password reset', [
53
            'input' => [
54
                'identifier' => $identifier,
55
            ],
56
        ]);
57
58
        // send email
59
    }
60
61
62
    public function resetIdentityPassword($token, $key)
63
    {
64
        $identity = new Entity\PasswordIdentity;
65
        $this->retrieveIdenityByToken($identity, $token, Entity\Identity::ACTION_RESET);
66
67 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...
68
            $this->logger->warning('no account with given reset token', [
69
                'input' => [
70
                    'token' => $token,
71
                    'key' => md5($key),
72
                ],
73
            ]);
74
75
            throw new IdentityNotFound;
76
        }
77
78
        $identity->setKey($key);
79
        $identity->clearToken();
80
81
82
        $mapper = $this->mapperFactory->create(Mapper\PasswordIdentity::class);
83
        $mapper->store($identity);
84
85
        $this->discardAllUserCookies($identity->getUserId());
86
87
        $this->logger->info('password reset successful', [
88
            'input' => [
89
                'token' => $token,
90
            ],
91
        ]);
92
    }
93
94
95
}
96