Passed
Push — master ( 35232d...8d0b59 )
by Melech
06:16 queued 02:04
created

ServiceProvider::publishMonolog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 25
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Log\Provider;
15
16
use Monolog\Formatter\LineFormatter;
17
use Monolog\Handler\StreamHandler;
18
use Monolog\Logger as Monolog;
19
use Psr\Log\LoggerInterface;
20
use Valkyrja\Container\Contract\Container;
21
use Valkyrja\Container\Support\Provider;
22
use Valkyrja\Log\Contract\Logger;
23
use Valkyrja\Log\Enum\LogLevel;
24
use Valkyrja\Log\NullLogger;
25
use Valkyrja\Log\PsrLogger;
26
use Valkyrja\Support\Directory;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Support\Directory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
use function date;
29
30
/**
31
 * Class ServiceProvider.
32
 *
33
 * @author Melech Mizrachi
34
 */
35
final class ServiceProvider extends Provider
36
{
37
    /**
38
     * @inheritDoc
39
     */
40
    public static function publishers(): array
41
    {
42
        return [
43
            Logger::class          => [self::class, 'publishLogger'],
44
            PsrLogger::class       => [self::class, 'publishPsrLogger'],
45
            NullLogger::class      => [self::class, 'publishNullLogger'],
46
            LoggerInterface::class => [self::class, 'publishLoggerInterface'],
47
            Monolog::class         => [self::class, 'publishMonolog'],
48
        ];
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public static function provides(): array
55
    {
56
        return [
57
            Logger::class,
58
            NullLogger::class,
59
            PsrLogger::class,
60
            LoggerInterface::class,
61
            Monolog::class,
62
        ];
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public static function publish(Container $container): void
69
    {
70
    }
71
72
    /**
73
     * Publish the logger service.
74
     */
75
    public static function publishLogger(Container $container): void
76
    {
77
        $container->setSingleton(
78
            Logger::class,
79
            $container->getSingleton(PsrLogger::class),
80
        );
81
    }
82
83
    /**
84
     * Publish the psr adapter service.
85
     */
86
    public static function publishPsrLogger(Container $container): void
87
    {
88
        $container->setSingleton(
89
            PsrLogger::class,
90
            new PsrLogger(
91
                $container->getSingleton(LoggerInterface::class),
92
            ),
93
        );
94
    }
95
96
    /**
97
     * Publish the null adapter service.
98
     *
99
     * @param Container $container The container
100
     *
101
     * @return void
102
     */
103
    public static function publishNullLogger(Container $container): void
104
    {
105
        $container->setSingleton(
106
            NullLogger::class,
107
            new NullLogger(),
108
        );
109
    }
110
111
    /**
112
     * Publish the psr logger interface.
113
     */
114
    public static function publishLoggerInterface(Container $container): void
115
    {
116
        $container->setSingleton(
117
            LoggerInterface::class,
118
            $container->getSingleton(Monolog::class),
119
        );
120
    }
121
122
    /**
123
     * Publish the Monolog service.
124
     */
125
    public static function publishMonolog(Container $container): void
126
    {
127
        $filePath = Directory::logsStoragePath();
128
        $name     = 'valkyrja' . date('-Y-m-d');
129
130
        $handler = new StreamHandler(
131
            "$filePath/$name.log",
132
            LogLevel::DEBUG->name
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Valkyrja\Log\Enum\LogLevel.
Loading history...
133
        );
134
135
        $formatter = new LineFormatter(
136
            null,
137
            null,
138
            true,
139
            true
140
        );
141
142
        $handler->setFormatter($formatter);
143
144
        $container->setSingleton(
145
            Monolog::class,
146
            new Monolog(
147
                $name,
148
                [
149
                    $handler,
150
                ]
151
            )
152
        );
153
    }
154
}
155