Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
StreamConfigsFactory
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getInstance
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\EventStreamConfig;
4
5use MediaWiki\Config\ServiceOptions;
6use MediaWiki\Extension\EventStreamConfig\Hooks\HookRunner;
7use Psr\Log\LoggerInterface;
8
9/**
10 * @internal
11 */
12class StreamConfigsFactory {
13
14    /**
15     * Name of the main config key(s) for stream configuration.
16     *
17     * @var array
18     */
19    public const CONSTRUCTOR_OPTIONS = [
20        'EventStreams',
21        'EventStreamsDefaultSettings',
22    ];
23
24    private ServiceOptions $options;
25    private HookRunner $hookRunner;
26    private LoggerInterface $logger;
27
28    public function __construct(
29        ServiceOptions $options,
30        HookRunner $hookRunner,
31        LoggerInterface $logger
32    ) {
33        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
34
35        $this->options = $options;
36        $this->hookRunner = $hookRunner;
37        $this->logger = $logger;
38    }
39
40    /**
41     * Create a new {@link StreamConfigs} instance.
42     *
43     * Stream configs are fetched from the following sources:
44     *
45     * 1. <code>$wgEventStreams</code>
46     * 2. {@link GetStreamConfigsHook}
47     *
48     * If there are duplicate keys in <code>$wgEventStreams</code> and <code>$streamConfigs</code>,
49     * then those in <code>$wgEventStreams</code> are used and those in <code>$streamConfigs</code>
50     * are discarded.
51     */
52    public function getInstance(): StreamConfigs {
53        $streamConfigs = [];
54        $this->hookRunner->onGetStreamConfigs( $streamConfigs );
55
56        $streamConfigs = array_merge(
57            $streamConfigs,
58            $this->options->get( 'EventStreams' )
59        );
60
61        return new StreamConfigs(
62            $streamConfigs,
63            $this->options->get( 'EventStreamsDefaultSettings' ),
64            $this->logger
65        );
66    }
67}