getLoggerIfEnabled()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

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