Passed
Pull Request — master (#284)
by Alejandro
03:54
created

AccessLogFactory::getLogger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\Logger\Swoole;
5
6
use Interop\Container\ContainerInterface;
7
use Psr\Log\LoggerInterface;
8
use Zend\Expressive\Swoole\Log;
9
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
10
use Zend\ServiceManager\Exception\ServiceNotFoundException;
11
use Zend\ServiceManager\Factory\FactoryInterface;
12
13
class AccessLogFactory implements FactoryInterface
14
{
15
    /**
16
     * Create an object
17
     *
18
     * @param  ContainerInterface $container
19
     * @param  string $requestedName
20
     * @param  null|array $options
21
     * @return object
22
     * @throws ServiceNotFoundException if unable to resolve the service.
23
     * @throws ServiceNotCreatedException if an exception is raised when creating a service.
24
     */
25 8
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
26
    {
27 8
        $config = $container->has('config') ? $container->get('config') : [];
28 8
        $config = $config['zend-expressive-swoole']['swoole-http-server']['logger'] ?? [];
29
30 8
        return new Log\Psr3AccessLogDecorator(
31 8
            $this->getLogger($container, $config),
32 8
            $this->getFormatter($container, $config),
33 8
            $config['use-hostname-lookups'] ?? false
34
        );
35
    }
36
37 8
    private function getLogger(ContainerInterface $container, array $config): LoggerInterface
38
    {
39 8
        $loggerName = $config['logger_name'] ?? LoggerInterface::class;
40 8
        return $container->has($loggerName) ? $container->get($loggerName) : new Log\StdoutLogger();
41
    }
42
43 8
    private function getFormatter(ContainerInterface $container, array $config): Log\AccessLogFormatterInterface
44
    {
45 8
        if ($container->has(Log\AccessLogFormatterInterface::class)) {
46 2
            return $container->get(Log\AccessLogFormatterInterface::class);
47
        }
48
49 6
        return new Log\AccessLogFormatter($config['format'] ?? Log\AccessLogFormatter::FORMAT_COMMON);
50
    }
51
}
52