Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConfiguredLexiconStorage
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 7
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
42
 getEntry
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLocalEntry
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createEntryItem
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 updateEntryItem
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 deleteEntryItem
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 syncEntryItem
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Wikispeech\Lexicon;
4
5/**
6 * @file
7 * @ingroup Extensions
8 * @license GPL-2.0-or-later
9 */
10
11use InvalidArgumentException;
12use MediaWiki\MediaWikiServices;
13use MediaWiki\Wikispeech\WikispeechServices;
14use RuntimeException;
15
16/**
17 * A decorated {@link LexiconStorage}
18 * selected based on the configuration value WikispeechPronunciationLexiconConfiguration
19 * @since 0.1.9
20 */
21class ConfiguredLexiconStorage implements LexiconStorage {
22
23    /** @var LexiconStorage */
24    private $decorated;
25
26    /**
27     * @since 0.1.9
28     * @param string $enumValue
29     * @param MediaWikiServices $services
30     * @throws RuntimeException If value of WikispeechPronunciationLexiconConfiguration is unsupported.
31     */
32    public function __construct(
33        string $enumValue,
34        MediaWikiServices $services
35    ) {
36        $enumValueLower = trim( mb_strtolower( $enumValue ) );
37        if ( $enumValueLower === 'speechoid' ) {
38            $this->decorated = WikispeechServices::getLexiconSpeechoidStorage( $services );
39        } elseif ( $enumValueLower === 'wiki' ) {
40            $this->decorated = WikispeechServices::getLexiconWikiStorage( $services );
41        } elseif ( $enumValueLower === 'wiki+speechoid' ) {
42            $this->decorated = new LexiconHandler(
43                WikispeechServices::getLexiconSpeechoidStorage( $services ),
44                WikispeechServices::getLexiconWikiStorage( $services )
45            );
46        } elseif ( $enumValueLower === 'cache' ) {
47            $this->decorated = WikispeechServices::getLexiconWanCacheStorage( $services );
48        } elseif ( $enumValueLower === 'cache+speechoid' ) {
49            $this->decorated = new LexiconHandler(
50                WikispeechServices::getLexiconSpeechoidStorage( $services ),
51                WikispeechServices::getLexiconWanCacheStorage( $services )
52            );
53        } else {
54            throw new RuntimeException( "Unsupported value for WikispeechPronunciationLexiconConfiguration: $enumValue"
55            );
56        }
57    }
58
59    /**
60     * @inheritDoc
61     */
62    public function getEntry(
63        string $language,
64        string $key
65    ): ?LexiconEntry {
66        return $this->decorated->getEntry( $language, $key );
67    }
68
69    /**
70     * Returns the local lexicon entry without considering any fallbacks.
71     *
72     * @since 0.1.12
73     *
74     * @param string $language
75     * @param string $key
76     * @return LexiconEntry|null
77     */
78    public function getLocalEntry(
79        string $language,
80        string $key
81    ): ?LexiconEntry {
82        /** @phan-suppress-next-line PhanUndeclaredMethod */
83        return $this->decorated->getLocalEntry( $language, $key );
84    }
85
86    /**
87     * @inheritDoc
88     */
89    public function createEntryItem(
90        string $language,
91        string $key,
92        LexiconEntryItem $item
93    ): void {
94        $this->decorated->createEntryItem( $language, $key, $item );
95    }
96
97    /**
98     * @inheritDoc
99     */
100    public function updateEntryItem(
101        string $language,
102        string $key,
103        LexiconEntryItem $item
104    ): void {
105        $this->decorated->updateEntryItem( $language, $key, $item );
106    }
107
108    /**
109     * @inheritDoc
110     */
111    public function deleteEntryItem(
112        string $language,
113        string $key,
114        LexiconEntryItem $item
115    ): void {
116        $this->decorated->deleteEntryItem( $language, $key, $item );
117    }
118
119    /**
120     * Synchronizes an entry item to both storages if applicable.
121     * Only works if the decorated storage is a LexiconHandler.
122     *
123     * @since 0.1.12
124     *
125     * @param string $language
126     * @param string $key
127     * @param int $speechoidId
128     * @throws InvalidArgumentException
129     */
130    public function syncEntryItem(
131        string $language,
132        string $key,
133        int $speechoidId
134    ): void {
135        if ( $this->decorated instanceof LexiconHandler ) {
136            if ( !$speechoidId ) {
137                throw new InvalidArgumentException(
138                    "Cannot sync item without Speechoid identity"
139                );
140            }
141            $this->decorated->syncEntryItem( $language, $key, $speechoidId );
142        } else {
143            throw new InvalidArgumentException(
144                "Decorated lexicon storage is not of type LexiconHandler."
145            );
146        }
147    }
148}