1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Joomla! Statistics Server |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\StatsServer\Providers; |
10
|
|
|
|
11
|
|
|
use Joomla\DI\Container; |
12
|
|
|
use Joomla\DI\ServiceProviderInterface; |
13
|
|
|
use Joomla\Event\Dispatcher; |
14
|
|
|
use Joomla\Event\DispatcherInterface; |
15
|
|
|
use Joomla\StatsServer\EventListener\ErrorSubscriber; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Event service provider |
20
|
|
|
*/ |
21
|
|
|
class EventServiceProvider implements ServiceProviderInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Registers the service provider with a DI container. |
25
|
|
|
* |
26
|
|
|
* @param Container $container The DI container. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
View Code Duplication |
public function register(Container $container): void |
|
|
|
|
31
|
|
|
{ |
32
|
12 |
|
$container->alias(Dispatcher::class, DispatcherInterface::class) |
33
|
|
|
->share(DispatcherInterface::class, [$this, 'getDispatcherService']); |
34
|
12 |
|
|
35
|
12 |
|
$container->share(ErrorSubscriber::class, [$this, 'getErrorSubscriberService']) |
36
|
|
|
->tag('event.subscriber', [ErrorSubscriber::class]); |
37
|
12 |
|
} |
38
|
12 |
|
|
39
|
|
|
/** |
40
|
12 |
|
* Get the DispatcherInterface service |
41
|
12 |
|
* |
42
|
12 |
|
* @param Container $container The DI container. |
43
|
|
|
* |
44
|
|
|
* @return DispatcherInterface |
45
|
|
|
*/ |
46
|
|
|
public function getDispatcherService(Container $container): DispatcherInterface |
47
|
|
|
{ |
48
|
|
|
$dispatcher = new Dispatcher; |
49
|
|
|
|
50
|
|
|
foreach ($container->getTagged('event.subscriber') as $subscriber) |
51
|
10 |
|
{ |
52
|
|
|
$dispatcher->addSubscriber($subscriber); |
53
|
10 |
|
} |
54
|
10 |
|
|
55
|
|
|
return $dispatcher; |
56
|
10 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the ErrorSubscriber service |
60
|
|
|
* |
61
|
|
|
* @param Container $container The DI container. |
62
|
|
|
* |
63
|
|
|
* @return ErrorSubscriber |
64
|
|
|
*/ |
65
|
|
|
public function getErrorSubscriberService(Container $container): ErrorSubscriber |
66
|
10 |
|
{ |
67
|
|
|
$subscriber = new ErrorSubscriber; |
68
|
10 |
|
$subscriber->setLogger($container->get(LoggerInterface::class)); |
69
|
|
|
|
70
|
10 |
|
return $subscriber; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.