Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
EventRelayerGroup
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRelayer
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace Wikimedia\EventRelayer;
8
9use UnexpectedValueException;
10
11/**
12 * Factory class for spawning EventRelayer objects using configuration
13 *
14 * @since 1.27
15 */
16class EventRelayerGroup {
17    /** @var array[] */
18    protected $configByChannel = [];
19
20    /** @var EventRelayer[] */
21    protected $relayers = [];
22
23    /**
24     * @param array[] $config Channel configuration
25     */
26    public function __construct( array $config ) {
27        $this->configByChannel = $config;
28    }
29
30    /**
31     * @param string $channel
32     * @return EventRelayer Relayer instance that handles the given channel
33     */
34    public function getRelayer( $channel ) {
35        $channelKey = isset( $this->configByChannel[$channel] )
36            ? $channel
37            : 'default';
38
39        if ( !isset( $this->relayers[$channelKey] ) ) {
40            if ( !isset( $this->configByChannel[$channelKey] ) ) {
41                throw new UnexpectedValueException( "No config for '$channelKey'" );
42            }
43
44            $config = $this->configByChannel[$channelKey];
45            $class = $config['class'];
46
47            $this->relayers[$channelKey] = new $class( $config );
48        }
49
50        return $this->relayers[$channelKey];
51    }
52}
53
54/** @deprecated class alias since 1.41 */
55class_alias( EventRelayerGroup::class, 'EventRelayerGroup' );