Passed
Pull Request — main (#3339)
by Rafael
06:51 queued 02:48
created

Repository::getSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\Domain\Search\ApacheSolrDocument;
17
18
use ApacheSolrForTypo3\Solr\ConnectionManager;
19
use ApacheSolrForTypo3\Solr\Domain\Search\Query\QueryBuilder;
20
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\Parser\DocumentEscapeService;
21
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
22
use ApacheSolrForTypo3\Solr\Search;
23
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
24
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
25
use ApacheSolrForTypo3\Solr\System\Solr\SolrCommunicationException;
26
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
27
use ApacheSolrForTypo3\Solr\Util;
28
use Exception;
29
use TYPO3\CMS\Core\SingletonInterface;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
/**
33
 * Class Repository
34
 *
35
 * Purpose: TYPO3 BE INFO module :: Index Documents tab
36
 */
37
class Repository implements SingletonInterface
38
{
39
    /**
40
     * Search
41
     *
42
     * @var Search|null
43
     */
44
    protected ?Search $search = null;
45
46
    /**
47
     * @var DocumentEscapeService
48
     */
49
    protected DocumentEscapeService $documentEscapeService;
50
51
    /**
52
     * @var TypoScriptConfiguration
53
     */
54
    protected TypoScriptConfiguration $typoScriptConfiguration;
55
56
    /**
57
     * @var QueryBuilder
58
     */
59
    protected QueryBuilder $queryBuilder;
60
61
    /**
62
     * Repository constructor.
63
     * @param DocumentEscapeService|null $documentEscapeService
64
     * @param QueryBuilder|null $queryBuilder
65
     */
66 7
    public function __construct(
67
        DocumentEscapeService $documentEscapeService = null,
68
        TypoScriptConfiguration $typoScriptConfiguration = null,
69
        QueryBuilder $queryBuilder = null
70
    ) {
71 7
        $this->typoScriptConfiguration = $typoScriptConfiguration ?? Util::getSolrConfiguration();
72 7
        $this->documentEscapeService = $documentEscapeService ?? GeneralUtility::makeInstance(DocumentEscapeService::class, /** @scrutinizer ignore-type */ $typoScriptConfiguration);
73 7
        $this->queryBuilder = $queryBuilder ?? GeneralUtility::makeInstance(QueryBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
74
    }
75
76
    /**
77
     * Returns firs found \ApacheSolrForTypo3\Solr\System\Solr\Document\Document for current page by given language id.
78
     *
79
     * @param $pageId
80
     * @param $languageId
81
     * @return Document|false
82
     * @throws Exception
83
     */
84 1
    public function findOneByPageIdAndByLanguageId($pageId, $languageId)
85
    {
86 1
        $documentCollection = $this->findByPageIdAndByLanguageId($pageId, $languageId);
87 1
        return reset($documentCollection);
88
    }
89
90
    /**
91
     * Returns all found \ApacheSolrForTypo3\Solr\System\Solr\Document\Document[] by given page id and language id.
92
     * Returns empty array if nothing found, e.g. if no language or no page(or no index for page) is present.
93
     *
94
     * @param int $pageId
95
     * @param int $languageId
96
     * @return Document[]
97
     * @throws Exception
98
     */
99 4
    public function findByPageIdAndByLanguageId(int $pageId, int $languageId): array
100
    {
101
        try {
102 4
            $this->initializeSearch($pageId, $languageId);
103 2
            $pageQuery = $this->queryBuilder->buildPageQuery($pageId);
104 2
            $response = $this->search->search($pageQuery, 0, 10000);
105 2
        } catch (NoSolrConnectionFoundException|SolrCommunicationException $exception) {
106 2
            return [];
107
        }
108 2
        $data = $response->getParsedData();
109
        // @extensionScannerIgnoreLine
110 2
        return $this->documentEscapeService->applyHtmlSpecialCharsOnAllFields($data->response->docs ?? []);
111
    }
112
113
    /**
114
     * @param string $type
115
     * @param int $uid
116
     * @param int $pageId
117
     * @param int $languageId
118
     * @return Document[]|array
119
     * @throws Exception
120
     */
121
    public function findByTypeAndPidAndUidAndLanguageId(
122
        string $type,
123
        int $uid,
124
        int $pageId,
125
        int $languageId
126
    ): array {
127
        try {
128
            $this->initializeSearch($pageId, $languageId);
129
            $recordQuery = $this->queryBuilder->buildRecordQuery($type, $uid, $pageId);
130
            $response = $this->search->search($recordQuery, 0, 10000);
131
        } catch (NoSolrConnectionFoundException|SolrCommunicationException $exception) {
132
            return [];
133
        }
134
        $data = $response->getParsedData();
135
        // @extensionScannerIgnoreLine
136
        return $this->documentEscapeService->applyHtmlSpecialCharsOnAllFields($data->response->docs ?? []);
137
    }
138
139
    /**
140
     * Initializes Search for given language
141
     *
142
     * @param int $pageId
143
     * @param int $languageId
144
     * @throws NoSolrConnectionFoundException
145
     */
146 3
    protected function initializeSearch(int $pageId, int $languageId = 0)
147
    {
148
        /* @var ConnectionManager $connectionManager */
149 3
        $connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
150 3
        $solrConnection = $connectionManager->getConnectionByPageId($pageId, $languageId);
151
152 2
        $this->search = $this->getSearch($solrConnection);
153
    }
154
155
    /**
156
     * Retrieves an instance of the Search object.
157
     *
158
     * @param SolrConnection $solrConnection
159
     * @return Search
160
     */
161 1
    protected function getSearch(SolrConnection $solrConnection): Search
162
    {
163 1
        return  GeneralUtility::makeInstance(Search::class, /** @scrutinizer ignore-type */ $solrConnection);
164
    }
165
}
166