Passed
Pull Request — release-11.0.x (#2992)
by Rafael
29:19
created

SolrWriteService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 52.94%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 32
c 3
b 0
f 0
dl 0
loc 91
ccs 18
cts 34
cp 0.5294
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteByType() 0 6 2
A extractByQuery() 0 26 2
A commit() 0 5 1
A addDocuments() 0 5 1
A deleteByQuery() 0 4 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Solr\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2017 Timo Hund <[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 3 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 ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
28
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter;
29
use Solarium\QueryType\Extract\Query;
30
31
/**
32
 * Class SolrWriteService
33
 */
34
class SolrWriteService extends AbstractSolrService
35
{
36
    const EXTRACT_SERVLET = 'update/extract';
37
38
    /**
39
     * Performs a content and meta data extraction request.
40
     *
41
     * @param Query $query An extraction query
42
     * @return array An array containing the extracted content [0] and meta data [1]
43
     */
44 1
    public function extractByQuery(Query $query)
45
    {
46
        try {
47 1
            $response = $this->createAndExecuteRequest($query);
48 1
            $fileName = basename($query->getFile());
0 ignored issues
show
Bug introduced by
It seems like $query->getFile() can also be of type null; however, parameter $path of basename() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            $fileName = basename(/** @scrutinizer ignore-type */ $query->getFile());
Loading history...
49 1
            $metaKey = $fileName . '_metadata';
50
            return [
51 1
                $response->{$fileName} ?? $response->file,
52 1
                (array)($response->{$metaKey} ?? $response->file_metadata)
53
            ];
54
        } catch (\Exception $e) {
55
            $param = $query->getRequestBuilder()->build($query)->getParams();
56
            $this->logger->log(
57
                SolrLogManager::ERROR,
58
                'Extracting text and meta data through Solr Cell over HTTP POST',
59
                [
60
                    'query' => (array)$query,
61
                    'parameters' => $param,
62
                    'file' => $query->getFile(),
63
                    'query url' => self::EXTRACT_SERVLET,
64
                    'exception' => $e->getMessage()
65
                ]
66
            );
67
        }
68
69
        return [];
70
    }
71
72
    /**
73
     * Deletes all index documents of a certain type and does a commit
74
     * afterwards.
75
     *
76
     * @param string $type The type of documents to delete, usually a table name.
77
     * @param bool $commit Will commit immediately after deleting the documents if set, defaults to TRUE
78
     */
79
    public function deleteByType($type, $commit = true)
80
    {
81
        $this->deleteByQuery('type:' . trim($type));
82
83
        if ($commit) {
84
            $this->commit(false, false);
85
        }
86
    }
87
88
    /**
89
     * Create a delete document based on a query and submit it
90
     *
91
     * @param string $rawQuery Expected to be utf-8 encoded
92
     * @return ResponseAdapter
93
     */
94 14
    public function deleteByQuery($rawQuery) {
95 14
        $query = $this->client->createUpdate();
96 14
        $query->addDeleteQuery($rawQuery);
97 14
        return $this->createAndExecuteRequest($query);
98
    }
99
100
    /**
101
     * Add an array of Solr Documents to the index all at once
102
     *
103
     * @param array $documents Should be an array of Apache_Solr_Document instances
104
     * @return ResponseAdapter
105
     */
106 89
    public function addDocuments($documents)
107
    {
108 89
        $update = $this->client->createUpdate();
109 89
        $update->addDocuments($documents);
110 89
        return $this->createAndExecuteRequest($update);
111
    }
112
113
    /**
114
     * Send a commit command.  Will be synchronous unless both wait parameters are set to false.
115
     *
116
     * @param boolean $expungeDeletes Defaults to false, merge segments with deletes away
117
     * @param boolean $waitSearcher Defaults to true, block until a new searcher is opened and registered as the main query searcher, making the changes visible
118
     * @return ResponseAdapter
119
     */
120 15
    public function commit($expungeDeletes = false, $waitSearcher = true)
121
    {
122 15
        $update = $this->client->createUpdate();
123 15
        $update->addCommit(false, $waitSearcher, $expungeDeletes);
124 15
        return $this->createAndExecuteRequest($update);
125
    }
126
}
127