Completed
Push — master ( d22c34...cd27e7 )
by Mārtiņš
02:11
created

Registration::verifyEmailIdentity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
crap 1
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\AccountNotFound;
13
14
use Palladium\Contract\CanCreateMapper;
15
use Palladium\Contract\HasId;
16
use Psr\Log\LoggerInterface;
17
18
class Registration
19
{
20
21
    private $mapperFactory;
22
    private $logger;
23
24
25 5
    public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger)
26
    {
27 5
        $this->mapperFactory = $mapperFactory;
28 5
        $this->logger = $logger;
29 5
    }
30
31
32
    /**
33
     * @param string $identifier
34
     * @param string $password
35
     */
36 2
    public function createEmailIdentity($identifier, $password)
37
    {
38 2
        $identity = new Entity\EmailIdentity;
39
40 2
        $identity->setIdentifier($identifier);
41 2
        $identity->setPassword($password);
42 2
        $identity->validate();
43
44 2
        $this->prepareNewIdentity($identity);
45
46 2
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
47
48 2
        if ($mapper->exists($identity)) {
49 1
            $this->logger->warning('email already registered', [
50
                'input' => [
51 1
                    'identifier' => $identifier,
52
                ],
53
            ]);
54
55 1
            throw new IdentityDuplicated;
56
        }
57
58 1
        $mapper->store($identity);
59
60 1
        return $identity;
61
    }
62
63
64 2
    private 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 bindAccountToIdentity(HasId $user, Entity\Identity $identity)
75
    {
76 2
        if ($user->getId() === null) {
77 1
            throw new AccountNotFound;
78
        }
79
80 1
        $identity->setAccountId($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
            'input' => [
87 1
            ],
88
            'user' => [
89 1
                'account' => $identity->getAccountId(),
90 1
                'identity' => $identity->getId(),
91
            ],
92
        ]);
93 1
    }
94
95
96 1
    public function verifyEmailIdentity(Entity\EmailIdentity $identity)
97
    {
98 1
        $identity->setStatus(Entity\Identity::STATUS_ACTIVE);
99 1
        $identity->clearToken();
100
101 1
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
102 1
        $mapper->store($identity);
103
104 1
        $this->logger->info('identity verified', [
105
            'input' => [
106 1
                'token' => $identity->getToken(),
107
            ],
108
            'user' => [
109 1
                'account' => $identity->getAccountId(),
110 1
                'identity' => $identity->getId(),
111
            ],
112
        ]);
113 1
    }
114
}
115