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        return $this->decorated->getLocalEntry( $language, $key );
83    }
84
85    /**
86     * @inheritDoc
87     */
88    public function createEntryItem(
89        string $language,
90        string $key,
91        LexiconEntryItem $item
92    ): void {
93        $this->decorated->createEntryItem( $language, $key, $item );
94    }
95
96    /**
97     * @inheritDoc
98     */
99    public function updateEntryItem(
100        string $language,
101        string $key,
102        LexiconEntryItem $item
103    ): void {
104        $this->decorated->updateEntryItem( $language, $key, $item );
105    }
106
107    /**
108     * @inheritDoc
109     */
110    public function deleteEntryItem(
111        string $language,
112        string $key,
113        LexiconEntryItem $item
114    ): void {
115        $this->decorated->deleteEntryItem( $language, $key, $item );
116    }
117
118    /**
119     * Synchronizes an entry item to both storages if applicable.
120     * Only works if the decorated storage is a LexiconHandler.
121     *
122     * @since 0.1.12
123     *
124     * @param string $language
125     * @param string $key
126     * @param int $speechoidId
127     * @throws InvalidArgumentException
128     */
129    public function syncEntryItem(
130        string $language,
131        string $key,
132        int $speechoidId
133    ): void {
134        if ( $this->decorated instanceof LexiconHandler ) {
135            if ( !$speechoidId ) {
136                throw new InvalidArgumentException(
137                    "Cannot sync item without Speechoid identity"
138                );
139            }
140            $this->decorated->syncEntryItem( $language, $key, $speechoidId );
141        } else {
142            throw new InvalidArgumentException(
143                "Decorated lexicon storage is not of type LexiconHandler."
144            );
145        }
146    }
147}