Passed
Push — master ( 39eae3...2e34de )
by Timo
05:33
created

Repository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 101
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 4
A findOneByPageIdAndByLanguageId() 0 5 1
A findByPageIdAndByLanguageId() 0 12 2
A initializeSearch() 0 14 3
A getSearch() 0 4 1
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\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\Query;
33
use ApacheSolrForTypo3\Solr\Search;
34
use ApacheSolrForTypo3\Solr\SolrService;
35
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
36
use ApacheSolrForTypo3\Solr\Util;
37
use TYPO3\CMS\Core\SingletonInterface;
38
use TYPO3\CMS\Core\Utility\GeneralUtility;
39
40
/**
41
 * Class ApacheSolrDocumentRepository uses connection to Solr Server
42
 */
43
class Repository implements SingletonInterface
44
{
45
46
    /**
47
     * Search
48
     *
49
     * @var \ApacheSolrForTypo3\Solr\Search
50
     */
51
    protected $search;
52
53
    /**
54
     * @var DocumentEscapeService
55
     */
56
    protected $documentEscapeService = null;
57
58
    /**
59
     * @var TypoScriptConfiguration|null
60
     */
61
    protected $typoScriptConfiguration = null;
62
63
    /**
64
     * @var QueryBuilder
65
     */
66 6
    protected $queryBuilder;
67
68 6
    /**
69 6
     * Repository constructor.
70 6
     * @param DocumentEscapeService|null $documentEscapeService
71
     * @param QueryBuilder|null $queryBuilder
72
     */
73
    public function __construct(DocumentEscapeService $documentEscapeService = null, TypoScriptConfiguration $typoScriptConfiguration = null, QueryBuilder $queryBuilder = null)
74
    {
75
        $this->typoScriptConfiguration = is_null($typoScriptConfiguration) ? Util::getSolrConfiguration() : $typoScriptConfiguration;
76
        $this->documentEscapeService = is_null($documentEscapeService) ? GeneralUtility::makeInstance(DocumentEscapeService::class, $typoScriptConfiguration) : $documentEscapeService;
77
        $this->queryBuilder = is_null($queryBuilder) ? GeneralUtility::makeInstance(QueryBuilder::class, $this->typoScriptConfiguration) : $queryBuilder;
78 1
    }
79
80 1
    /**
81 1
     * Returns firs found Apache_Solr_Document for current page by given language id.
82
     *
83
     * @param $languageId
84
     * @return \Apache_Solr_Document|false
85
     */
86
    public function findOneByPageIdAndByLanguageId($pageId, $languageId)
87
    {
88
        $documentCollection = $this->findByPageIdAndByLanguageId($pageId, $languageId);
89
        return reset($documentCollection);
90
    }
91
92 6
    /**
93
     * Returns all found Apache_Solr_Document[] by given page id and language id.
94
     * Returns empty array if nothing found, e.g. if no language or no page(or no index for page) is present.
95 6
     *
96 2
     * @param int $pageId
97 4
     * @param int $languageId
98 2
     * @return \Apache_Solr_Document[]
99
     */
100 2
    public function findByPageIdAndByLanguageId($pageId, $languageId)
101 2
    {
102
        try {
103
            $this->initializeSearch($pageId, $languageId);
104
            $pageQuery = $this->queryBuilder->buildPageQuery($pageId);
105
            $response = $this->search->search($pageQuery, 0, 10000);
106
        } catch (NoSolrConnectionFoundException $exception) {
107
            return [];
108
        }
109 5
        $data = $response->getParsedData();
110
        return $this->documentEscapeService->applyHtmlSpecialCharsOnAllFields($data->response->docs);
111 5
    }
112 1
113
    /**
114 4
     * Initializes Search for given language
115 1
     *
116
     * @param int $languageId
117
     */
118 3
    protected function initializeSearch($pageId, $languageId = 0)
119 3
    {
120
        if (!is_int($pageId)) {
121 2
            throw new \InvalidArgumentException('Invalid page ID = ' . $pageId, 1487332926);
122 2
        }
123
        if (!is_int($languageId)) { // @todo: Check if lang id is defined and present?
124
            throw new \InvalidArgumentException('Invalid language ID = ' . $languageId, 1487335178);
125
        }
126
        /* @var $connectionManager ConnectionManager */
127
        $connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
128
        $solrConnection = $connectionManager->getConnectionByPageId($pageId, $languageId);
129
130 1
        $this->search = $this->getSearch($solrConnection);
131
    }
132
133 1
    /**
134 1
     * Retrieves an instance of the Search object.
135
     *
136 1
     * @param SolrService $solrConnection
137 1
     * @return Search
138 1
     */
139 1
    protected function getSearch($solrConnection)
140 1
    {
141 1
        return  GeneralUtility::makeInstance(Search::class, $solrConnection);
142 1
    }
143
}
144