Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
TranslatableBundleFactory | |
0.00% |
0 / 29 |
|
0.00% |
0 / 7 |
306 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getBundle | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
getValidBundle | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getBundleFromClass | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getPageMoveLogger | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
getPageDeleteLogger | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
getStore | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\MessageGroupProcessing; |
5 | |
6 | use InvalidArgumentException; |
7 | use MediaWiki\Extension\Translate\MessageBundleTranslation\MessageBundle; |
8 | use MediaWiki\Extension\Translate\MessageBundleTranslation\MessageBundleStore; |
9 | use MediaWiki\Extension\Translate\PageTranslation\TranslatablePage; |
10 | use MediaWiki\Title\Title; |
11 | |
12 | /** |
13 | * Create instances of various classes based on the type of TranslatableBundle |
14 | * @author Abijeet Patro |
15 | * @author Niklas Laxström |
16 | * @since 2022.03 |
17 | * @license GPL-2.0-or-later |
18 | */ |
19 | class TranslatableBundleFactory { |
20 | /** @var TranslatablePageStore */ |
21 | private $translatablePageStore; |
22 | /** @var MessageBundleStore */ |
23 | private $messageBundleStore; |
24 | |
25 | public function __construct( |
26 | TranslatablePageStore $translatablePageStore, |
27 | MessageBundleStore $messageBundleStore |
28 | ) { |
29 | $this->translatablePageStore = $translatablePageStore; |
30 | $this->messageBundleStore = $messageBundleStore; |
31 | } |
32 | |
33 | /** Returns a TranslatableBundle if Title is a valid translatable bundle else returns null */ |
34 | public function getBundle( Title $title ): ?TranslatableBundle { |
35 | if ( TranslatablePage::isSourcePage( $title ) ) { |
36 | return TranslatablePage::newFromTitle( $title ); |
37 | } elseif ( MessageBundle::isSourcePage( $title ) ) { |
38 | return new MessageBundle( $title ); |
39 | } |
40 | |
41 | return null; |
42 | } |
43 | |
44 | /** Return a TranslatableBundle from the Title, throwing an error if it is not a TranslatableBundle */ |
45 | public function getValidBundle( Title $title ): TranslatableBundle { |
46 | $bundle = $this->getBundle( $title ); |
47 | if ( $bundle ) { |
48 | return $bundle; |
49 | } |
50 | |
51 | throw new InvalidArgumentException( "{$title->getPrefixedText()} is not a TranslatableBundle" ); |
52 | } |
53 | |
54 | public function getBundleFromClass( Title $title, string $bundleType ): TranslatableBundle { |
55 | if ( $bundleType === MessageBundle::class ) { |
56 | return new MessageBundle( $title ); |
57 | } else { |
58 | return TranslatablePage::newFromTitle( $title ); |
59 | } |
60 | } |
61 | |
62 | public function getPageMoveLogger( TranslatableBundle $bundle ): PageMoveLogger { |
63 | if ( $bundle instanceof TranslatablePage ) { |
64 | return new PageMoveLogger( $bundle->getTitle(), 'pagetranslation' ); |
65 | } elseif ( $bundle instanceof MessageBundle ) { |
66 | return new PageMoveLogger( $bundle->getTitle(), 'messagebundle' ); |
67 | } |
68 | |
69 | throw new InvalidArgumentException( 'Unknown TranslatableBundle type: ' . get_class( $bundle ) ); |
70 | } |
71 | |
72 | public function getPageDeleteLogger( TranslatableBundle $bundle ): PageDeleteLogger { |
73 | if ( $bundle instanceof TranslatablePage ) { |
74 | return new PageDeleteLogger( $bundle->getTitle(), 'pagetranslation' ); |
75 | } elseif ( $bundle instanceof MessageBundle ) { |
76 | return new PageDeleteLogger( $bundle->getTitle(), 'messagebundle' ); |
77 | } |
78 | |
79 | throw new InvalidArgumentException( 'Unknown TranslatableBundle type: ' . get_class( $bundle ) ); |
80 | } |
81 | |
82 | public function getStore( TranslatableBundle $bundle ): TranslatableBundleStore { |
83 | if ( $bundle instanceof TranslatablePage ) { |
84 | return $this->translatablePageStore; |
85 | } elseif ( $bundle instanceof MessageBundle ) { |
86 | return $this->messageBundleStore; |
87 | } |
88 | |
89 | throw new InvalidArgumentException( "Unknown TranslatableBundle type: " . get_class( $bundle ) ); |
90 | } |
91 | } |