WebProcessorFactory::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 7
cp 0
rs 9.8333
cc 2
nc 2
nop 3
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasMonolog\Factory\Processor;
6
7
use Arp\LaminasFactory\AbstractFactory;
8
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
9
use Monolog\Processor\WebProcessor;
10
use Psr\Container\ContainerExceptionInterface;
11
use Psr\Container\ContainerInterface;
12
13
final class WebProcessorFactory extends AbstractFactory
14
{
15
    /**
16
     * @var array<string, string>
17
     */
18
    private array $defaultExtraFields = [
19
        'url'         => 'REQUEST_URI',
20
        'ip'          => 'REMOTE_ADDR',
21
        'http_method' => 'REQUEST_METHOD',
22
        'server'      => 'SERVER_NAME',
23
        'referrer'    => 'HTTP_REFERER',
24
    ];
25
26
    /**
27
     * @throws ServiceNotCreatedException
28
     * @throws ContainerExceptionInterface
29
     */
30
    public function __invoke(ContainerInterface $container, string $requestedName, array $options = null): WebProcessor
31
    {
32
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
33
34
        try {
35
            return new WebProcessor(
36
                $options['server_data'] ?? null,
37
                $options['extra_fields'] ?? $this->defaultExtraFields
38
            );
39
        } catch (\UnexpectedValueException $e) {
40
            throw new ServiceNotCreatedException(
41
                sprintf(
42
                    'Failed to create web processor \'%s\': %s',
43
                    $requestedName,
44
                    $e->getMessage()
45
                ),
46
                $e->getCode(),
47
                $e
48
            );
49
        }
50
    }
51
}
52