AbstractService   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 58.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 88
ccs 14
cts 24
cp 0.5833
rs 10
c 2
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdditionalCommandOptions() 0 18 4
A initializeService() 0 2 1
A getSupportedMimeTypes() 0 3 1
A __construct() 0 4 1
A log() 0 9 2
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 Psr\Log\LoggerAwareInterface;
21
use Psr\Log\LoggerAwareTrait;
22
use Psr\Log\LogLevel;
23
use TYPO3\CMS\Core\Utility\CommandUtility;
24
25
/**
26
 * Abstract Tika service implementing shared methods
27
 *
28
 * @copyright (c) 2015 Ingo Renner <[email protected]>
29
 */
30
abstract class AbstractService implements ServiceInterface, LoggerAwareInterface
31
{
32
    protected const JAVA_COMMAND_OPTIONS_REGEX = '/-D(?P<property>[\w.]+)=(?P<value>"[^"]+"|\'[^\']+\'|[^\\s\'"]+)/';
33
34
    use LoggerAwareTrait;
35
36
    /**
37
     * @var array
38
     */
39
    protected array $configuration;
40
41
    /**
42
     * Constructor
43
     *
44
     * @param array $configuration
45
     */
46 61
    public function __construct(array $configuration)
47
    {
48 61
        $this->configuration = $configuration;
49 61
        $this->initializeService();
50
    }
51
52
    /**
53
     * Service initialization
54
     */
55
    protected function initializeService(): void
56
    {
57
    }
58
59
    /**
60
     * Logs a message and optionally data to log file
61
     *
62
     * @param string $message Log message
63
     * @param array $data Optional data
64
     * @param int|string $severity Use constants from class LogLevel
65
     * @see LogLevel For supported log levels
66
     */
67 28
    protected function log(string $message, array $data = [], $severity = LogLevel::DEBUG): void
68
    {
69 28
        if (empty($this->configuration['logging'])) {
70 28
            return;
71
        }
72
        $this->logger->/** @scrutinizer ignore-call */ log(
73
            $severity,
74
            $message,
75
            $data
76
        );
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function getSupportedMimeTypes(): array
83
    {
84
        return [];
85
    }
86
87
    /**
88
     * Parse additional Java command options.
89
     *
90
     * Reads the configuration value `javaCommandOptions` and tries to parse it to a
91
     * safe argument string. For safety reasons, only the following variants are
92
     * allowed (multiple separated by space):
93
     *
94
     * -Dfoo=bar
95
     * -Dfoo='hello world'
96
     * -Dfoo="hello world"
97
     *
98
     * @return string Parsed additional Java command options
99
     */
100 12
    protected function getAdditionalCommandOptions(): string
101
    {
102 12
        $commandOptions = trim((string)($this->configuration['javaCommandOptions'] ?? ''));
103
104
        // Early return if no additional command options are configured
105
        // or configuration does not match required pattern (only -D parameter is supported)
106 12
        if ($commandOptions === '' || !preg_match_all(self::JAVA_COMMAND_OPTIONS_REGEX, $commandOptions, $matches)) {
107
            return '';
108
        }
109
110
        // Combine matched command options with escaped argument value
111 12
        $commandOptionsString = '';
112 12
        foreach (array_combine($matches['property'], $matches['value']) as $property => $unescapedValue) {
113 12
            $escapedValue = CommandUtility::escapeShellArgument(trim($unescapedValue ?? '', '"\''));
114 12
            $commandOptionsString .= sprintf(' -D%s=%s', $property, $escapedValue);
115
        }
116
117 12
        return $commandOptionsString;
118
    }
119
}
120