MonologFileContainerConfigurator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 7
dl 0
loc 47
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureContainer() 0 17 2
A createHandler() 0 13 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Packages\Monolog;
4
5
/**
6
 * Copyright 2015-2020 [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 Exception;
22
use Limoncello\Application\Packages\Monolog\MonologFileSettings as C;
23
use Limoncello\Contracts\Application\ApplicationConfigurationInterface as A;
24
use Limoncello\Contracts\Application\CacheSettingsProviderInterface;
25
use Limoncello\Contracts\Application\ContainerConfiguratorInterface;
26
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface;
27
use Monolog\Formatter\LineFormatter;
28
use Monolog\Handler\HandlerInterface;
29
use Monolog\Handler\NullHandler;
30
use Monolog\Handler\StreamHandler;
31
use Monolog\Logger;
32
use Monolog\Processor\UidProcessor;
33
use Monolog\Processor\WebProcessor;
34
use Psr\Container\ContainerInterface as PsrContainerInterface;
35
use Psr\Log\LoggerInterface;
36
use function array_key_exists;
37
use function assert;
38
39
/**
40
 * @package Limoncello\Application
41
 *
42
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
43
 */
44
class MonologFileContainerConfigurator implements ContainerConfiguratorInterface
45
{
46
    /** @var callable */
47
    const CONFIGURATOR = [self::class, self::CONTAINER_METHOD_NAME];
48 1
49
    /**
50
     * @inheritdoc
51
     */
52 1
    public static function configureContainer(LimoncelloContainerInterface $container): void
53 1
    {
54 1
        $container[LoggerInterface::class] = function (PsrContainerInterface $container) {
55
            /** @var CacheSettingsProviderInterface $settingsProvider */
56 1
            $settingsProvider = $container->get(CacheSettingsProviderInterface::class);
57 1
            $appConfig        = $settingsProvider->getApplicationConfiguration();
58 1
            $monologSettings  = $settingsProvider->get(C::class);
59
60 1
            $monolog = new Logger($appConfig[A::KEY_APP_NAME]);
61
            $handler = $monologSettings[C::KEY_IS_ENABLED] === true ?
62 1
                static::createHandler($monologSettings) : new NullHandler();
63
64
            $monolog->pushHandler($handler);
65
66
            return $monolog;
67
        };
68
    }
69
70
    /**
71
     * @param array $settings
72
     *
73 1
     * @return HandlerInterface
74
     *
75 1
     * @throws Exception
76
     */
77 1
    protected static function createHandler(array $settings): HandlerInterface
78 1
    {
79 1
        assert(array_key_exists(C::KEY_LOG_PATH, $settings) === true);
80 1
81 1
        $logPath  = $settings[C::KEY_LOG_PATH];
82 1
        $logLevel = $settings[C::KEY_LOG_LEVEL] ?? Logger::ERROR;
83
        $handler  = new StreamHandler($logPath, $logLevel);
84 1
        $handler->setFormatter(new LineFormatter(null, null, true, true));
85
        $handler->pushProcessor(new WebProcessor());
86
        $handler->pushProcessor(new UidProcessor());
87
88
        return $handler;
89
    }
90
}
91