Completed
Push — master ( 6a0ab6...96f47f )
by Mārtiņš
02:11
created

Registration::bindIdentityToUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 9.2
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\IdentityNotFound;
14
use Palladium\Exception\TokenNotFound;
15
16
use Palladium\Contract\CanCreateMapper;
17
use Palladium\Contract\HasId;
18
use Psr\Log\LoggerInterface;
19
20
class Registration
21
{
22
23
    private $mapperFactory;
24
    private $logger;
25
26
27 5
    public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger)
28
    {
29 5
        $this->mapperFactory = $mapperFactory;
30 5
        $this->logger = $logger;
31 5
    }
32
33
34 3
    public function createEmailIdentity($identifier, $password)
35
    {
36 3
        $identity = new Entity\EmailIdentity;
37
38 3
        $identity->setIdentifier($identifier);
39 3
        $identity->setPassword($password);
40
41 3
        $this->prepareNewIdentity($identity);
42
43 3
        $identity->validate();
44
45 3
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
46
47 3
        if ($mapper->exists($identity)) {
48 1
            $this->logger->warning('email already registered', [
49
                'input' => [
50 1
                    'identifier' => $identifier,
51
                ],
52
            ]);
53
54 1
            throw new IdentityDuplicated;
55
        }
56
57 2
        $mapper->store($identity);
58
59
        // process not ended, no point in logging
60
61 2
        return $identity;
62
    }
63
64
65 3
    public function prepareNewIdentity(Entity\EmailIdentity $identity)
66
    {
67 3
        $identity->setStatus(Entity\Identity::STATUS_NEW);
68
69 3
        $identity->generateToken();
70 3
        $identity->setTokenAction(Entity\Identity::ACTION_VERIFY);
71 3
        $identity->setTokenEndOfLife(time() + Entity\Identity::TOKEN_LIFESPAN);
72 3
    }
73
74
75 2
    public function bindIdentityToUser(Entity\Identity $identity, HasId $user)
76
    {
77 2
        if ($user->getId() === null) {
78 1
            throw new UserNotFound;
79
        }
80
81 1
        $identity->setUserId($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
            'input' => [
88 1
            ],
89
            'account' => [
90 1
                'user' => $identity->getUserId(),
91 1
                'identity' => $identity->getId(),
92
            ],
93
        ]);
94
95
        // @TODO: add mail later
96 1
    }
97
98
99 1
    public function verifyEmailIdentity($token)
100
    {
101 1
        $identity = new Entity\EmailIdentity;
102 1
        $this->retrieveIdenityByToken($identity, $token, Entity\Identity::ACTION_VERIFY);
103
104 1
        if ($identity->getId() === null) {
105
            $this->logger->warning('no identity with given verification token', [
106
                'input' => [
107
                    'token' => $token,
108
                ],
109
            ]);
110
111
            throw new TokenNotFound;
112
        }
113
114 1
        $identity->setStatus(Entity\Identity::STATUS_ACTIVE);
115 1
        $identity->clearToken();
116
117 1
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
118 1
        $mapper->store($identity);
119
120 1
        $this->logger->info('identity verified', [
121
            'input' => [
122 1
                'token' => $token,
123
            ],
124
            'account' => [
125 1
                'user' => $identity->getUserId(),
126 1
                'identity' => $identity->getId(),
127
            ],
128
        ]);
129
130 1
        return $identity;
131
    }
132
133
134 1 View Code Duplication
    private function retrieveIdenityByToken(Entity\Identity $identity, $token, $action = Entity\Identity::ACTION_ANY)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136 1
        $identity->setToken($token);
137 1
        $identity->setTokenAction($action);
138 1
        $identity->setTokenEndOfLife(time());
139
140 1
        $mapper = $this->mapperFactory->create(Mapper\Identity::class);
141 1
        $mapper->fetch($identity);
142
143 1
        return $identity;
144
    }
145
146
}
147