Completed
Push — master ( ee9c7d...9819c1 )
by Rafael
14:45
created

Repository::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 4
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 2 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\ResultSet\Result\Parser\DocumentEscapeService;
29
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
30
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
31
use ApacheSolrForTypo3\Solr\Query;
32
use ApacheSolrForTypo3\Solr\Search;
33
use ApacheSolrForTypo3\Solr\SolrService;
34
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
35
use ApacheSolrForTypo3\Solr\Util;
36
use TYPO3\CMS\Core\SingletonInterface;
37
use TYPO3\CMS\Core\Utility\GeneralUtility;
38
39
/**
40
 * Class ApacheSolrDocumentRepository uses connection to Solr Server
41
 */
42
class Repository implements SingletonInterface
43
{
44
45
    /**
46
     * Search
47
     *
48
     * @var \ApacheSolrForTypo3\Solr\Search
49
     */
50
    protected $search;
51
52
    /**
53
     * @var DocumentEscapeService
54
     */
55 1
    protected $documentEscapeService = null;
56
57 1
    /**
58 1
     * @var TypoScriptConfiguration|null
59
     */
60
    protected $typoScriptConfiguration = null;
61
62
    /**
63
     * Repository constructor.
64
     * @param DocumentEscapeService|null $documentEscapeService
65
     */
66
    public function __construct(DocumentEscapeService $documentEscapeService = null, TypoScriptConfiguration $typoScriptConfiguration = null)
67
    {
68
        $this->typoScriptConfiguration = is_null($typoScriptConfiguration) ? Util::getSolrConfiguration() : $typoScriptConfiguration;
69 6
        $this->documentEscapeService = is_null($documentEscapeService) ? GeneralUtility::makeInstance(DocumentEscapeService::class, $typoScriptConfiguration) : $documentEscapeService;
70
    }
71
72 6
    /**
73 2
     * Returns firs found Apache_Solr_Document for current page by given language id.
74 4
     *
75 2
     * @param $languageId
76
     * @return \Apache_Solr_Document|false
77 2
     */
78
    public function findOneByPageIdAndByLanguageId($pageId, $languageId)
79
    {
80
        $documentCollection = $this->findByPageIdAndByLanguageId($pageId, $languageId);
81
        return reset($documentCollection);
82
    }
83
84
    /**
85 5
     * Returns all found Apache_Solr_Document[] by given page id and language id.
86
     * Returns empty array if nothing found, e.g. if no language or no page(or no index for page) is present.
87 5
     *
88 1
     * @param int $pageId
89
     * @param int $languageId
90 4
     * @return \Apache_Solr_Document[]
91 1
     */
92
    public function findByPageIdAndByLanguageId($pageId, $languageId)
93
    {
94 3
        try {
95 3
            $this->initializeSearch($pageId, $languageId);
96
            $response = $this->search->search($this->getQueryForPage($pageId), 0, 10000);
97 2
        } catch (NoSolrConnectionFoundException $exception) {
98 2
            return [];
99
        }
100
        $data = $response->getParsedData();
101
        return $this->documentEscapeService->applyHtmlSpecialCharsOnAllFields($data->response->docs);
102
    }
103
104
    /**
105
     * Initializes Search for given language
106 1
     *
107
     * @param int $languageId
108
     */
109 1
    protected function initializeSearch($pageId, $languageId = 0)
110 1
    {
111
        if (!is_int($pageId)) {
112 1
            throw new \InvalidArgumentException('Invalid page ID = ' . $pageId, 1487332926);
113 1
        }
114 1
        if (!is_int($languageId)) { // @todo: Check if lang id is defined and present?
115 1
            throw new \InvalidArgumentException('Invalid language ID = ' . $languageId, 1487335178);
116 1
        }
117 1
        /* @var $connectionManager ConnectionManager */
118 1
        $connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
119 1
        $solrConnection = $connectionManager->getConnectionByPageId($pageId, $languageId);
120
121 1
        $this->search = $this->getSearch($solrConnection);
122
    }
123
124
    /**
125
     * Returns Query for Saearch which finds document for given page.
126
     * Note: The Connection is per language as recommended in ext-solr docs.
127
     *
128
     * @return Query
129
     */
130 1
    protected function getQueryForPage($pageId)
131
    {
132 1
            /** @var $siteRepository SiteRepository */
133
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
134
        $site = $siteRepository->getSiteByPageId($pageId);
135
        /* @var Query $query */
136
        $query = GeneralUtility::makeInstance(Query::class, '');
137
        $query->setQueryType('standard');
138
        $query->useRawQueryString(true);
139
        $query->setQueryString('*:*');
140
        $query->getFilters()->add('(type:pages AND uid:' . $pageId . ') OR (*:* AND pid:' . $pageId . ' NOT type:pages)');
141
        $query->getFilters()->add('siteHash:' . $site->getSiteHash());
142
        $query->getReturnFields()->add('*');
143
        $query->setSorting('type asc, title asc');
144
145
        return $query;
146
    }
147
148
    /**
149
     * Retrieves an instance of the Search object.
150
     *
151
     * @param SolrService $solrConnection
152
     * @return Search
153
     */
154
    protected function getSearch($solrConnection)
155
    {
156
        return  GeneralUtility::makeInstance(Search::class, $solrConnection);
157
    }
158
}
159