Skip to content

Commit ff56b6f

Browse files
committed
Converting to using service ids, simply so anyone can create susbcribers for anything and control their own deps
1 parent 8664a89 commit ff56b6f

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

app/config/github.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
# listener services that you can opt into for a repository
2+
services:
3+
app.subscriber.issue_subscriber:
4+
class: AppBundle\Issues\IssueListener
5+
arguments:
6+
- '@app.status_api.default'
7+
18
parameters:
29
# defines symfony repository as default
310
default_repository: symfony/symfony
411

512
# point to the main symfony repositories
613
repositories:
714
symfony/symfony:
8-
listeners:
9-
- AppBundle\Listener\SymfonyIssueListener
15+
subscribers:
16+
- app.subscriber.issue_subscriber
1017
maintainers:
1118
- fabpot
1219
- webmozart
@@ -21,8 +28,8 @@ parameters:
2128
- javiereguiluz
2229
secret: change_me
2330
symfony/symfony-docs:
24-
listeners:
25-
- AppBundle\Listener\SymfonyDocsIssueListener
31+
subscribers:
32+
- app.subscriber.issue_subscriber
2633
maintainers:
2734
- weaverryan
2835
- WouterJ

app/config/services.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ services:
3232

3333
app.github.request_handler:
3434
class: AppBundle\Issues\GitHubRequestHandler
35-
arguments: ['@app.github.labels_api', '@event_dispatcher', '%repositories%']
35+
arguments: ['@app.github.labels_api', '@event_dispatcher', '%repositories%', '@service_container']
3636

3737
app.github.exception_listener:
38-
class: Appbundle\Listener\GitHubExceptionListener
38+
class: AppBundle\Listener\GitHubExceptionListener
3939
arguments: ['%kernel.debug%']
4040
tags:
4141
- { name: kernel.event_subscriber }

src/AppBundle/Issues/GitHubRequestHandler.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
use AppBundle\Issues\GitHub\CachedLabelsApi;
1313
use AppBundle\Issues\GitHub\GitHubStatusApi;
1414
use Github\Api\Issue\Labels;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
1516
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1617
use Symfony\Component\HttpFoundation\Request;
1718

1819
/**
19-
* Class GitHubRequestHandler.
20+
* Handles GitHub webhook requests
2021
*
2122
* @author Jules Pietri <jules@heahprod.com>
2223
*/
@@ -25,15 +26,21 @@ class GitHubRequestHandler
2526
private $labelsApi;
2627
private $dispatcher;
2728
private $repositories;
29+
/**
30+
* @var ContainerInterface
31+
*/
32+
private $container;
2833

29-
public function __construct(Labels $labelsApi, EventDispatcherInterface $dispatcher, array $repositories)
34+
public function __construct(Labels $labelsApi, EventDispatcherInterface $dispatcher, array $repositories, ContainerInterface $container)
3035
{
3136
$this->labelsApi = $labelsApi;
3237
$this->dispatcher = $dispatcher;
3338
$this->repositories = $repositories;
39+
$this->container = $container;
3440
}
3541

3642
/**
43+
* @param Request $request
3744
* @return array The response data
3845
*
3946
* @throws GitHubExceptionInterface When the request or the configuration are invalid
@@ -63,20 +70,12 @@ public function handle(Request $request)
6370
throw new GitHubAccessDeniedException('Invalid signature.');
6471
}
6572
}
66-
if (empty($config['listeners'])) {
67-
throw new GitHubInvalidConfigurationException('The repository "%s" has no listeners configured.');
73+
if (empty($config['subscribers'])) {
74+
throw new GitHubInvalidConfigurationException('The repository "%s" has no subscribers configured.');
6875
}
6976

70-
$api = new GitHubStatusApi(
71-
new CachedLabelsApi($this->labelsApi, $repository),
72-
$repository
73-
);
74-
foreach ($config['listeners'] as $listener) {
75-
if (!is_subclass_of($listener, __NAMESPACE__.'\IssueListener')) {
76-
throw new GitHubInvalidConfigurationException(sprintf('The listener "%s" must be an instance of "\AppBundle\Issues\IssueListener".', $listener));
77-
}
78-
79-
$this->dispatcher->addSubscriber(new $listener($api));
77+
foreach ($config['subscribers'] as $subscriberId) {
78+
$this->dispatcher->addSubscriber($this->container->get($subscriberId));
8079
}
8180

8281
$event = new GitHubEvent($data, $repository, $config['maintainers']);

0 commit comments

Comments
 (0)