Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
87.50% |
14 / 16 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
EditorCapabilityFactory | |
87.50% |
14 / 16 |
|
50.00% |
1 / 2 |
4.03 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
newCapability | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
3.03 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\CommunityConfiguration\EditorCapabilities; |
4 | |
5 | use InvalidArgumentException; |
6 | use LogicException; |
7 | use MediaWiki\Config\ServiceOptions; |
8 | use MediaWiki\Context\IContextSource; |
9 | use MediaWiki\Logger\LoggerFactory; |
10 | use MediaWiki\Title\Title; |
11 | use Wikimedia\ObjectFactory\ObjectFactory; |
12 | |
13 | class EditorCapabilityFactory { |
14 | /** |
15 | * @var string[] |
16 | * @internal for use in ServiceWiring only |
17 | */ |
18 | public const CONSTRUCTOR_OPTIONS = [ |
19 | 'CommunityConfigurationEditorCapabilities', |
20 | ]; |
21 | |
22 | /** @var array ObjectFactory specs for validators, indexed by validator name */ |
23 | private array $capabilitiesSpecs; |
24 | |
25 | private ObjectFactory $objectFactory; |
26 | |
27 | /** |
28 | * @param ServiceOptions $options |
29 | * @param ObjectFactory $objectFactory |
30 | */ |
31 | public function __construct( ServiceOptions $options, ObjectFactory $objectFactory ) { |
32 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
33 | $this->capabilitiesSpecs = $options->get( 'CommunityConfigurationEditorCapabilities' ); |
34 | |
35 | $this->objectFactory = $objectFactory; |
36 | } |
37 | |
38 | /** |
39 | * @param string $name |
40 | * @param IContextSource $ctx |
41 | * @param Title $parentTitle |
42 | * @return IEditorCapability |
43 | */ |
44 | public function newCapability( |
45 | string $name, |
46 | IContextSource $ctx, |
47 | Title $parentTitle |
48 | ): IEditorCapability { |
49 | if ( !array_key_exists( $name, $this->capabilitiesSpecs ) ) { |
50 | throw new InvalidArgumentException( "Capability $name is not supported" ); |
51 | } |
52 | |
53 | $result = $this->objectFactory->createObject( |
54 | $this->capabilitiesSpecs[$name], |
55 | [ |
56 | 'assertClass' => IEditorCapability::class, |
57 | 'extraArgs' => [ $ctx, $parentTitle ], |
58 | ] |
59 | ); |
60 | |
61 | // NOTE: This is here to allow for type hints. |
62 | if ( !$result instanceof IEditorCapability ) { |
63 | throw new LogicException( 'ObjectFactory\'s assertion is invalid' ); |
64 | } |
65 | |
66 | $result->setLogger( LoggerFactory::getInstance( 'CommunityConfiguration' ) ); |
67 | return $result; |
68 | } |
69 | } |