Passed
Push — master ( 498335...52b5ef )
by Rafael
42:19
created

SolrWriteService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 42.86%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 31
c 3
b 0
f 0
dl 0
loc 98
ccs 15
cts 35
cp 0.4286
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteByType() 0 6 2
A optimizeIndex() 0 5 1
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
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
18
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter;
19
use Solarium\QueryType\Extract\Query;
20
use Solarium\QueryType\Update\Result;
21
22
/**
23
 * Class SolrWriteService
24
 *
25
 * @author Timo Hund <[email protected]>
26
 */
27
class SolrWriteService extends AbstractSolrService
28
{
29
    const EXTRACT_SERVLET = 'update/extract';
30
31
    /**
32
     * Performs a content and meta data extraction request.
33
     *
34
     * @param Query $query An extraction query
35
     * @return array An array containing the extracted content [0] and meta data [1]
36
     */
37 1
    public function extractByQuery(Query $query)
38
    {
39
        try {
40 1
            $response = $this->createAndExecuteRequest($query);
41 1
            return [$response->file, (array)$response->file_metadata];
42
        } catch (\Exception $e) {
43
            $param = $query->getRequestBuilder()->build($query)->getParams();
44
            $this->logger->log(
45
                SolrLogManager::ERROR,
46
                'Extracting text and meta data through Solr Cell over HTTP POST',
47
                [
48
                    'query' => (array)$query,
49
                    'parameters' => $param,
50
                    'file' => $query->getFile(),
51
                    'query url' => self::EXTRACT_SERVLET,
52
                    'exception' => $e->getMessage()
53
                ]
54
            );
55
        }
56
57
        return [];
58
    }
59
60
    /**
61
     * Deletes all index documents of a certain type and does a commit
62
     * afterwards.
63
     *
64
     * @param string $type The type of documents to delete, usually a table name.
65
     * @param bool $commit Will commit immediately after deleting the documents if set, defaults to TRUE
66
     */
67
    public function deleteByType($type, $commit = true)
68
    {
69
        $this->deleteByQuery('type:' . trim($type));
70
71
        if ($commit) {
72
            $this->commit(false, false);
73
        }
74
    }
75
76
    /**
77
     * Create a delete document based on a query and submit it
78
     *
79
     * @param string $rawQuery Expected to be utf-8 encoded
80
     * @return ResponseAdapter
81
     */
82 14
    public function deleteByQuery($rawQuery) {
83 14
        $query = $this->client->createUpdate();
84 14
        $query->addDeleteQuery($rawQuery);
85 14
        return $this->createAndExecuteRequest($query);
86
    }
87
88
    /**
89
     * Add an array of Solr Documents to the index all at once
90
     *
91
     * @param array $documents Should be an array of \ApacheSolrForTypo3\Solr\System\Solr\Document\Document instances
92
     * @return ResponseAdapter
93
     */
94 89
    public function addDocuments($documents)
95
    {
96 89
        $update = $this->client->createUpdate();
97 89
        $update->addDocuments($documents);
98 89
        return $this->createAndExecuteRequest($update);
99
    }
100
101
    /**
102
     * Send a commit command.  Will be synchronous unless both wait parameters are set to false.
103
     *
104
     * @param boolean $expungeDeletes Defaults to false, merge segments with deletes away
105
     * @param boolean $waitSearcher Defaults to true, block until a new searcher is opened and registered as the main query searcher, making the changes visible
106
     * @return ResponseAdapter
107
     */
108 15
    public function commit($expungeDeletes = false, $waitSearcher = true)
109
    {
110 15
        $update = $this->client->createUpdate();
111 15
        $update->addCommit(false, $waitSearcher, $expungeDeletes);
112 15
        return $this->createAndExecuteRequest($update);
113
    }
114
115
    /**
116
     * Optimize the solr index
117
     *
118
     * @return Result
119
     */
120
    public function optimizeIndex()
121
    {
122
        $update = $this->client->createUpdate();
123
        $update->addOptimize(true, false, 5);
124
        return $this->client->update($update);
125
    }
126
}
127