Completed
Push — master ( bc4bd1...b40f3e )
by Timo
05:05
created

ServiceFactory::getConfiguredTika()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace ApacheSolrForTypo3\Tika\Service\Tika;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29
/**
30
 * Provides a Tika service depending on the extension's configuration
31
 *
32
 * @author Ingo Renner <[email protected]>
33
 */
34
class ServiceFactory
35
{
36
37
    /**
38
     * Creates an instance of a Tika service
39
     *
40
     * @param string $tikaServiceType Tika Service type, one of jar, server, or solr (or tika for BC, same as jar)
41
     * @param array $configuration EXT:tika EM configuration (initialized by this factory, parameter exists for tests)
42
     * @return AppService|ServerService|SolrCellService
43
     *
44
     * @throws \InvalidArgumentException for unknown Tika service type
45
     * @throws \RuntimeException if configuration cannot be initialized
46
     */
47 6
    public static function getTika(
48
        $tikaServiceType,
49
        array $configuration = null
50
    ) {
51 6
        if (empty($configuration)) {
52 1
            $configuration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['tika']);
53
        }
54
55 6
        if (!is_array($configuration)) {
56 1
            throw new \RuntimeException(
57 1
                'Invalid configuration',
58 1
                1439352237
59
            );
60
        }
61
62
        switch ($tikaServiceType) {
63 5
            case 'jar':
64 4
            case 'tika': // backwards compatibility only
65 2
                return GeneralUtility::makeInstance(AppService::class, $configuration);
66 3
            case 'server':
67 1
                return GeneralUtility::makeInstance(ServerService::class, $configuration);
68 2
            case 'solr':
69 1
                return GeneralUtility::makeInstance(SolrCellService::class, $configuration);
70
            default:
71 1
                throw new \InvalidArgumentException(
72 1
                    'Unknown Tika service type "' . $tikaServiceType . '". Must be one of jar, server, or solr.',
73 1
                    1423035119
74
                );
75
        }
76
    }
77
78
    /**
79
     * Creates a tika service instance from the extension configuration.
80
     *
81
     * @return AppService|ServerService|SolrCellService
82
     */
83
    public static function getConfiguredTika()
84
    {
85
        $tikaConfiguration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['tika']);
86
        return static::getTika($tikaConfiguration['extractor'], $tikaConfiguration);
87
    }
88
89
}
90