Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
16 / 16 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
StoreFactory | |
100.00% |
16 / 16 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
newStore | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
getSupportedKeys | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\CommunityConfiguration\Store; |
4 | |
5 | use InvalidArgumentException; |
6 | use MediaWiki\Config\ServiceOptions; |
7 | use Wikimedia\ObjectFactory\ObjectFactory; |
8 | |
9 | /** |
10 | * Create a configuration store object |
11 | * @see IConfigurationStore for further documentation |
12 | */ |
13 | class StoreFactory { |
14 | |
15 | /** |
16 | * @var string[] |
17 | * @internal for use in ServiceWiring only |
18 | */ |
19 | public const CONSTRUCTOR_OPTIONS = [ |
20 | 'CommunityConfigurationStores', |
21 | ]; |
22 | |
23 | /** @var array ObjectFactory specs for validators, indexed by validator name */ |
24 | private array $storeSpecs; |
25 | /** @var IConfigurationStore[] validators indexed by name */ |
26 | private array $stores = []; |
27 | private ObjectFactory $objectFactory; |
28 | |
29 | /** |
30 | * @param ServiceOptions $options |
31 | * @param ObjectFactory $objectFactory |
32 | */ |
33 | public function __construct( |
34 | ServiceOptions $options, |
35 | ObjectFactory $objectFactory |
36 | ) { |
37 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
38 | $this->storeSpecs = $options->get( 'CommunityConfigurationStores' ); |
39 | |
40 | $this->objectFactory = $objectFactory; |
41 | } |
42 | |
43 | /** |
44 | * @param string $name |
45 | * @param string $type |
46 | * @param array $storeArgs |
47 | * @return IConfigurationStore |
48 | */ |
49 | public function newStore( string $name, string $type, array $storeArgs ): IConfigurationStore { |
50 | if ( !array_key_exists( $type, $this->storeSpecs ) ) { |
51 | throw new InvalidArgumentException( "Store $type is not supported" ); |
52 | } |
53 | $storeKey = $name . '_' . $type; |
54 | if ( !array_key_exists( $storeKey, $this->stores ) ) { |
55 | $this->stores[$storeKey] = $this->objectFactory->createObject( |
56 | $this->storeSpecs[$type], |
57 | [ |
58 | 'assertClass' => IConfigurationStore::class, |
59 | 'extraArgs' => $storeArgs, |
60 | ], |
61 | ); |
62 | } |
63 | return $this->stores[$storeKey]; |
64 | } |
65 | |
66 | /** |
67 | * Return a list of supported store backends |
68 | * |
69 | * @return string[] List of store names (supported by newStore) |
70 | */ |
71 | public function getSupportedKeys(): array { |
72 | return array_keys( $this->storeSpecs ); |
73 | } |
74 | } |