Skip to content

Commit 3fd5e39

Browse files
author
Sam Stevens
committed
refactor to register the callabcks directly on the bugsnag service, rather than through the factory
1 parent a25770e commit 3fd5e39

File tree

3 files changed

+14
-45
lines changed

3 files changed

+14
-45
lines changed

DependencyInjection/ClientFactory.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,6 @@ class ClientFactory
114114
*/
115115
protected $filters;
116116

117-
/**
118-
* Custom callbacks to register on the client.
119-
*
120-
* @var callable[]
121-
*/
122-
protected $customCallbacks = [];
123-
124117
/**
125118
* Create a new client factory instance.
126119
*
@@ -176,26 +169,6 @@ public function __construct(
176169
$this->filters = $filters;
177170
}
178171

179-
/**
180-
* Add a custom callback to be registered on the client.
181-
*
182-
* @param callable|array $callable
183-
*/
184-
public function addCallback($callable)
185-
{
186-
// If the parameter is not a callable (if the method name does not exist on the class for example),
187-
// then throw an exception informing the user
188-
if (!is_callable($callable)) {
189-
throw new \RuntimeException(sprintf(
190-
'Could not add Bugsnag callback. Class %s does not contain the "%s" method.',
191-
get_class($callable[0]),
192-
$callable[1]
193-
));
194-
}
195-
196-
$this->customCallbacks[] = $callable;
197-
}
198-
199172
/**
200173
* Make a new client instance.
201174
*
@@ -237,10 +210,6 @@ public function make()
237210
$client->setFilters($this->filters);
238211
}
239212

240-
foreach ($this->customCallbacks as $callable) {
241-
$client->registerCallback($callable);
242-
}
243-
244213
return $client;
245214
}
246215

DependencyInjection/Compiler/CallbackRegisteringPass.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@
88

99
class CallbackRegisteringPass implements CompilerPassInterface
1010
{
11-
const FACTORY_SERVICE_NAME = 'bugsnag.factory';
11+
const BUGSNAG_SERVICE_NAME = 'bugsnag';
1212
const TAG_NAME = 'bugsnag.callback';
1313

1414
/**
1515
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
1616
*/
1717
public function process(ContainerBuilder $container)
1818
{
19-
if (!$container->has(self::FACTORY_SERVICE_NAME)) {
19+
if (!$container->has(self::BUGSNAG_SERVICE_NAME)) {
2020
return;
2121
}
2222

23-
// Get the Bugsnag factory service
24-
$bugsnagFactory = $container->findDefinition(self::FACTORY_SERVICE_NAME);
23+
// Get the Bugsnag service
24+
$bugsnag = $container->findDefinition(self::BUGSNAG_SERVICE_NAME);
2525

2626
// Get all services tagged as a callback
2727
$callbackServices = $container->findTaggedServiceIds(self::TAG_NAME);
2828

29-
// Add each callback to the factory service via an addCallback call
29+
// Register each callback on the bugsnag service
3030
foreach ($callbackServices as $id => $tags) {
3131
foreach ($tags as $attributes) {
3232
// Get the method name to call on the service from the tag definition,
3333
// defaulting to registerCallback
3434
$method = isset($attributes['method']) ? $attributes['method'] : 'registerCallback';
35-
$bugsnagFactory->addMethodCall('addCallback', [[new Reference($id), $method]]);
35+
$bugsnag->addMethodCall('registerCallback', [[new Reference($id), $method]]);
3636
}
3737
}
3838
}

Tests/DependencyInjection/Compiler/CallbackRegisteringPassTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Bugsnag\BugsnagBundle\Tests\DependencyInjection\Compiler;
44

5-
use Bugsnag\BugsnagBundle\DependencyInjection\ClientFactory;
65
use Bugsnag\BugsnagBundle\DependencyInjection\Compiler\CallbackRegisteringPass;
6+
use Bugsnag\Client;
77
use PHPUnit\Framework\TestCase;
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
99
use Symfony\Component\DependencyInjection\Definition;
@@ -15,8 +15,8 @@ public function testTaggedCallbacksAreAddedToFactoryServiceDefinition()
1515
{
1616
$containerBuilder = new ContainerBuilder();
1717

18-
$factory = new Definition(ClientFactory::class);
19-
$containerBuilder->setDefinition(CallbackRegisteringPass::FACTORY_SERVICE_NAME, $factory);
18+
$bugsnag = new Definition(Client::class);
19+
$containerBuilder->setDefinition(CallbackRegisteringPass::BUGSNAG_SERVICE_NAME, $bugsnag);
2020

2121
$taggedCallbackOne = new Definition();
2222
$taggedCallbackOne->addTag(CallbackRegisteringPass::TAG_NAME);
@@ -29,10 +29,10 @@ public function testTaggedCallbacksAreAddedToFactoryServiceDefinition()
2929
$pass = new CallbackRegisteringPass();
3030
$pass->process($containerBuilder);
3131

32-
$this->assertSame(2, count($factory->getMethodCalls()));
33-
$this->assertSame('addCallback', $factory->getMethodCalls()[0][0]);
34-
$this->assertEquals([new Reference('callback_1'), 'registerCallback'], $factory->getMethodCalls()[0][1][0]);
35-
$this->assertSame('addCallback', $factory->getMethodCalls()[1][0]);
36-
$this->assertEquals([new Reference('callback_2'), 'customMethod'], $factory->getMethodCalls()[1][1][0]);
32+
$this->assertSame(2, count($bugsnag->getMethodCalls()));
33+
$this->assertSame('registerCallback', $bugsnag->getMethodCalls()[0][0]);
34+
$this->assertEquals([new Reference('callback_1'), 'registerCallback'], $bugsnag->getMethodCalls()[0][1][0]);
35+
$this->assertSame('registerCallback', $bugsnag->getMethodCalls()[1][0]);
36+
$this->assertEquals([new Reference('callback_2'), 'customMethod'], $bugsnag->getMethodCalls()[1][1][0]);
3737
}
3838
}

0 commit comments

Comments
 (0)