Passed
Pull Request — master (#2666)
by Rafael
50:52 queued 08:22
created

OptimizeIndexTask::getCoresToOptimizeIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Task;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
use ApacheSolrForTypo3\Solr\ConnectionManager;
19
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
/**
23
 * Scheduler task to empty the indexes of a site and re-initialize the
24
 * Solr Index Queue thus making the indexer re-index the site.
25
 *
26
 * @author Jens Jacobsen <[email protected]>
27
 */
28
class OptimizeIndexTask extends AbstractSolrTask
29
{
30
    /**
31
     * Cores to optimize.
32
     *
33
     * @var array
34
     */
35
    protected $coresToOptimizeIndex = [];
36
37
    /**
38
     * Optimizes all Solr indexes for selected cores and returns TRUE if the execution was successful
39
     *
40
     * @return bool Returns TRUE on success, FALSE on failure.
41
     * @throws NoSolrConnectionFoundException
42
     * @noinspection PhpMissingReturnTypeInspection
43
     * @noinspection PhpUnused
44
     */
45
    public function execute()
46
    {
47
        $optimizeResult = true;
48
        $solrServers = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionsBySite($this->getSite());
49
        foreach ($solrServers as $solrServer) {
50
            $writeService = $solrServer->getWriteService();
51
            $corePath = $writeService->getCorePath();
52
            if (!in_array($corePath, $this->coresToOptimizeIndex)) {
53
                continue;
54
            }
55
            $result = $writeService->optimizeIndex();
56
            if ($result->getResponse()->getStatusCode() != 200) {
57
                $optimizeResult = false;
58
            }
59
        }
60
        return $optimizeResult;
61
    }
62
63
    /**
64
     * Gets the cores to optimize.
65
     *
66
     * @return array
67
     */
68
    public function getCoresToOptimizeIndex(): array
69
    {
70
        return $this->coresToOptimizeIndex;
71
    }
72
73
    /**
74
     * Sets the cores to optimize.
75
     *
76
     * @param array $coresToOptimizeIndex
77
     */
78
    public function setCoresToOptimizeIndex(array $coresToOptimizeIndex): void
79
    {
80
        $this->coresToOptimizeIndex = $coresToOptimizeIndex;
81
    }
82
83
    /**
84
     * This method is designed to return some additional information about the task,
85
     * that may help to set it apart from other tasks from the same class
86
     * This additional information is used - for example - in the Scheduler's BE module
87
     * This method should be implemented in most task classes
88
     *
89
     * @return string Information to display
90
     * @noinspection PhpMissingReturnTypeInspection
91
     */
92
    public function getAdditionalInformation()
93
    {
94
        $site = $this->getSite();
95
        if (is_null($site)) {
96
            return 'Invalid site configuration for scheduler please re-create the task!';
97
        }
98
99
        $information = 'Site: ' . $this->getSite()->getLabel();
100
        if (!empty($this->coresToOptimizeIndex)) {
101
            $information .= PHP_EOL . 'Corepaths: ' . implode(', ', $this->coresToOptimizeIndex);
102
        }
103
        return $information;
104
    }
105
}
106