Passed
Push — master ( 1101fb...182bcf )
by Rafael
33:26
created

SolrWriteService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 34.09%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 28
c 3
b 0
f 0
dl 0
loc 86
ccs 15
cts 44
cp 0.3409
rs 10

5 Methods

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