Passed
Pull Request — master (#1151)
by
unknown
19:19
created

validateAdditionalFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 6
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Task;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Ingo Renner <[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\Backend\SiteSelectorField;
28
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
31
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
32
use TYPO3\CMS\Scheduler\Task\AbstractTask;
33
34
/**
35
 * Additional field provider for the index queue worker task
36
 *
37
 * @author Ingo Renner <[email protected]>
38
 */
39
class IndexQueueWorkerTaskAdditionalFieldProvider implements AdditionalFieldProviderInterface
40
{
41
42
    /**
43
     * SiteRepository
44
     *
45
     * @var SiteRepository
46
     */
47
    protected $siteRepository;
48
49
    public function __construct()
50
    {
51
        $this->siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
52
    }
53
54
    /**
55
     * Used to define fields to provide the TYPO3 site to index and number of
56
     * items to index per run when adding or editing a task.
57
     *
58
     * @param array $taskInfo reference to the array containing the info used in the add/edit form
59
     * @param AbstractTask $task when editing, reference to the current task object. Null when adding.
60
     * @param SchedulerModuleController $schedulerModule : reference to the calling object (Scheduler's BE module)
61
     * @return array Array containing all the information pertaining to the additional fields
62
     *                    The array is multidimensional, keyed to the task class name and each field's id
63
     *                    For each field it provides an associative sub-array with the following:
64
     */
65
    public function getAdditionalFields(
66
        array &$taskInfo,
67
        $task,
68
        SchedulerModuleController $schedulerModule
69
    ) {
70
        $additionalFields = [];
71
        $siteSelectorField = GeneralUtility::makeInstance(SiteSelectorField::class);
72
73
        if (!$this->isTaskInstanceofIndexQueueWorkerTask($task)) {
74
            return $additionalFields;
75
        }
76
77
        if ($schedulerModule->CMD == 'add') {
78
            $taskInfo['site'] = null;
79
            $taskInfo['documentsToIndexLimit'] = 50;
80
            $taskInfo['forcedWebRoot'] = '';
81
        }
82
83
        if ($schedulerModule->CMD == 'edit') {
84
            $taskInfo['site'] = $task->getSite();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class TYPO3\CMS\Scheduler\Task\AbstractTask as the method getSite() does only exist in the following sub-classes of TYPO3\CMS\Scheduler\Task\AbstractTask: ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask, ApacheSolrForTypo3\Solr\Task\ReIndexTask. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
85
            $taskInfo['documentsToIndexLimit'] = $task->getDocumentsToIndexLimit();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class TYPO3\CMS\Scheduler\Task\AbstractTask as the method getDocumentsToIndexLimit() does only exist in the following sub-classes of TYPO3\CMS\Scheduler\Task\AbstractTask: ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
86
            $taskInfo['forcedWebRoot'] = $task->getForcedWebRoot();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class TYPO3\CMS\Scheduler\Task\AbstractTask as the method getForcedWebRoot() does only exist in the following sub-classes of TYPO3\CMS\Scheduler\Task\AbstractTask: ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
87
        }
88
89
        $additionalFields['site'] = [
90
            'code' => $siteSelectorField->getAvailableSitesSelector('tx_scheduler[site]',
91
                $taskInfo['site']),
92
            'label' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:field_site',
93
            'cshKey' => '',
94
            'cshLabel' => ''
95
        ];
96
97
        $additionalFields['documentsToIndexLimit'] = [
98
            'code' => '<input type="number" class="form-control" name="tx_scheduler[documentsToIndexLimit]" value="' . htmlspecialchars($taskInfo['documentsToIndexLimit']) . '" />',
99
            'label' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:indexqueueworker_field_documentsToIndexLimit',
100
            'cshKey' => '',
101
            'cshLabel' => ''
102
        ];
103
104
        $additionalFields['forcedWebRoot'] = [
105
            'code' => '<input type="text" class="form-control" name="tx_scheduler[forcedWebRoot]" value="' . htmlspecialchars($taskInfo['forcedWebRoot']) . '" />',
106
            'label' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:indexqueueworker_field_forcedWebRoot',
107
            'cshKey' => '',
108
            'cshLabel' => ''
109
        ];
110
111
        return $additionalFields;
112
    }
113
114
    /**
115
     * Checks any additional data that is relevant to this task. If the task
116
     * class is not relevant, the method is expected to return TRUE
117
     *
118
     * @param array $submittedData reference to the array containing the data submitted by the user
119
     * @param SchedulerModuleController $schedulerModule reference to the calling object (Scheduler's BE module)
120
     * @return bool True if validation was ok (or selected class is not relevant), FALSE otherwise
121
     */
122
    public function validateAdditionalFields(
123
        array &$submittedData,
124
        SchedulerModuleController $schedulerModule
125
    ) {
126
        $result = false;
127
128
        // validate site
129
        $sites = $this->siteRepository->getAvailableSites();
130
        if (array_key_exists($submittedData['site'], $sites)) {
131
            $result = true;
132
        }
133
134
        // escape limit
135
        $submittedData['documentsToIndexLimit'] = intval($submittedData['documentsToIndexLimit']);
136
137
        return $result;
138
    }
139
140
    /**
141
     * Saves any additional input into the current task object if the task
142
     * class matches.
143
     *
144
     * @param array $submittedData array containing the data submitted by the user
145
     * @param AbstractTask $task reference to the current task object
146
     */
147
    public function saveAdditionalFields(
148
        array $submittedData,
149
        AbstractTask $task
150
    ) {
151
        if (!$this->isTaskInstanceofIndexQueueWorkerTask($task)) {
152
            return;
153
        }
154
155
        $task->setSite($this->siteRepository->getSiteByRootPageId($submittedData['site']));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class TYPO3\CMS\Scheduler\Task\AbstractTask as the method setSite() does only exist in the following sub-classes of TYPO3\CMS\Scheduler\Task\AbstractTask: ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask, ApacheSolrForTypo3\Solr\Task\ReIndexTask. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
156
        $task->setDocumentsToIndexLimit($submittedData['documentsToIndexLimit']);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class TYPO3\CMS\Scheduler\Task\AbstractTask as the method setDocumentsToIndexLimit() does only exist in the following sub-classes of TYPO3\CMS\Scheduler\Task\AbstractTask: ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
157
        $task->setForcedWebRoot($submittedData['forcedWebRoot']);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class TYPO3\CMS\Scheduler\Task\AbstractTask as the method setForcedWebRoot() does only exist in the following sub-classes of TYPO3\CMS\Scheduler\Task\AbstractTask: ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
158
    }
159
160
    /**
161
     * Check that a task is an instance of IndexQueueWorkerTask
162
     *
163
     * @param AbstractTask $task
164
     * @return boolean
165
     * @throws \LogicException
166
     */
167
    protected function isTaskInstanceofIndexQueueWorkerTask($task)
168
    {
169
        if ((!is_null($task)) && (!($task instanceof IndexQueueWorkerTask))) {
170
            throw new \LogicException(
171
                '$task must be an instance of IndexQueueWorkerTask, '
172
                .'other instances are not supported.', 1487499814
173
            );
174
        }
175
        return true;
176
    }
177
}
178