Completed
Pull Request — master (#1083)
by
unknown
16:21
created

ApacheSolrDocumentRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 82
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findOneByPageIdAndByLanguageId() 0 5 1
A findByPageIdAndByLanguageId() 0 10 2
A initializeSearch() 0 14 4
A getQueryForPage() 0 14 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Repository;
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\NoSolrConnectionFoundException;
29
use ApacheSolrForTypo3\Solr\Query;
30
use ApacheSolrForTypo3\Solr\Search;
31
use ApacheSolrForTypo3\Solr\Site;
32
use TYPO3\CMS\Core\SingletonInterface;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35
/**
36
 * Class ApacheSolrDocumentRepository uses connection to Solr Server
37
 */
38
class ApacheSolrDocumentRepository implements SingletonInterface
39
{
40
41
    /**
42
     * Search
43
     *
44
     * @var \ApacheSolrForTypo3\Solr\Search
45
     */
46
    protected $search;
47
48
    /**
49
     * Returns firs found Apache_Solr_Document for current page by given language id.
50
     *
51
     * @param $languageId
52
     * @return \Apache_Solr_Document|false
53
     */
54 1
    public function findOneByPageIdAndByLanguageId($pageId, $languageId)
55
    {
56 1
        $documentCollection = $this->findByPageIdAndByLanguageId($pageId, $languageId);
57 1
        return reset($documentCollection);
58
    }
59
60
    /**
61
     * Returns all found Apache_Solr_Document[] by given page id and language id.
62
     * Returns empty array if nothing found, e.g. if no language or no page(or no index for page) is present.
63
     *
64
     * @param int $pageId
65
     * @param int $languageId
66
     * @return \Apache_Solr_Document[]
67
     */
68 6
    public function findByPageIdAndByLanguageId($pageId, $languageId)
69
    {
70
        try {
71 6
            $this->initializeSearch($pageId, $languageId);
72 2
            $this->search->search($this->getQueryForPage($pageId), 0, 10000);
73 4
        } catch (NoSolrConnectionFoundException $exception) {
74 2
            return [];
75
        }
76 2
        return $this->search->getResultDocumentsEscaped();
77
    }
78
79
    /**
80
     * Initializes Search for given language
81
     *
82
     * @param int $languageId
83
     */
84 5
    protected function initializeSearch($pageId, $languageId = 0)
85
    {
86 5
        if (empty($pageId) || !is_int($pageId)) {
87 1
            throw new \InvalidArgumentException('Invalid page ID = ' . $pageId, 1487332926);
88
        }
89 4
        if (!is_int($languageId)) { // @todo: Check if lang id is defined and present?
90 1
            throw new \InvalidArgumentException('Invalid language ID = ' . $languageId, 1487335178);
91
        }
92
        /* @var $connectionManager ConnectionManager */
93 3
        $connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
94 3
        $solrConnection = $connectionManager->getConnectionByPageId($pageId, $languageId);
95
96 2
        $this->search = GeneralUtility::makeInstance(Search::class, $solrConnection);
97 2
    }
98
99
    /**
100
     * Returns Query for Saearch which finds document for given page.
101
     * Note: The Connection is per language as recommended in ext-solr docs.
102
     *
103
     * @return Query
104
     */
105 1
    protected function getQueryForPage($pageId)
106
    {
107
        /* @var Query $query */
108 1
        $query = GeneralUtility::makeInstance(Query::class, '');
109 1
        $query->setQueryType('standard');
110 1
        $query->useRawQueryString(true);
111 1
        $query->setQueryString('*:*');
112 1
        $query->addFilter('(type:pages AND uid:' . $pageId . ') OR (*:* AND pid:' . $pageId . ' NOT type:pages)');
113 1
        $query->addFilter('siteHash:' . Site::getSiteByPageId($pageId)->getSiteHash());
114 1
        $query->setFieldList('*');
115 1
        $query->setSorting('type asc, title asc');
116
117 1
        return $query;
118
    }
119
}
120