Test Failed
Push — master ( bdaab5...176ee2 )
by Vladimir
06:36
created

LogServiceProvider::boot()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 1
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Foundation;
6
7
use Monolog\Logger;
8
use Monolog\ErrorHandler;
9
use Psr\Log\LoggerInterface;
10
use Monolog\Handler\HandlerInterface;
11
use League\Container\ServiceProvider\AbstractServiceProvider;
12
use League\Container\ServiceProvider\BootableServiceProviderInterface;
13
14
abstract class LogServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface
15
{
16
    protected $provides = [
17
        LoggerInterface::class,
18
    ];
19
20
    /**
21
     * Define handlers.
22
     *
23
     * @return HandlerInterface[]
24
     */
25
    abstract public function handlers(): array;
26
27
    /**
28
     * Method will be invoked on registration of a service provider implementing
29
     * this interface. Provides ability for eager loading of Service Providers.
30
     *
31
     * @return void
32
     *
33
     * @throws \Psr\Container\NotFoundExceptionInterface
34
     * @throws \Psr\Container\ContainerExceptionInterface
35
     * @throws \Exception
36
     * @throws \InvalidArgumentException
37
     */
38
    public function boot(): void
39
    {
40
        $this->container->share(LoggerInterface::class, function () {
41
            $logger = new Logger('FondBot');
42
43
            foreach ($this->handlers() as $handler) {
44
                $logger->pushHandler($handler);
45
            }
46
47
            ErrorHandler::register($logger);
48
49
            return $logger;
50
        });
51
52
        $this->container->share(Logger::class, $this->container->get(LoggerInterface::class));
53
    }
54
55
    /**
56
     * Use the register method to register items with the container via the
57
     * protected $this->container property or the `getContainer` method
58
     * from the ContainerAwareTrait.
59
     *
60
     * @return void
61
     */
62
    public function register(): void
63
    {
64
    }
65
}
66