AccountManager   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 91
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAccount() 0 4 1
A getPassport() 0 4 1
A setAccount() 0 8 1
A setAccountWithTokenValue() 0 19 3
A getContainer() 0 4 1
A getPassportSettings() 0 6 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Passport\Authentication;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Contracts\Authentication\AccountInterface;
22
use Limoncello\Contracts\Authentication\AccountManagerInterface;
23
use Limoncello\Contracts\Passport\PassportAccountInterface;
24
use Limoncello\Contracts\Passport\PassportAccountManagerInterface;
25
use Limoncello\Contracts\Settings\SettingsProviderInterface;
26
use Limoncello\Passport\Contracts\Entities\DatabaseSchemaInterface;
27
use Limoncello\Passport\Contracts\Repositories\TokenRepositoryInterface;
28
use Limoncello\Passport\Exceptions\AuthenticationException;
29
use Psr\Container\ContainerInterface;
30
use Limoncello\Passport\Package\PassportSettings as S;
31
use Psr\Log\LoggerAwareTrait;
32
use function assert;
33
34
/**
35
 * @package Limoncello\Passport
36
 */
37
class AccountManager implements PassportAccountManagerInterface
38
{
39
    use LoggerAwareTrait;
40
41
    /**
42
     * @var ContainerInterface
43
     */
44
    private $container;
45
46
    /**
47
     * @var null|PassportAccountInterface
48
     */
49
    private $account = null;
50
51
    /**
52
     * @param ContainerInterface $container
53
     */
54 4
    public function __construct(ContainerInterface $container)
55
    {
56 4
        $this->container = $container;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 1
    public function getAccount(): ?AccountInterface
63
    {
64 1
        return $this->getPassport();
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 1
    public function getPassport(): ?PassportAccountInterface
71
    {
72 1
        return $this->account;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 2
    public function setAccount(AccountInterface $account): AccountManagerInterface
79
    {
80 2
        assert($account instanceof PassportAccountInterface);
81
82 2
        $this->account = $account;
83
84 2
        return $this;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 2
    public function setAccountWithTokenValue(string $value): PassportAccountInterface
91
    {
92
        /** @var TokenRepositoryInterface $tokenRepo */
93 2
        $tokenRepo    = $this->getContainer()->get(TokenRepositoryInterface::class);
94 2
        $expInSeconds = $this->getPassportSettings()[S::KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS];
95 2
        $properties   = $tokenRepo->readPassport($value, $expInSeconds);
96 2
        if ($properties === null) {
97 1
            throw new AuthenticationException();
98
        }
99
100
        /** @var DatabaseSchemaInterface $schema */
101 1
        $schema  = $this->getContainer()->get(DatabaseSchemaInterface::class);
102 1
        $account = new PassportAccount($schema, $properties);
103 1
        $this->setAccount($account);
104
105 1
        $this->logger === null ?: $this->logger->info('Passport account is set for a given token value.');
106
107 1
        return $account;
108
    }
109
110
    /**
111
     * @return ContainerInterface
112
     */
113 2
    protected function getContainer(): ContainerInterface
114
    {
115 2
        return $this->container;
116
    }
117
118
    /**
119
     * @return array
120
     */
121 2
    protected function getPassportSettings(): array
122
    {
123 2
        $settings = $this->getContainer()->get(SettingsProviderInterface::class)->get(S::class);
124
125 2
        return $settings;
126
    }
127
}
128