Completed
Push — master ( 8c4272...ef6d68 )
by Thomas Mauro
11s
created

ConfigurationFactory::__invoke()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
ccs 19
cts 19
cp 1
rs 8.439
cc 5
eloc 19
nc 12
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\SentryModule\Service;
6
7
use Facile\SentryModule\Exception\InvalidArgumentException;
8
use Facile\SentryModule\Options\Configuration;
9
use Facile\SentryModule\Options\ConfigurationInterface;
10
use Facile\SentryModule\Options\ErrorHandlerOptions;
11
use Facile\SentryModule\Options\StackTraceOptions;
12
use Facile\SentryModule\SendCallback\CallbackChain;
13
use Interop\Container\ContainerInterface;
14
15
/**
16
 * Class ConfigurationFactory.
17
 */
18
final class ConfigurationFactory
19
{
20
    /**
21
     * @param ContainerInterface $container
22
     * @return ConfigurationInterface
23
     */
24 3
    public function __invoke(ContainerInterface $container): ConfigurationInterface
25
    {
26
        /** @var array $config */
27 3
        $config = $container->get('config');
28
29 3
        $configuration = $config['facile']['sentry'] ?? [];
30 3
        $ravenOptions = $configuration['raven_options'] ?? [];
31
32 3
        if (! array_key_exists('logger', $ravenOptions)) {
33 3
            $ravenOptions['logger'] = 'SentryModule';
34
        }
35
36 3
        if (array_key_exists('send_callback', $ravenOptions)) {
37 1
            $ravenOptions['send_callback'] = $this->buildCallbackChain($container, $ravenOptions['send_callback']);
38
        }
39
40 3
        if (array_key_exists('transport', $ravenOptions)) {
41 1
            $transport = $ravenOptions['transport'];
42 1
            if (is_string($transport)) {
43 1
                $transport = $container->get($transport);
44
            }
45 1
            $ravenOptions['transport'] = $transport;
46
        }
47
48 3
        $configuration['raven_options'] = $ravenOptions;
49
50 3
        $configuration['error_handler_options'] = new ErrorHandlerOptions(
51 3
            $configuration['error_handler_options'] ?? []
52
        );
53
54 3
        $configuration['stack_trace_options'] = new StackTraceOptions(
55 3
            $configuration['stack_trace_options'] ?? []
56
        );
57
58 3
        return new Configuration($configuration);
59
    }
60
61
62
    /**
63
     * @param ContainerInterface $container
64
     * @param array|string|callable $callbackOptions
65
     *
66
     * @return CallbackChain
67
     * @throws \Facile\SentryModule\Exception\InvalidArgumentException
68
     */
69 1
    private function buildCallbackChain(ContainerInterface $container, $callbackOptions): CallbackChain
70
    {
71 1
        if (null === $callbackOptions) {
72
            return new CallbackChain();
73
        }
74
75 1
        $callbackOptions = (array) $callbackOptions;
76 1
        $callbacks = [];
77
78 1
        foreach ($callbackOptions as $callbackItem) {
79 1
            if (is_string($callbackItem) && $container->has($callbackItem)) {
80 1
                $callbackItem = $container->get($callbackItem);
81
            }
82 1
            if (! $this->isValidCallback($container, $callbackItem)) {
0 ignored issues
show
Documentation introduced by
$callbackItem is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
                throw new InvalidArgumentException('Invalid callback');
84
            }
85
86 1
            $callbacks[] = $callbackItem;
87
        }
88
89 1
        return new CallbackChain($callbacks);
90
    }
91
92
    /**
93
     * @param ContainerInterface $container
94
     * @param callable|string $callbackItem
95
     * @return bool
96
     * @throws \Facile\SentryModule\Exception\InvalidArgumentException
97
     */
98 1
    private function isValidCallback(ContainerInterface $container, $callbackItem): bool
99
    {
100 1
        return is_callable($callbackItem) || (is_string($callbackItem) && $container->has($callbackItem));
101
    }
102
}
103