Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
SchemaConverterFactory | |
0.00% |
0 / 17 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSchemaConverterFromClass | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
getSchemaReader | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getConverterFromVersion | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\CommunityConfiguration\Schema; |
4 | |
5 | use LogicException; |
6 | use Wikimedia\ObjectFactory\ObjectFactory; |
7 | |
8 | /** |
9 | * Construct an ISchemaConverter for a specified version and schema. |
10 | */ |
11 | class SchemaConverterFactory { |
12 | |
13 | private ObjectFactory $objectFactory; |
14 | |
15 | public function __construct( ObjectFactory $objectFactory ) { |
16 | $this->objectFactory = $objectFactory; |
17 | } |
18 | |
19 | /** |
20 | * Construct a ISchemaConverter from a class name |
21 | * |
22 | * @param string|null $className |
23 | * @return ISchemaConverter|null |
24 | */ |
25 | private function getSchemaConverterFromClass( ?string $className ): ?ISchemaConverter { |
26 | if ( $className === null ) { |
27 | return null; |
28 | } |
29 | |
30 | // ObjectFactory::createObject supports arrays as its arguments, not just callables |
31 | // Link to Phan bug: https://github.com/phan/phan/issues/1648 |
32 | // @phan-suppress-next-line PhanTypeInvalidCallableArraySize |
33 | $res = $this->objectFactory->createObject( [ |
34 | // ObjectFactory::createObject supports arrays as its arguments, not just callables |
35 | // Link to Phan bug: https://github.com/phan/phan/issues/1648 |
36 | // @phan-suppress-next-line PhanTypeInvalidCallableArraySize |
37 | 'class' => $className |
38 | ], [ |
39 | 'assertClass' => ISchemaConverter::class, |
40 | ] ); |
41 | |
42 | // NOTE: This is here to allow for type hints. |
43 | if ( !$res instanceof ISchemaConverter ) { |
44 | throw new LogicException( 'ObjectFactory\'s assertion is invalid' ); |
45 | } |
46 | |
47 | return $res; |
48 | } |
49 | |
50 | /** |
51 | * Get an appropriate SchemaReader for given version |
52 | * |
53 | * @param SchemaBuilder $schemaBuilder |
54 | * @param string|null $version Version (null for latest) |
55 | * @return SchemaReader |
56 | */ |
57 | private function getSchemaReader( |
58 | SchemaBuilder $schemaBuilder, |
59 | ?string $version = null |
60 | ): SchemaReader { |
61 | if ( $version === null ) { |
62 | return $schemaBuilder->getSchemaReader(); |
63 | } |
64 | |
65 | return $schemaBuilder->getVersionManager() |
66 | ->getVersionForSchema( $version ); |
67 | } |
68 | |
69 | /** |
70 | * Construct a schema converter for a particular version |
71 | * |
72 | * @param SchemaBuilder $schemaBuilder |
73 | * @param string|null $version Version (null for latest) |
74 | * @return ISchemaConverter|null |
75 | */ |
76 | public function getConverterFromVersion( |
77 | SchemaBuilder $schemaBuilder, |
78 | ?string $version = null |
79 | ): ?ISchemaConverter { |
80 | $schemaReader = $this->getSchemaReader( $schemaBuilder, $version ); |
81 | return $this->getSchemaConverterFromClass( $schemaReader->getSchemaConverterId() ); |
82 | } |
83 | } |