Completed
Push — master ( 6a0ab6...96f47f )
by Mārtiņš
02:11
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 3
    public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger)
25
    {
26 3
        $this->mapperFactory = $mapperFactory;
27 3
        $this->logger = $logger;
28 3
    }
29
30
31 2
    public function markForReset(Entity\EmailIdentity $identity)
32
    {
33 2
        if ($identity->getStatus() === Entity\Identity::STATUS_NEW) {
34 1
            $this->logger->warning('account not verified', [
35
                'input' => [
36 1
                    'identifier' => $identity->getIdentifier(),
37
                ],
38
                'account' => [
39 1
                    'user' => $identity->getUserId(),
40 1
                    'identity' => $identity->getId(),
41
                ],
42
            ]);
43
44 1
            throw new IdentityNotVerified;
45
        }
46
47 1
        $identity->generateToken();
48 1
        $identity->setTokenAction(Entity\Identity::ACTION_RESET);
49 1
        $identity->setTokenEndOfLife(time() + Entity\Identity::TOKEN_LIFESPAN);
50
51 1
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
52 1
        $mapper->store($identity);
53
54 1
        $this->logger->info('request password reset', [
55
            'input' => [
56 1
                'identifier' => $identity->getIdentifier(),
57
            ],
58
        ]);
59 1
    }
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