Completed
Push — master ( 7c8813...b0bb77 )
by Alejandro
21s queued 11s
created

LoggerFactory::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 13
rs 10
ccs 8
cts 8
cp 1
cc 3
nc 4
nop 3
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\Logger;
5
6
use Cascade\Cascade;
7
use Interop\Container\ContainerInterface;
8
use Interop\Container\Exception\ContainerException;
9
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
10
use Zend\ServiceManager\Exception\ServiceNotFoundException;
11
use Zend\ServiceManager\Factory\FactoryInterface;
12
13
use function count;
14
use function explode;
15
16
class LoggerFactory implements FactoryInterface
17
{
18
    /**
19
     * Create an object
20
     *
21
     * @param  ContainerInterface $container
22
     * @param  string $requestedName
23
     * @param  null|array $options
24
     * @return object
25
     * @throws ServiceNotFoundException if unable to resolve the service.
26
     * @throws ServiceNotCreatedException if an exception is raised when
27
     *     creating a service.
28
     * @throws ContainerException if any other error occurs
29
     */
30 3
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
31
    {
32 3
        $config = $container->has('config') ? $container->get('config') : [];
33 3
        Cascade::fileConfig($config['logger'] ?? ['loggers' => []]);
0 ignored issues
show
Bug introduced by
It seems like $config['logger'] ?? array('loggers' => array()) can also be of type array<string,array>; however, parameter $resource of Cascade\Cascade::fileConfig() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        Cascade::fileConfig(/** @scrutinizer ignore-type */ $config['logger'] ?? ['loggers' => []]);
Loading history...
34
35
        // Compose requested logger name
36 3
        $loggerName = $options['logger_name'] ?? 'Logger';
37 3
        $nameParts = explode('_', $requestedName);
38 3
        if (count($nameParts) > 1) {
39 1
            $loggerName = $nameParts[1];
40
        }
41
42 3
        return Cascade::getLogger($loggerName);
43
    }
44
}
45