Completed
Push — master ( 561bb0...6a0ab6 )
by Mārtiņš
05:13
created

Recovery   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 46.43%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 63
ccs 13
cts 28
cp 0.4643
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B markForReset() 0 29 2
A resetIdentityPassword() 0 16 1
1
<?php
2
3
namespace Palladium\Service;
4
5
/**
6
 * Application logic for password reset handling
7
 */
8
9
use Palladium\Mapper as Mapper;
10
use Palladium\Entity as Entity;
11
use Palladium\Exception\IdentityNotFound;
12
use Palladium\Exception\IdentityNotVerified;
13
14
use Palladium\Contract\CanCreateMapper;
15
use Psr\Log\LoggerInterface;
16
17
class Recovery
18
{
19
20
    protected $mapperFactory;
21
    protected $logger;
22
23
24 1
    public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger)
25
    {
26 1
        $this->mapperFactory = $mapperFactory;
27 1
        $this->logger = $logger;
28 1
    }
29
30
31
    public function markForReset(Entity\EmailIdentity $identity)
32
    {
33
        if ($identity->getStatus() === Entity\Identity::STATUS_NEW) {
34
            $this->logger->warning('account not verified', [
35
                'input' => [
36
                    'identifier' => $identifier,
0 ignored issues
show
Bug introduced by
The variable $identifier does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
37
                ],
38
                'account' => [
39
                    'user' => $identity->getUserId(),
40
                    'identity' => $identity->getId(),
41
                ],
42
            ]);
43
44
            throw new IdentityNotVerified;
45
        }
46
47
        $identity->generateToken();
48
        $identity->setTokenAction(Entity\Identity::ACTION_RESET);
49
        $identity->setTokenEndOfLife(time() + Entity\Identity::TOKEN_LIFESPAN);
50
51
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
52
        $mapper->store($identity);
53
54
        $this->logger->info('request password reset', [
55
            'input' => [
56
                'identifier' => $identifier,
57
            ],
58
        ]);
59
    }
60
61
62
63 1
    public function resetIdentityPassword(Entity\EmailIdentity $identity, $password)
64
    {
65 1
        $token = $identity->getToken();
66
67 1
        $identity->setPassword($password);
68 1
        $identity->clearToken();
69
70 1
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
71 1
        $mapper->store($identity);
72
73 1
        $this->logger->info('password reset successful', [
74
            'input' => [
75 1
                'token' => $token,
76
            ],
77
        ]);
78 1
    }
79
}
80