Passed
Push — release-11.0.x ( 910681...ed160c )
by Rafael
11:00
created

ServiceFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 72.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 44
ccs 13
cts 18
cp 0.7221
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfiguredTika() 0 4 1
A getTika() 0 18 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApacheSolrForTypo3\Tika\Service\Tika;
6
7
/*
8
 * This file is part of the TYPO3 CMS project.
9
 *
10
 * It is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU General Public License, either version 2
12
 * of the License, or any later version.
13
 *
14
 * For the full copyright and license information, please read the
15
 * LICENSE.txt file that was distributed with this source code.
16
 *
17
 * The TYPO3 project - inspiring people to share!
18
 */
19
20
use ApacheSolrForTypo3\Tika\Util;
21
use InvalidArgumentException;
22
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
23
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
/**
27
 * Provides a Tika service depending on the extension's configuration
28
 *
29
 * @author Ingo Renner <[email protected]>
30
 */
31
class ServiceFactory
32
{
33
34
    /**
35
     * Creates an instance of a Tika service
36
     *
37
     * @param string $tikaServiceType Tika Service type, one of jar, server, or solr (or tika for BC, same as jar)
38
     * @param array|null $configuration EXT:tika EM configuration (initialized by this factory, parameter exists for tests)
39
     * @return AppService|ServerService|SolrCellService
40
     *
41
     * @throws ExtensionConfigurationExtensionNotConfiguredException
42
     * @throws ExtensionConfigurationPathDoesNotExistException
43
     */
44 5
    public static function getTika(string $tikaServiceType, array $configuration = null)
45
    {
46 5
        if (empty($configuration)) {
47
            $configuration = Util::getTikaExtensionConfiguration();
48
        }
49
50 5
        switch ($tikaServiceType) {
51 5
            case 'jar':
52 4
            case 'tika': // backwards compatibility only
53 2
                return GeneralUtility::makeInstance(AppService::class, $configuration);
54 3
            case 'server':
55 1
                return GeneralUtility::makeInstance(ServerService::class, $configuration);
56 2
            case 'solr':
57 1
                return GeneralUtility::makeInstance(SolrCellService::class, $configuration);
58
            default:
59 1
                throw new InvalidArgumentException(
60 1
                    'Unknown Tika service type "' . $tikaServiceType . '". Must be one of jar, server, or solr.',
61 1
                    1423035119
62
                );
63
        }
64
    }
65
66
    /**
67
     * Creates a tika service instance from the extension configuration.
68
     *
69
     * @return AppService|ServerService|SolrCellService
70
     */
71
    public static function getConfiguredTika(): AbstractService
72
    {
73
        $tikaConfiguration = Util::getTikaExtensionConfiguration();
74
        return static::getTika($tikaConfiguration['extractor'], Util::getTikaExtensionConfiguration());
75
    }
76
}
77