Completed
Push — master ( 801091...de4e4b )
by Mārtiņš
02:04
created

Registration::bindAccountToIdentity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

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