Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
LanguageDetectorFactory
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDetectors
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace CirrusSearch\LanguageDetector;
4
5use CirrusSearch\SearchConfig;
6use MediaWiki\Logger\LoggerFactory;
7
8class LanguageDetectorFactory {
9    /**
10     * @var SearchConfig
11     */
12    private $config;
13
14    /**
15     * @param SearchConfig $config
16     */
17    public function __construct( SearchConfig $config ) {
18        $this->config = $config;
19    }
20
21    /**
22     * @return Detector[] array of detectors indexed by name
23     */
24    public function getDetectors() {
25        $detectors = [];
26        foreach ( $this->config->get( 'CirrusSearchLanguageDetectors' ) as $name => $klass ) {
27            if ( !class_exists( $klass ) ) {
28                LoggerFactory::getInstance( 'CirrusSearch' )->info(
29                    "Unknown detector class for {name}: {class}",
30                    [
31                        "name" => $name,
32                        "class" => $klass,
33                    ]
34                );
35                continue;
36            }
37            if ( !in_array( \CirrusSearch\LanguageDetector\Detector::class, class_implements( $klass ) ) ) {
38                LoggerFactory::getInstance( 'CirrusSearch' )->info(
39                    "Bad detector class for {name}: {class}",
40                    [
41                        "name" => $name,
42                        "class" => $klass,
43                    ]
44                );
45                continue;
46            }
47            $detectors[$name] = $klass::build( $this->config );
48        }
49        return $detectors;
50    }
51}