Passed
Push — master ( 35f5e1...94f5b1 )
by Timo
34:40
created

isTaskInstanceofIndexQueueWorkerTask()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 3
cp 0
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
crap 12
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\Site;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
30
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
31
use TYPO3\CMS\Scheduler\Task\AbstractTask;
32
33
/**
34
 * Additional field provider for the index queue worker task
35
 *
36
 * @author Ingo Renner <[email protected]>
37
 */
38
class IndexQueueWorkerTaskAdditionalFieldProvider implements AdditionalFieldProviderInterface
39
{
40
41
    /**
42
     * Used to define fields to provide the TYPO3 site to index and number of
43
     * items to index per run when adding or editing a task.
44
     *
45
     * @param array $taskInfo reference to the array containing the info used in the add/edit form
46
     * @param AbstractTask $task when editing, reference to the current task object. Null when adding.
47
     * @param SchedulerModuleController $schedulerModule : reference to the calling object (Scheduler's BE module)
48
     * @return array Array containing all the information pertaining to the additional fields
49
     *                    The array is multidimensional, keyed to the task class name and each field's id
50
     *                    For each field it provides an associative sub-array with the following:
51
     */
52
    public function getAdditionalFields(
53
        array &$taskInfo,
54
        $task,
55
        SchedulerModuleController $schedulerModule
56
    ) {
57
        $additionalFields = [];
58
59
        $this->isTaskInstanceofIndexQueueWorkerTask($task);
60
61
        if ($schedulerModule->CMD == 'add') {
62
            $taskInfo['site'] = null;
63
            $taskInfo['documentsToIndexLimit'] = 50;
64
            $taskInfo['forcedWebRoot'] = '';
65
        }
66
67
        if ($schedulerModule->CMD == 'edit') {
68
            $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...
69
            $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...
70
            $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...
71
        }
72
73
        $additionalFields['site'] = [
74
            'code' => Site::getAvailableSitesSelector('tx_scheduler[site]',
75
                $taskInfo['site']),
76
            'label' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:field_site',
77
            'cshKey' => '',
78
            'cshLabel' => ''
79
        ];
80
81
        $additionalFields['documentsToIndexLimit'] = [
82
            'code' => '<input type="number" class="form-control" name="tx_scheduler[documentsToIndexLimit]" value="' . htmlspecialchars($taskInfo['documentsToIndexLimit']) . '" />',
83
            'label' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:indexqueueworker_field_documentsToIndexLimit',
84
            'cshKey' => '',
85
            'cshLabel' => ''
86
        ];
87
88
        $additionalFields['forcedWebRoot'] = [
89
            'code' => '<input type="text" class="form-control" name="tx_scheduler[forcedWebRoot]" value="' . htmlspecialchars($taskInfo['forcedWebRoot']) . '" />',
90
            'label' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:indexqueueworker_field_forcedWebRoot',
91
            'cshKey' => '',
92
            'cshLabel' => ''
93
        ];
94
95
        return $additionalFields;
96
    }
97
98
    /**
99
     * Checks any additional data that is relevant to this task. If the task
100
     * class is not relevant, the method is expected to return TRUE
101
     *
102
     * @param array $submittedData reference to the array containing the data submitted by the user
103
     * @param SchedulerModuleController $schedulerModule reference to the calling object (Scheduler's BE module)
104
     * @return bool True if validation was ok (or selected class is not relevant), FALSE otherwise
105
     */
106
    public function validateAdditionalFields(
107
        array &$submittedData,
108
        SchedulerModuleController $schedulerModule
109
    ) {
110
        $result = false;
111
112
        // validate site
113
        $sites = Site::getAvailableSites();
114
        if (array_key_exists($submittedData['site'], $sites)) {
115
            $result = true;
116
        }
117
118
        // escape limit
119
        $submittedData['documentsToIndexLimit'] = intval($submittedData['documentsToIndexLimit']);
120
121
        return $result;
122
    }
123
124
    /**
125
     * Saves any additional input into the current task object if the task
126
     * class matches.
127
     *
128
     * @param array $submittedData array containing the data submitted by the user
129
     * @param AbstractTask $task reference to the current task object
130
     */
131
    public function saveAdditionalFields(
132
        array $submittedData,
133
        AbstractTask $task
134
    ) {
135
        $this->isTaskInstanceofIndexQueueWorkerTask($task);
136
137
        $task->setSite(GeneralUtility::makeInstance(Site::class, $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...
138
        $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...
139
        $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...
140
    }
141
142
    /**
143
     * Check that a task is an instance of IndexQueueWorkerTask
144
     *
145
     * @param $task
146
     * @throws \LogicException
147
     */
148
    protected function isTaskInstanceofIndexQueueWorkerTask($task)
149
    {
150
        if ((!is_null($task)) && (!($task instanceof IndexQueueWorkerTask))) {
151
            throw new \LogicException(
152
                '$task must be an instance of IndexQueueWorkerTask, '
153
                .'other instances are not supported.', 1487499814
154
            );
155
        }
156
    }
157
}
158