Passed
Push — master ( ed38f6...1c87ec )
by Timo
24:44
created

AbstractStrategy::deleteIndexDocuments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 8
cts 9
cp 0.8889
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0054
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Index\Queue\GarbageRemover;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2018 - Timo Hund <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\ConnectionManager;
29
use ApacheSolrForTypo3\Solr\GarbageCollectorPostProcessor;
30
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
31
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34
/**
35
 * An implementation ob a garbage remover strategy is responsible to remove all garbage from the index queue and
36
 * the solr server for a certain table and uid combination.
37
 *
38
 * @package ApacheSolrForTypo3\Solr\Domain\Index\Queue\GarbageRemover
39
 */
40
abstract class AbstractStrategy
41
{
42
43
    /**
44
     * @var Queue
45
     */
46
    protected $queue;
47
48
    /**
49
     * @var ConnectionManager
50
     */
51
    protected $connectionManager;
52
53
    /**
54
     * AbstractStrategy constructor.
55
     * @param Queue|null $queue
56
     * @param ConnectionManager|null $connectionManager
57
     */
58 11
    public function __construct(Queue $queue = null, ConnectionManager $connectionManager = null)
59
    {
60 11
        $this->queue = $queue ?? GeneralUtility::makeInstance(Queue::class);
61 11
        $this->connectionManager = $connectionManager ?? GeneralUtility::makeInstance(ConnectionManager::class);
62 11
    }
63
64
    /**
65
     * Call's the removal of the strategy and afterwards the garbagecollector post processing hook.
66
     *
67
     * @param string $table
68
     * @param int $uid
69
     * @return mixed
70
     */
71 11
    public function removeGarbageOf($table, $uid)
72
    {
73 11
        $this->removeGarbageOfByStrategy($table, $uid);
74 11
        $this->callPostProcessGarbageCollectorHook($table, $uid);
75 11
    }
76
77
    /**
78
     * A implementation of the GarbageCollection strategy is responsible to remove the garbage from
79
     * the indexqueue and from the solr server.
80
     *
81
     * @param string $table
82
     * @param int $uid
83
     * @return mixed
84
     */
85
    abstract protected function removeGarbageOfByStrategy($table, $uid);
86
87
    /**
88
     * Deletes a document from solr and from the index queue.
89
     *
90
     * @param string $table
91
     * @param integer $uid
92
     */
93 7
    protected function deleteInSolrAndRemoveFromIndexQueue($table, $uid)
94
    {
95 7
        $this->deleteIndexDocuments($table, $uid);
96 7
        $this->queue->deleteItem($table, $uid);
97 7
    }
98
99
    /**
100
     * Deletes a document from solr and updates the item in the index queue (e.g. on page content updates).
101
     *
102
     * @param string $table
103
     * @param integer $uid
104
     */
105 4
    protected function deleteInSolrAndUpdateIndexQueue($table, $uid)
106
    {
107 4
        $this->deleteIndexDocuments($table, $uid);
108 4
        $this->queue->updateItem($table, $uid);
109 4
    }
110
111
    /**
112
     * Deletes index documents for a given record identification.
113
     *
114
     * @param string $table The record's table name.
115
     * @param int $uid The record's uid.
116
     */
117 11
    protected function deleteIndexDocuments($table, $uid)
118
    {
119
        // record can be indexed for multiple sites
120 11
        $indexQueueItems = $this->queue->getItems($table, $uid);
121 11
        foreach ($indexQueueItems as $indexQueueItem) {
122 10
            $site = $indexQueueItem->getSite();
123 10
            $enableCommitsSetting = $site->getSolrConfiguration()->getEnableCommits();
124 10
            $siteHash = $site->getSiteHash();
125
            // a site can have multiple connections (cores / languages)
126 10
            $solrConnections = $this->connectionManager->getConnectionsBySite($site);
127 10
            $this->deleteRecordInAllSolrConnections($table, $uid, $solrConnections, $siteHash, $enableCommitsSetting);
128
        }
129 11
    }
130
131
    /**
132
     * Deletes the record in all solr connections from that site.
133
     *
134
     * @param string $table
135
     * @param int $uid
136
     * @param SolrConnection[] $solrConnections
137
     * @param string $siteHash
138
     * @param boolean $enableCommitsSetting
139
     */
140 10
    protected function deleteRecordInAllSolrConnections($table, $uid, $solrConnections, $siteHash, $enableCommitsSetting)
141
    {
142 10
        foreach ($solrConnections as $solr) {
143 10
            $solr->getWriteService()->deleteByQuery('type:' . $table . ' AND uid:' . (int)$uid . ' AND siteHash:' . $siteHash);
144 10
            if ($enableCommitsSetting) {
145 10
                $solr->getWriteService()->commit(false, false);
146
            }
147
        }
148 10
    }
149
150
    /**
151
     * Calls the registered post processing hooks after the garbageCollection.
152
     *
153
     * @param string $table
154
     * @param int $uid
155
     */
156 11
    protected function callPostProcessGarbageCollectorHook($table, $uid)
157
    {
158 11
        if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessGarbageCollector'])) {
159 10
            return;
160
        }
161
162 1
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessGarbageCollector'] as $classReference) {
163 1
            $garbageCollectorPostProcessor = GeneralUtility::makeInstance($classReference);
164
165 1
            if ($garbageCollectorPostProcessor instanceof GarbageCollectorPostProcessor) {
166 1
                $garbageCollectorPostProcessor->postProcessGarbageCollector($table, $uid);
167
            } else {
168
                $message = get_class($garbageCollectorPostProcessor) . ' must implement interface ' .
169
                    GarbageCollectorPostProcessor::class;
170 1
                throw new \UnexpectedValueException($message, 1345807460);
171
            }
172
        }
173
    }
174
}