Passed
Push — master ( 53eeea...c4621c )
by Mārtiņš
02:52
created

Registration::createStandardIdentity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 9.0856
cc 2
eloc 12
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Palladium\Service;
4
5
/**
6
 * Code for creating new identities
7
 */
8
9
use Palladium\Entity as Entity;
10
use Palladium\Component\DataMapper;
11
use Palladium\Exception\IdentityConflict;
12
use Palladium\Repository\Identity as Repository;
13
use Psr\Log\LoggerInterface;
14
15
class Registration
16
{
17
18
    const DEFAULT_TOKEN_LIFESPAN = 28800; // 8 hours
19
    const DEFAULT_NONCE_LIFESPAN = 7200; // 2 hours
20
    const DEFAULT_HASH_COST = 12;
21
22
    private $repository;
23
    private $logger;
24
    private $hashCost;
25
26
    /**
27
     * @param Repository $repository Repository for abstracting persistence layer structures
28
     * @param LoggerInterface $logger PSR-3 compatible logger
29
     * @param int $hashCost Optional value for setting the cost of hashing algorythm (default: 12)
30
     */
31 5
    public function __construct(Repository $repository, DataMapper $accountMapper, LoggerInterface $logger, $hashCost = self::DEFAULT_HASH_COST)
32
    {
33 5
        $this->repository = $repository;
34 5
        $this->accountMapper = $accountMapper;
0 ignored issues
show
Bug Best Practice introduced by
The property accountMapper does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
35 5
        $this->logger = $logger;
36 5
        $this->hashCost = $hashCost;
37 5
    }
38
39
40 2
    public function createStandardIdentity(string $identifier, string $password, int $tokenLifespan = self::DEFAULT_TOKEN_LIFESPAN): Entity\StandardIdentity
41
    {
42 2
        $identity = new Entity\StandardIdentity;
43
44 2
        $identity->setIdentifier($identifier);
45 2
        $identity->setPassword($password, $this->hashCost);
46 2
        $identity->setTokenEndOfLife(time() + $tokenLifespan);
47
48 2
        $this->prepareNewIdentity($identity);
49
50 2
        if ($this->repository->has($identity)) {
51 1
            $this->logger->notice('identifier already registered', [
52
                'input' => [
53 1
                    'identifier' => $identifier,
54
                ],
55
            ]);
56
57 1
            throw new IdentityConflict;
58
        }
59
60 1
        $this->repository->save($identity);
61
62 1
        return $identity;
63
    }
64
65
66 1
    public function createNonceIdentity($accountId, $identityLifespan = self::DEFAULT_NONCE_LIFESPAN)
67
    {
68 1
        $identity = new Entity\NonceIdentity;
69
70 1
        $identity->setAccountId($accountId);
71 1
        $identity->setExpiresOn(time() + $identityLifespan);
72 1
        $identity->setStatus(Entity\Identity::STATUS_ACTIVE);
73 1
        $identity->generateNewNonce();
74 1
        $identity->generateNewKey($this->hashCost);
75
76 1
        $this->repository->save($identity);
77
78 1
        $this->logger->info('new single-use identity created', [
79
            'user' => [
80 1
                'account' => $identity->getAccountId(),
81 1
                'identity' => $identity->getId(),
82
            ],
83
        ]);
84
85 1
        return $identity;
86
    }
87
88
89 2
    private function prepareNewIdentity(Entity\StandardIdentity $identity)
90
    {
91 2
        $identity->setStatus(Entity\Identity::STATUS_NEW);
92 2
        $identity->generateToken();
93 2
        $identity->setTokenAction(Entity\Identity::ACTION_VERIFY);
94 2
    }
95
96
97 1
    public function bindAccountToIdentity(int $accountId, Entity\Identity $identity)
98
    {
99 1
        $identity->setAccountId($accountId);
100 1
        $this->accountMapper->store($identity);
0 ignored issues
show
Bug introduced by
The method store() does not exist on Palladium\Component\DataMapper. Since it exists in all sub-types, consider adding an abstract or default implementation to Palladium\Component\DataMapper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
        $this->accountMapper->/** @scrutinizer ignore-call */ 
101
                              store($identity);
Loading history...
101
102 1
        $this->logger->info('new identifier registered', [
103
            'user' => [
104 1
                'account' => $identity->getAccountId(),
105 1
                'identity' => $identity->getId(),
106
            ],
107
        ]);
108 1
    }
109
110
111 1
    public function verifyStandardIdentity(Entity\StandardIdentity $identity)
112
    {
113 1
        $identity->setStatus(Entity\Identity::STATUS_ACTIVE);
114 1
        $identity->clearToken();
115
116 1
        $this->repository->save($identity);
117
118 1
        $this->logger->info('identity verified', [
119
            'input' => [
120 1
                'token' => $identity->getToken(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $identity->getToken() targeting Palladium\Entity\Identity::getToken() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
121
            ],
122
            'user' => [
123 1
                'account' => $identity->getAccountId(),
124 1
                'identity' => $identity->getId(),
125
            ],
126
        ]);
127 1
    }
128
}
129