Completed
Push — develop ( 3db5f7...76d949 )
by Neomerx
04:11
created

AccountManager::getPassportSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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