Completed
Pull Request — master (#7)
by Thomas Mauro
05:04
created

ClientFactory::buildCallbackChain()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.027

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
ccs 10
cts 11
cp 0.9091
rs 8.6737
cc 6
eloc 11
nc 7
nop 2
crap 6.027
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\SentryModule\Service;
6
7
use Facile\SentryModule\Options\ClientOptions;
8
use Facile\SentryModule\Processor\SanitizeDataProcessor;
9
use Facile\SentryModule\SendCallback\CallbackChain;
10
use Interop\Container\ContainerInterface;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
13
/**
14
 * Class ClientFactory.
15
 */
16
final class ClientFactory extends AbstractFactory
17
{
18
    /**
19
     * @param ContainerInterface    $container
20
     * @param array|string|callable $callbackOptions
21
     *
22
     * @return CallbackChain
23
     */
24 1
    protected function buildCallbackChain(ContainerInterface $container, $callbackOptions): CallbackChain
25
    {
26 1
        $callbackChain = new CallbackChain();
27
28 1
        if (null === $callbackOptions) {
29
            return $callbackChain;
30
        }
31
32 1
        if (is_callable($callbackOptions) || is_string($callbackOptions)) {
33 1
            $callbackOptions = [$callbackOptions];
34
        }
35
36 1
        foreach ($callbackOptions as $callbackItem) {
37 1
            if (is_string($callbackItem)) {
38 1
                $callbackItem = $container->get($callbackItem);
39
            }
40
41 1
            $callbackChain->addCallback($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...
42
        }
43
44 1
        return $callbackChain;
45
    }
46
47
    /**
48
     * @param ContainerInterface $container
49
     * @param string             $requestedName
50
     * @param array|null         $options
51
     *
52
     * @return Client
53
     *
54
     * @throws \RuntimeException
55
     * @throws \Interop\Container\Exception\NotFoundException
56
     * @throws \Interop\Container\Exception\ContainerException
57
     */
58 2
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): Client
59
    {
60
        /** @var ClientOptions $clientOptions */
61 2
        $clientOptions = $this->getOptions($container, 'client');
62
63 2
        $ravenOptions = $clientOptions->getOptions();
64
65 2
        if (! array_key_exists('processors', $ravenOptions)) {
66 2
            $ravenOptions['processors'] = [SanitizeDataProcessor::class];
67
        }
68
69 2
        if (! array_key_exists('logger', $ravenOptions)) {
70 2
            $ravenOptions['logger'] = 'SentryModule';
71
        }
72
73 2
        if (array_key_exists('send_callback', $ravenOptions)) {
74 1
            $ravenOptions['send_callback'] = $this->buildCallbackChain($container, $ravenOptions['send_callback']);
75
        }
76
77 2
        if (array_key_exists('transport', $ravenOptions)) {
78
            $transport = $ravenOptions['transport'];
79
            if (is_string($transport)) {
80
                $transport = $container->get($transport);
81
            }
82
            $ravenOptions['transport'] = $transport;
83
        }
84
85 2
        $ravenClient = new \Raven_Client($clientOptions->getDsn(), $ravenOptions);
86
87 2
        $client = new Client($ravenClient, $clientOptions);
88
89 2
        $errorHandlerListener = $container->get($clientOptions->getErrorHandlerListener());
90 2
        if ($errorHandlerListener instanceof ClientAwareInterface) {
91 1
            $errorHandlerListener->setClient($client);
92
        }
93
        // @todo: find another way to handle this. This should be passed in the constructor.
94 2
        $client->setErrorHandlerListener($errorHandlerListener);
95
96 2
        return $client;
97
    }
98
99
    /**
100
     * @param ServiceLocatorInterface $serviceLocator
101
     *
102
     * @return Client
103
     */
104 1
    public function createService(ServiceLocatorInterface $serviceLocator): Client
105
    {
106 1
        return $this($serviceLocator, Client::class);
107
    }
108
109
    /**
110
     * Get the class name of the options associated with this factory.
111
     *
112
     *
113
     * @return string
114
     */
115 2
    public function getOptionsClass(): string
116
    {
117 2
        return ClientOptions::class;
118
    }
119
}
120