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); |
|
|
|
|
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
|
|
|
|
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: