Completed
Push — master ( 6b3e22...51e9c2 )
by Oleg
02:17
created

AppLoggerFactory::__invoke()   C

Complexity

Conditions 11
Paths 12

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
eloc 19
nc 12
nop 1
dl 0
loc 31
ccs 19
cts 19
cp 1
crap 11
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Logger;
5
6
use Monolog\Handler\HandlerInterface;
7
use Monolog\Handler\StreamHandler;
8
use Monolog\Logger;
9
use Psr\Container\ContainerInterface;
10
11
class AppLoggerFactory
12
{
13
    /**
14
     * @param ContainerInterface $container
15
     * @return Logger
16
     * @throws \Psr\Container\ContainerExceptionInterface
17
     * @throws \Psr\Container\NotFoundExceptionInterface
18
     * @throws \Exception
19
     */
20 34
    public function __invoke(ContainerInterface $container)
21
    {
22 34
        $config = $container->has('config') ? $container->get('config') : [];
23 34
        $loggerConfig = $config['logger'] ?? [];
24 34
        $handlersAdded = false;
25
26 34
        $logger = new Logger('app');
27
28 34
        if (!empty($loggerConfig)) {
29 34
            $handlers = $loggerConfig['handlers'] ?? [];
30 34
            if (!empty($handlers) && is_array($handlers)) {
31 34
                foreach ($handlers as $handler) {
32 34
                    if (is_object($handler) && $handler instanceof HandlerInterface) {
33 1
                        $logger->pushHandler($handler);
34 1
                        $handlersAdded = true;
35 33
                    } elseif (is_string($handler) && $container->has($handler)) {
36 32
                        $logger->pushHandler($container->get($handler));
37 34
                        $handlersAdded = true;
38
                    }
39
                }
40
            }
41
        }
42
43 34
        if (!$handlersAdded) {
44
            // add default handler
45 1
            $baseHandler = new StreamHandler('data/log/app.log');
46 1
            $logger->pushHandler($baseHandler);
47
        }
48
49 34
        return $logger;
50
    }
51
}
52