Passed
Pull Request — master (#2666)
by Jens
35:27
created

getLanguageService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Task;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2020 Jens Jacobsen <[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\Backend\CoreSelectorField;
29
use ApacheSolrForTypo3\Solr\Backend\SiteSelectorField;
30
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
31
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
32
use TYPO3\CMS\Core\Localization\LanguageService;
33
use TYPO3\CMS\Core\Messaging\FlashMessage;
34
use TYPO3\CMS\Core\Page\PageRenderer;
35
use TYPO3\CMS\Core\Utility\GeneralUtility;
36
use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider;
37
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
38
use TYPO3\CMS\Scheduler\Task\AbstractTask;
39
use TYPO3\CMS\Scheduler\Task\Enumeration\Action;
40
41
/**
42
 * Adds an additional field to specify the Solr server to initialize the index queue for
43
 *
44
 * @author Jens Jacobsen <[email protected]>
45
 */
46
class OptimizeIndexTaskAdditionalFieldProvider extends AbstractAdditionalFieldProvider
47
{
48
    /**
49
     * Default language file of the extension linkvalidator
50
     *
51
     * @var string
52
     */
53
    protected $languageFile = 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf';
54
55
    /**
56
     * Task information
57
     *
58
     * @var array
59
     */
60
    protected $taskInformation;
61
62
    /**
63
     * Scheduler task
64
     *
65
     * @var AbstractTask|ReIndexTask|NULL
66
     */
67
    protected $task = null;
68
69
    /**
70
     * Scheduler Module
71
     *
72
     * @var SchedulerModuleController
73
     */
74
    protected $schedulerModule;
75
76
    /**
77
     * Selected site
78
     *
79
     * @var Site
80
     */
81
    protected $site = null;
82
83
    /**
84
     * SiteRepository
85
     *
86
     * @var SiteRepository
87
     */
88
    protected $siteRepository;
89
90
    /**
91
     * @var PageRenderer
92
     */
93
    protected $pageRenderer = null;
94
95
    /**
96
     * ReIndexTaskAdditionalFieldProvider constructor.
97
     */
98
    public function __construct()
99
    {
100
        $this->siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
101
    }
102
103
    /**
104
     * @param \TYPO3\CMS\Scheduler\Task\AbstractTask|NULL $task
105
     * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule
106
     * @param array $taskInfo
107
     */
108
    protected function initialize(
109
        AbstractTask $task = null,
110
        SchedulerModuleController $schedulerModule,
111
        array $taskInfo = []
112
    )
113
    {
114
        /** @var $task ReIndexTask */
115
        $this->task = $task;
116
        $this->schedulerModule = $schedulerModule;
117
        $this->taskInformation = $taskInfo;
118
119
        $currentAction = $schedulerModule->getCurrentAction();
120
121
        if ($currentAction->equals(Action::EDIT)) {
122
            $this->site = $this->siteRepository->getSiteByRootPageId($task->getRootPageId());
123
        }
124
    }
125
126
    /**
127
     * Used to define fields to provide the Solr server address when adding
128
     * or editing a task.
129
     *
130
     * @param array $taskInfo reference to the array containing the info used in the add/edit form
131
     * @param AbstractTask $task when editing, reference to the current task object. Null when adding.
132
     * @param SchedulerModuleController $schedulerModule reference to the calling object (Scheduler's BE module)
133
     * @return array Array containing all the information pertaining to the additional fields
134
     *                        The array is multidimensional, keyed to the task class name and each field's id
135
     *                        For each field it provides an associative sub-array with the following:
136
     */
137
    public function getAdditionalFields(
138
        array &$taskInfo,
139
        $task,
140
        SchedulerModuleController $schedulerModule
141
    )
142
    {
143
        $additionalFields = [];
144
145
        if (!$this->isTaskInstanceofOptimizeIndexTask($task)) {
146
            return $additionalFields;
147
        }
148
149
        $this->initialize($task, $schedulerModule, $taskInfo);
150
        $siteSelectorField = GeneralUtility::makeInstance(SiteSelectorField::class);
151
152
        $additionalFields['site'] = [
153
            'code' => $siteSelectorField->getAvailableSitesSelector('tx_scheduler[site]', $this->site),
154
            'label' => $this->languageFile . ':field_site',
155
            'cshKey' => '',
156
            'cshLabel' => ''
157
        ];
158
159
        $additionalFields['cores'] = [
160
            'code' => $this->getCoreSelector(),
161
            'label' => $this->languageFile . ':field_cores',
162
            'cshKey' => '',
163
            'cshLabel' => ''
164
        ];
165
166
        return $additionalFields;
167
    }
168
169
    protected function getCoreSelector()
170
    {
171
        $selectorMarkup = $this->getLanguageService()->sL($this->languageFile . ':tasks.validate.selectSiteFirst');
172
173
        if (is_null($this->site)) {
174
            return $selectorMarkup;
175
        }
176
177
        $selectorField = GeneralUtility::makeInstance(CoreSelectorField::class, /** @scrutinizer ignore-type */ $this->site);
178
        $selectorField->setFormElementName('tx_scheduler[cores]');
179
        $selectorField->setSelectedValues($this->task->getCoresToOptimizeIndex());
0 ignored issues
show
Bug introduced by
The method getCoresToOptimizeIndex() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
        $selectorField->setSelectedValues($this->task->/** @scrutinizer ignore-call */ getCoresToOptimizeIndex());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getCoresToOptimizeIndex() does not exist on ApacheSolrForTypo3\Solr\Task\ReIndexTask. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
        $selectorField->setSelectedValues($this->task->/** @scrutinizer ignore-call */ getCoresToOptimizeIndex());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getCoresToOptimizeIndex() does not exist on TYPO3\CMS\Scheduler\Task\AbstractTask. It seems like you code against a sub-type of TYPO3\CMS\Scheduler\Task\AbstractTask such as ApacheSolrForTypo3\Solr\Task\OptimizeIndexTask. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
        $selectorField->setSelectedValues($this->task->/** @scrutinizer ignore-call */ getCoresToOptimizeIndex());
Loading history...
180
        $selectorMarkup = $selectorField->render();
181
182
        return $selectorMarkup;
183
    }
184
185
    /**
186
     * Checks any additional data that is relevant to this task. If the task
187
     * class is not relevant, the method is expected to return TRUE
188
     *
189
     * @param array $submittedData reference to the array containing the data submitted by the user
190
     * @param SchedulerModuleController $schedulerModule reference to the calling object (Scheduler's BE module)
191
     * @return bool True if validation was ok (or selected class is not relevant), FALSE otherwise
192
     */
193
    public function validateAdditionalFields(
194
        array &$submittedData,
195
        SchedulerModuleController $schedulerModule
196
    )
197
    {
198
        $result = true;
199
200
        // validate site
201
        $sites = $this->siteRepository->getAvailableSites();
202
        if (!array_key_exists($submittedData['site'], $sites)) {
203
            $result = false;
204
        }
205
206
        // validate core selection
207
        if (array_key_exists('cores', $submittedData)
208
            && !is_array($submittedData['cores'])
209
        ) {
210
            $this->addMessage(
211
                $this->getLanguageService()->sL($this->languageFile . ':tasks.validate.invalidCores'),
212
                FlashMessage::ERROR
213
            );
214
            $result = false;
215
        }
216
        return $result;
217
    }
218
219
    /**
220
     * Saves any additional input into the current task object if the task
221
     * class matches.
222
     *
223
     * @param array $submittedData array containing the data submitted by the user
224
     * @param AbstractTask $task reference to the current task object
225
     */
226
    public function saveAdditionalFields(
227
        array $submittedData,
228
        AbstractTask $task
229
    )
230
    {
231
        /** @var $task OptimizeIndexTask */
232
        if (!$this->isTaskInstanceofOptimizeIndexTask($task)) {
233
            return;
234
        }
235
236
        $task->setRootPageId($submittedData['site']);
237
238
        $cores = [];
239
        if (!empty($submittedData['cores'])) {
240
            $cores = $submittedData['cores'];
241
        }
242
        $task->setCoresToOptimizeIndex($cores);
243
    }
244
245
    /**
246
     * @return PageRenderer
247
     */
248
    protected function getPageRenderer()
249
    {
250
        if (!isset($this->pageRenderer)) {
251
            $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
252
        }
253
        return $this->pageRenderer;
254
    }
255
256
    /**
257
     * Check that a task is an instance of ReIndexTask
258
     *
259
     * @param AbstractTask $task
260
     * @return boolean
261
     */
262
    protected function isTaskInstanceofOptimizeIndexTask($task)
263
    {
264
        return ($task instanceof OptimizeIndexTask);
265
    }
266
267
    /**
268
     * @return LanguageService
269
     */
270
    protected function getLanguageService()
271
    {
272
        return $GLOBALS['LANG'];
273
    }
274
}
275