Passed
Push — master ( 079fbb...13643b )
by Rafael
36:13
created

SchemaParser::parseManagedResourceId()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 10.4218

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 20
ccs 10
cts 17
cp 0.5881
rs 8.8333
c 0
b 0
f 0
cc 7
nc 4
nop 1
crap 10.4218
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\System\Solr\Parser;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2010-2016 Timo Hund <[email protected]
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\System\Solr\Schema\Schema;
29
use stdClass;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
/**
33
 * Class to parse the schema from a solr response.
34
 *
35
 * @author Timo Hund <[email protected]>
36
 */
37
class SchemaParser
38
{
39
40
    /**
41
     * Parse the solr stopwords response from an json string to an array.
42
     *
43
     * @param string $jsonString
44
     * @return Schema
45
     */
46 8
    public function parseJson($jsonString)
47
    {
48 8
        $decodedResponse = json_decode($jsonString);
49 8
        $schemaResponse = $decodedResponse->schema;
50
51 8
        $schema = GeneralUtility::makeInstance(Schema::class);
52
53 8
        if ($schemaResponse === null) {
54 1
            return $schema;
55
        }
56
57 7
        $language = $this->parseManagedResourceId($schemaResponse);
58 7
        $schema->setManagedResourceId($language);
59
60 7
        $name = $this->parseName($schemaResponse);
61 7
        $schema->setName($name);
62
63 7
        return $schema;
64
    }
65
66
    /**
67
     * Extracts the language from a solr schema response.
68
     *
69
     * @param stdClass $schema
70
     * @return ?string
71
     */
72 7
    protected function parseManagedResourceId(stdClass $schema): ?string
73
    {
74 7
        $managedResourceId = null;
75 7
        if (!is_object($schema) || !isset($schema->fieldTypes)) {
76
            return null;
77
        }
78
79 7
        foreach ($schema->fieldTypes as $fieldType) {
80 7
            if ($fieldType->name !== 'text') {
81 7
                continue;
82
            }
83
            // we have a text field
84 7
            foreach ($fieldType->queryAnalyzer->filters as $filter) {
85 7
                if ($filter->class === 'solr.ManagedSynonymGraphFilterFactory') {
86 7
                    $managedResourceId = $filter->managed;
87
                }
88
            }
89
        }
90
91 7
        return $managedResourceId;
92
    }
93
94
    /**
95
     * Extracts the schema name from the response.
96
     *
97
     * @param stdClass $schemaResponse
98
     * @return string
99
     */
100 7
    protected function parseName(stdClass $schemaResponse): string
101
    {
102 7
        return $schemaResponse->name ?? '';
103
    }
104
}
105