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

Recovery::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 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