Passed
Push — master ( 21b29e...6b264d )
by Timo
37:16
created

Repository::initializeSearch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ApacheSolrDocument;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 Rafael Kähm <[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\ConnectionManager;
28
use ApacheSolrForTypo3\Solr\Domain\Search\Query\QueryBuilder;
29
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\Parser\DocumentEscapeService;
30
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
31
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
32
use ApacheSolrForTypo3\Solr\Search;
33
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
34
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
35
use ApacheSolrForTypo3\Solr\System\Solr\SolrCommunicationException;
36
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
37
use ApacheSolrForTypo3\Solr\Util;
38
use TYPO3\CMS\Core\SingletonInterface;
39
use TYPO3\CMS\Core\Utility\GeneralUtility;
40
41
/**
42
 * Class ApacheSolrDocumentRepository uses connection to Solr Server
43
 */
44
class Repository implements SingletonInterface
45
{
46
47
    /**
48
     * Search
49
     *
50
     * @var \ApacheSolrForTypo3\Solr\Search
51
     */
52
    protected $search;
53
54
    /**
55
     * @var DocumentEscapeService
56
     */
57
    protected $documentEscapeService = null;
58
59
    /**
60
     * @var TypoScriptConfiguration|null
61
     */
62
    protected $typoScriptConfiguration = null;
63
64
    /**
65
     * @var QueryBuilder
66
     */
67
    protected $queryBuilder;
68
69
    /**
70
     * Repository constructor.
71
     * @param DocumentEscapeService|null $documentEscapeService
72
     * @param QueryBuilder|null $queryBuilder
73
     */
74 6
    public function __construct(DocumentEscapeService $documentEscapeService = null, TypoScriptConfiguration $typoScriptConfiguration = null, QueryBuilder $queryBuilder = null)
75
    {
76 6
        $this->typoScriptConfiguration = $typoScriptConfiguration ?? Util::getSolrConfiguration();
77 6
        $this->documentEscapeService = $documentEscapeService ?? GeneralUtility::makeInstance(DocumentEscapeService::class, /** @scrutinizer ignore-type */ $typoScriptConfiguration);
78 6
        $this->queryBuilder = $queryBuilder ?? GeneralUtility::makeInstance(QueryBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
79 6
    }
80
81
    /**
82
     * Returns firs found Apache_Solr_Document for current page by given language id.
83
     *
84
     * @param $languageId
85
     * @return Document|false
86
     */
87 1
    public function findOneByPageIdAndByLanguageId($pageId, $languageId)
88
    {
89 1
        $documentCollection = $this->findByPageIdAndByLanguageId($pageId, $languageId);
90 1
        return reset($documentCollection);
91
    }
92
93
    /**
94
     * Returns all found Apache_Solr_Document[] by given page id and language id.
95
     * Returns empty array if nothing found, e.g. if no language or no page(or no index for page) is present.
96
     *
97
     * @param int $pageId
98
     * @param int $languageId
99
     * @return Document[]
100
     */
101 6
    public function findByPageIdAndByLanguageId($pageId, $languageId)
102
    {
103
        try {
104 6
            $this->initializeSearch($pageId, $languageId);
105 2
            $pageQuery = $this->queryBuilder->buildPageQuery($pageId);
106 2
            $response = $this->search->search($pageQuery, 0, 10000);
107 4
        } catch (NoSolrConnectionFoundException $exception) {
108 2
            return [];
109 2
        } catch (SolrCommunicationException $exception) {
110
            return [];
111
        }
112 2
        $data = $response->getParsedData();
113 2
        return $this->documentEscapeService->applyHtmlSpecialCharsOnAllFields($data->response->docs ?? []);
114
    }
115
116
    /**
117
     * @param string $type
118
     * @param int $uid
119
     * @param int $pageId
120
     * @param int $languageId
121
     * @return Document[]|array
122
     */
123
    public function findByTypeAndPidAndUidAndLanguageId($type, $uid, $pageId, $languageId): array
124
    {
125
        try {
126
            $this->initializeSearch($pageId, $languageId);
127
            $recordQuery = $this->queryBuilder->buildRecordQuery($type, $uid, $pageId);
128
            $response = $this->search->search($recordQuery, 0, 10000);
129
        } catch (NoSolrConnectionFoundException $exception) {
130
            return [];
131
        } catch (SolrCommunicationException $exception) {
132
            return [];
133
        }
134
        $data = $response->getParsedData();
135
        return $this->documentEscapeService->applyHtmlSpecialCharsOnAllFields($data->response->docs ?? []);
136
    }
137
138
    /**
139
     * Initializes Search for given language
140
     *
141
     * @param int $languageId
142
     */
143 5
    protected function initializeSearch($pageId, $languageId = 0)
144
    {
145 5
        if (!is_int($pageId)) {
146 1
            throw new \InvalidArgumentException('Invalid page ID = ' . $pageId, 1487332926);
147
        }
148 4
        if (!is_int($languageId)) { // @todo: Check if lang id is defined and present?
0 ignored issues
show
introduced by
The condition is_int($languageId) is always true.
Loading history...
149 1
            throw new \InvalidArgumentException('Invalid language ID = ' . $languageId, 1487335178);
150
        }
151
        /* @var $connectionManager ConnectionManager */
152 3
        $connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
153 3
        $solrConnection = $connectionManager->getConnectionByPageId($pageId, $languageId);
154
155 2
        $this->search = $this->getSearch($solrConnection);
156 2
    }
157
158
    /**
159
     * Retrieves an instance of the Search object.
160
     *
161
     * @param SolrConnection $solrConnection
162
     * @return Search
163
     */
164 1
    protected function getSearch($solrConnection)
165
    {
166 1
        return  GeneralUtility::makeInstance(Search::class, /** @scrutinizer ignore-type */ $solrConnection);
167
    }
168
}
169