Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
77.38% |
65 / 84 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
TtmServerFactory | |
77.38% |
65 / 84 |
|
28.57% |
2 / 7 |
52.84 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getNames | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
7.23 | |||
has | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
create | |
66.67% |
12 / 18 |
|
0.00% |
0 / 1 |
12.00 | |||
getDefaultForQuerying | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
4.01 | |||
getWritable | |
74.07% |
20 / 27 |
|
0.00% |
0 / 1 |
13.11 | |||
getWriteOnly | |
72.73% |
8 / 11 |
|
0.00% |
0 / 1 |
4.32 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\TtmServer; |
5 | |
6 | use InvalidArgumentException; |
7 | |
8 | /** |
9 | * @since 2021.01 |
10 | * @license GPL-2.0-or-later |
11 | * @author Niklas Laxström |
12 | */ |
13 | class TtmServerFactory { |
14 | private array $configs; |
15 | private ?string $default; |
16 | private const TTMSERVER_CLASSES = [ |
17 | ReadableTtmServer::class, |
18 | WritableTtmServer::class, |
19 | SearchableTtmServer::class |
20 | ]; |
21 | |
22 | /** @see https://www.mediawiki.org/wiki/Help:Extension:Translate/Translation_memories#Configuration */ |
23 | public function __construct( array $configs, ?string $default = null ) { |
24 | $this->configs = $configs; |
25 | $this->default = $default; |
26 | } |
27 | |
28 | /** @return string[] */ |
29 | public function getNames(): array { |
30 | $ttmServersIds = []; |
31 | foreach ( $this->configs as $serviceId => $config ) { |
32 | $type = $config['type'] ?? ''; |
33 | if ( $type === 'ttmserver' || $type === 'remote-ttmserver' ) { |
34 | $ttmServersIds[] = $serviceId; |
35 | } |
36 | |
37 | // Translation memory configuration may not define a type, in such |
38 | // cases we determine whether the service is a TTM server using the |
39 | // interfaces it implements. |
40 | $serviceClass = $config['class'] ?? null; |
41 | if ( $serviceClass !== null ) { |
42 | foreach ( self::TTMSERVER_CLASSES as $ttmClass ) { |
43 | if ( $serviceClass instanceof $ttmClass ) { |
44 | $ttmServersIds[] = $serviceId; |
45 | break; |
46 | } |
47 | } |
48 | } |
49 | } |
50 | return $ttmServersIds; |
51 | } |
52 | |
53 | public function has( string $name ): bool { |
54 | $ttmServersIds = $this->getNames(); |
55 | return in_array( $name, $ttmServersIds ); |
56 | } |
57 | |
58 | public function create( string $name ): TtmServer { |
59 | if ( !$this->has( $name ) ) { |
60 | throw new ServiceCreationFailure( "No configuration for name '$name'" ); |
61 | } |
62 | |
63 | $config = $this->configs[$name]; |
64 | if ( !is_array( $config ) ) { |
65 | throw new ServiceCreationFailure( "Invalid configuration for name '$name'" ); |
66 | } |
67 | |
68 | if ( isset( $config['class'] ) ) { |
69 | $class = $config['class']; |
70 | |
71 | // TODO: Add a factory to create TTM server instances |
72 | if ( in_array( $class, [ DatabaseTtmServer::class, 'DatabaseTTMServer', 'DatabaseTtmServer' ] ) ) { |
73 | return new DatabaseTtmServer( $config ); |
74 | } |
75 | |
76 | return new $class( $config ); |
77 | } elseif ( isset( $config['type'] ) ) { |
78 | $type = $config['type']; |
79 | switch ( $type ) { |
80 | case 'ttmserver': |
81 | return new DatabaseTtmServer( $config ); |
82 | case 'remote-ttmserver': |
83 | return new RemoteTTMServer( $config ); |
84 | default: |
85 | throw new ServiceCreationFailure( "Unknown type for name '$name': $type" ); |
86 | } |
87 | } |
88 | |
89 | throw new ServiceCreationFailure( "Invalid configuration for name '$name': type not specified" ); |
90 | } |
91 | |
92 | public function getDefaultForQuerying(): ReadableTtmServer { |
93 | if ( $this->default === null ) { |
94 | return new FakeTtmServer(); |
95 | } |
96 | |
97 | if ( $this->configs[ $this->default ][ 'writable' ] ?? false ) { |
98 | throw new InvalidArgumentException( |
99 | "Default TTM service {$this->default} cannot be write only" |
100 | ); |
101 | } |
102 | |
103 | $service = $this->create( $this->default ); |
104 | |
105 | if ( $service instanceof ReadableTtmServer ) { |
106 | return $service; |
107 | } |
108 | |
109 | throw new InvalidArgumentException( |
110 | "Default TTM service {$this->default} must implement ReadableTtmServer." |
111 | ); |
112 | } |
113 | |
114 | /** |
115 | * Returns writable servers if configured, else returns the default TtmServer else returns null. |
116 | * @return array [ serverId => WritableTtmServer ] |
117 | */ |
118 | public function getWritable(): array { |
119 | $writableServers = $readOnlyServers = []; |
120 | $ttmServerIds = $this->getNames(); |
121 | |
122 | foreach ( $ttmServerIds as $serverId ) { |
123 | $isWritable = $this->configs[ $serverId ][ 'writable' ] ?? null; |
124 | |
125 | if ( $isWritable ) { |
126 | if ( $serverId === $this->default ) { |
127 | throw new InvalidArgumentException( |
128 | "Default TTM server {$this->default} cannot be write only" |
129 | ); |
130 | } |
131 | |
132 | $server = $this->create( $serverId ); |
133 | if ( !$server instanceof WritableTtmServer ) { |
134 | throw new InvalidArgumentException( |
135 | "Server '$serverId' marked writable does not implement WritableTtmServer interface" |
136 | ); |
137 | } |
138 | $writableServers[ $serverId ] = $server; |
139 | } elseif ( $isWritable === false ) { |
140 | $readOnlyServers[] = $serverId; |
141 | } |
142 | } |
143 | |
144 | if ( $writableServers ) { |
145 | return $writableServers; |
146 | } |
147 | |
148 | // If there are no writable server, check and use the default server |
149 | if ( $this->default ) { |
150 | $defaultTtmServer = $this->create( $this->default ); |
151 | |
152 | if ( $defaultTtmServer instanceof WritableTtmServer ) { |
153 | if ( !in_array( $this->default, $readOnlyServers ) ) { |
154 | $writableServers[ $this->default ] = $defaultTtmServer; |
155 | } |
156 | } |
157 | |
158 | if ( $writableServers ) { |
159 | return $writableServers; |
160 | } |
161 | } |
162 | |
163 | // Did not find any writable servers. |
164 | return []; |
165 | } |
166 | |
167 | /** Get servers marked as writable */ |
168 | public function getWriteOnly(): array { |
169 | $ttmServerIds = $this->getNames(); |
170 | $writableServers = []; |
171 | foreach ( $ttmServerIds as $serverId ) { |
172 | if ( $this->configs[ $serverId ][ 'writable' ] ?? false ) { |
173 | $server = $this->create( $serverId ); |
174 | if ( !$server instanceof WritableTtmServer ) { |
175 | throw new \InvalidArgumentException( |
176 | "Server '$serverId' marked writable does not implement WritableTtmServer interface" |
177 | ); |
178 | } |
179 | $writableServers[ $serverId ] = $server; |
180 | } |
181 | } |
182 | |
183 | return $writableServers; |
184 | } |
185 | } |