Passed
Push — task/2976_TYPO3.11_compatibili... ( 803400...b598e4 )
by Rafael
33:58 queued 15:41
created

AbstractStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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
abstract class AbstractStrategy
39
{
40
41
    /**
42
     * @var Queue
43
     */
44
    protected $queue;
45
46
    /**
47
     * @var ConnectionManager
48
     */
49
    protected $connectionManager;
50
51
    /**
52
     * AbstractStrategy constructor.
53
     * @param Queue|null $queue
54
     * @param ConnectionManager|null $connectionManager
55
     */
56 24
    public function __construct(Queue $queue = null, ConnectionManager $connectionManager = null)
57
    {
58 24
        $this->queue = $queue ?? GeneralUtility::makeInstance(Queue::class);
59 24
        $this->connectionManager = $connectionManager ?? GeneralUtility::makeInstance(ConnectionManager::class);
60 24
    }
61
62
    /**
63
     * Call's the removal of the strategy and afterwards the garbagecollector post processing hook.
64
     *
65
     * @param string $table
66
     * @param int $uid
67
     * @return mixed
68
     */
69 24
    public function removeGarbageOf($table, $uid)
70
    {
71 24
        $this->removeGarbageOfByStrategy($table, $uid);
72 24
        $this->callPostProcessGarbageCollectorHook($table, $uid);
73 24
    }
74
75
    /**
76
     * A implementation of the GarbageCollection strategy is responsible to remove the garbage from
77
     * the indexqueue and from the solr server.
78
     *
79
     * @param string $table
80
     * @param int $uid
81
     */
82
    abstract protected function removeGarbageOfByStrategy($table, $uid);
83
84
    /**
85
     * Deletes a document from solr and from the index queue.
86
     *
87
     * @param string $table
88
     * @param integer $uid
89
     */
90 14
    protected function deleteInSolrAndRemoveFromIndexQueue($table, $uid)
91
    {
92 14
        $this->deleteIndexDocuments($table, $uid);
93 14
        $this->queue->deleteItem($table, $uid);
94 14
    }
95
96
    /**
97
     * Deletes a document from solr and updates the item in the index queue (e.g. on page content updates).
98
     *
99
     * @param string $table
100
     * @param integer $uid
101
     */
102 8
    protected function deleteInSolrAndUpdateIndexQueue($table, $uid)
103
    {
104 8
        $this->deleteIndexDocuments($table, $uid);
105 8
        $this->queue->updateItem($table, $uid);
106 8
    }
107
108
    /**
109
     * Deletes index documents for a given record identification.
110
     *
111
     * @param string $table The record's table name.
112
     * @param int $uid The record's uid.
113
     */
114 24
    protected function deleteIndexDocuments($table, $uid, $language = 0)
115
    {
116
        // record can be indexed for multiple sites
117 24
        $indexQueueItems = $this->queue->getItems($table, $uid);
118 24
        foreach ($indexQueueItems as $indexQueueItem) {
119 21
            $site = $indexQueueItem->getSite();
120 21
            $enableCommitsSetting = $site->getSolrConfiguration()->getEnableCommits();
121 21
            $siteHash = $site->getSiteHash();
122
            // a site can have multiple connections (cores / languages)
123 21
            $solrConnections = $this->connectionManager->getConnectionsBySite($site);
124 21
            if ($language > 0 && isset($solrConnections[$language])) {
125 2
                $solrConnections = [$language => $solrConnections[$language]];
126
            }
127 21
            $this->deleteRecordInAllSolrConnections($table, $uid, $solrConnections, $siteHash, $enableCommitsSetting);
128
        }
129 24
    }
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 21
    protected function deleteRecordInAllSolrConnections($table, $uid, $solrConnections, $siteHash, $enableCommitsSetting)
141
    {
142 21
        foreach ($solrConnections as $solr) {
143 21
            $solr->getWriteService()->deleteByQuery('type:' . $table . ' AND uid:' . (int)$uid . ' AND siteHash:' . $siteHash);
144 21
            if ($enableCommitsSetting) {
145 21
                $solr->getWriteService()->commit(false, false);
146
            }
147
        }
148 21
    }
149
150
    /**
151
     * Calls the registered post processing hooks after the garbageCollection.
152
     *
153
     * @param string $table
154
     * @param int $uid
155
     */
156 24
    protected function callPostProcessGarbageCollectorHook($table, $uid)
157
    {
158 24
        if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessGarbageCollector'] ?? null)) {
159 22
            return;
160
        }
161
162 2
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessGarbageCollector'] as $classReference) {
163 2
            $garbageCollectorPostProcessor = GeneralUtility::makeInstance($classReference);
164
165 2
            if ($garbageCollectorPostProcessor instanceof GarbageCollectorPostProcessor) {
166 2
                $garbageCollectorPostProcessor->postProcessGarbageCollector($table, $uid);
167
            } else {
168
                $message = get_class($garbageCollectorPostProcessor) . ' must implement interface ' .
169
                    GarbageCollectorPostProcessor::class;
170
                throw new \UnexpectedValueException($message, 1345807460);
171
            }
172
        }
173 2
    }
174
}
175