Completed
Push — master ( 25598f...82bcfc )
by Mārtiņš
02:18
created

Registration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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
13
use Palladium\Contract\CanCreateMapper;
14
use Palladium\Contract\HasId;
15
use Psr\Log\LoggerInterface;
16
17
class Registration
18
{
19
20
    const DEFAULT_TOKEN_LIFESPAN = 28800; // 8 hours
21
22
    private $mapperFactory;
23
    private $logger;
24
25
    private $tokenLifespan;
26
27
    /**
28
     * @param Palladium\Contract\CanCreateMapper $mapperFactory Factory for creating persistence layer structures
29
     * @param Psr\Log\LoggerInterface $logger PSR-3 compatible logger
30
     * @param int $tokenLifespan Lifespan of the email verification token in seconds
31
     */
32 4
    public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger, $tokenLifespan = self::DEFAULT_TOKEN_LIFESPAN)
33
    {
34 4
        $this->mapperFactory = $mapperFactory;
35 4
        $this->logger = $logger;
36 4
        $this->tokenLifespan = $tokenLifespan;
37 4
    }
38
39
40
    /**
41
     * @param string $emailAddress
42
     * @param string $password
43
     *
44
     * @return Palladium\Entity\EmailIdentity
45
     */
46 2
    public function createEmailIdentity(string $emailAddress, string $password)
47
    {
48 2
        $identity = new Entity\EmailIdentity;
49
50 2
        $identity->setEmailAddress($emailAddress);
51 2
        $identity->setPassword($password);
52
53 2
        $this->prepareNewIdentity($identity);
54
55 2
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
56
57 2
        if ($mapper->exists($identity)) {
58 1
            $this->logger->notice('email already registered', [
59
                'input' => [
60 1
                    'email' => $emailAddress,
61
                ],
62
            ]);
63
64 1
            throw new IdentityDuplicated;
65
        }
66
67 1
        $mapper->store($identity);
68
69 1
        return $identity;
70
    }
71
72
73
    public function createOneTimeIdentity($accountId)
74
    {
75
        $identity = new Entity\OneTimeIdentity;
76
77
        $identity->setAccountId($accountId);
78
        $identity->generateNewNonce();
79
        $identity->generateNewKey();
80
81
        $mapper = $this->mapperFactory->create(Mapper\OneTimeIdentity::class);
82
        $mapper->store($identity);
83
84
        $this->logger->info('new single-use identity created', [
85
            'user' => [
86
                'account' => $identity->getAccountId(),
87
                'identity' => $identity->getId(),
88
            ],
89
        ]);
90
91
        return $identity;
92
    }
93
94
95
96 2
    private function prepareNewIdentity(Entity\EmailIdentity $identity)
97
    {
98 2
        $identity->setStatus(Entity\Identity::STATUS_NEW);
99
100 2
        $identity->generateToken();
101 2
        $identity->setTokenAction(Entity\Identity::ACTION_VERIFY);
102 2
        $identity->setTokenEndOfLife(time() + $this->tokenLifespan);
103 2
    }
104
105
106 1
    public function bindAccountToIdentity(int $accountId, Entity\Identity $identity)
107
    {
108 1
        $identity->setAccountId($accountId);
109
110 1
        $mapper = $this->mapperFactory->create(Mapper\IdentityAccount::class);
111 1
        $mapper->store($identity);
112
113 1
        $this->logger->info('new email identity registered', [
114
            'user' => [
115 1
                'account' => $identity->getAccountId(),
116 1
                'identity' => $identity->getId(),
117
            ],
118
        ]);
119 1
    }
120
121
122 1
    public function verifyEmailIdentity(Entity\EmailIdentity $identity)
123
    {
124 1
        $identity->setStatus(Entity\Identity::STATUS_ACTIVE);
125 1
        $identity->clearToken();
126
127 1
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
128 1
        $mapper->store($identity);
129
130 1
        $this->logger->info('identity verified', [
131
            'input' => [
132 1
                'token' => $identity->getToken(),
133
            ],
134
            'user' => [
135 1
                'account' => $identity->getAccountId(),
136 1
                'identity' => $identity->getId(),
137
            ],
138
        ]);
139 1
    }
140
}
141