Completed
Push — master ( 739d01...5ed129 )
by Neomerx
02:14
created

getLoggerIfEnabled()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 1
crap 3
1
<?php namespace Limoncello\Passport\Package;
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\AccountManagerInterface;
20
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface;
21
use Limoncello\Contracts\Settings\SettingsProviderInterface;
22
use Limoncello\Passport\Authentication\AccountManager;
23
use Limoncello\Passport\Contracts\Authentication\PassportAccountManagerInterface;
24
use Limoncello\Passport\Contracts\Entities\DatabaseSchemeInterface;
25
use Limoncello\Passport\Contracts\PassportServerIntegrationInterface;
26
use Limoncello\Passport\Contracts\PassportServerInterface;
27
use Limoncello\Passport\Entities\DatabaseScheme;
28
use Limoncello\Passport\Package\PassportSettings as C;
29
use Limoncello\Passport\PassportServer;
30
use Psr\Container\ContainerInterface as PsrContainerInterface;
31
use Psr\Log\LoggerInterface;
32
33
/**
34
 * @package Limoncello\Passport
35
 *
36
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
37
 */
38
abstract class BasePassportContainerConfigurator
39
{
40
    /**
41
     * @inheritdoc
42
     */
43 4
    protected static function baseConfigureContainer(LimoncelloContainerInterface $container): void
44
    {
45 4
        $accountManager = null;
46 4
        $factory        = function (
47
            PsrContainerInterface $container
48
        ) use (&$accountManager): PassportAccountManagerInterface {
49 1
            if ($accountManager === null) {
50 1
                $accountManager = new AccountManager($container);
51 1
                if (($logger = static::getLoggerIfEnabled($container)) !== null) {
52 1
                    $accountManager->setLogger($logger);
53
                }
54
            }
55
56 1
            return $accountManager;
57 4
        };
58 4
        $container[AccountManagerInterface::class]         = $factory;
59 4
        $container[PassportAccountManagerInterface::class] = $factory;
60
61 2
        $container[DatabaseSchemeInterface::class] = function (
62
            PsrContainerInterface $container
63
        ): DatabaseSchemeInterface {
64 2
            $settings = $container->get(SettingsProviderInterface::class)->get(C::class);
65
66 2
            return new DatabaseScheme($settings[C::KEY_USER_TABLE_NAME], $settings[C::KEY_USER_PRIMARY_KEY_NAME]);
67
        };
68
69 1
        $container[PassportServerInterface::class] = function (
70
            PsrContainerInterface $container
71
        ): PassportServerInterface {
72 1
            $integration    = $container->get(PassportServerIntegrationInterface::class);
73 1
            $passportServer = new PassportServer($integration);
74
75 1
            if (($logger = static::getLoggerIfEnabled($container)) !== null) {
76 1
                $passportServer->setLogger($logger);
77
            }
78
79 1
            return $passportServer;
80
        };
81
    }
82
83
    /**
84
     * @param PsrContainerInterface $container
85
     *
86
     * @return null|LoggerInterface
87
     */
88 1
    protected static function getLoggerIfEnabled(PsrContainerInterface $container): ?LoggerInterface
89
    {
90 1
        $logger   = null;
91 1
        $settings = $container->get(SettingsProviderInterface::class)->get(C::class);
92 1
        if ($settings[C::KEY_IS_LOG_ENABLED] === true && $container->has(LoggerInterface::class) === true) {
93 1
            $logger = $container->get(LoggerInterface::class);
94
        }
95
96 1
        return $logger;
97
    }
98
}
99