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