Passed
Branch main (a47c5a)
by AOEPeople
15:53 queued 02:59
created

Tx_FeatureFlag_System_Typo3_Cli::main()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 26
rs 8.9617
1
<?php
2
3
/***************************************************************
4
 *  Copyright notice
5
 *
6
 *  (c) 2016 AOE GmbH <[email protected]>
7
 *
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 3 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
28
/**
29
 * @package FeatureFlag
30
 * @subpackage System_Typo3
31
 */
32
class Tx_FeatureFlag_System_Typo3_Cli extends \TYPO3\CMS\Core\Controller\CommandLineController
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Controller\CommandLineController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
{
34
    /**
35
     * @var string
36
     */
37
    protected $extKey = 'feature_flag';
38
    /**
39
     * @var string
40
     */
41
    protected $prefixId = 'tx_featureflag_system_typo3_cli';
42
    /**
43
     * @var string
44
     */
45
    protected $scriptRelPath = 'Classes/System/Typo3/Cli.php';
46
47
    /**
48
     * @var TYPO3\CMS\Scheduler\Scheduler
49
     */
50
    protected $scheduler;
51
52
    /**
53
     * @var Tx_FeatureFlag_Service
54
     */
55
    protected $service;
56
57
    /**
58
     * constructor
59
     */
60
    public function __construct()
61
    {
62
        parent::__construct();
63
        if (!defined('TYPO3_cliMode')) {
64
            die('Access denied: CLI only.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
65
        }
66
        $this->cli_options = array_merge($this->cli_options, array());
0 ignored issues
show
Bug Best Practice introduced by
The property cli_options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
67
        $this->cli_help = array_merge($this->cli_help, array(
0 ignored issues
show
Bug Best Practice introduced by
The property cli_help does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
68
            'name' => $this->prefixId,
69
            'synopsis' => $this->extKey . ' command',
70
            'description' => 'This script can flag all configured tables by feature flags.',
71
            'examples' => 'typo3/cli_dispatch.phpsh ' . $this->extKey . ' [flagEntries]',
72
            'author' => '(c) 2013 AOE GmbH <[email protected]>',
73
        ));
74
        $this->conf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$this->extKey]);
0 ignored issues
show
Bug Best Practice introduced by
The property conf does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75
76
        $this->scheduler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Scheduler\\Scheduler');
77
78
        $this->service = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class)
79
            ->get(Tx_FeatureFlag_Service::class);
80
    }
81
82
    /**
83
     * @param $argv
84
     * @return int
85
     */
86
    public function main($argv)
87
    {
88
        $this->init();
89
        try {
90
            switch ($this->getAction()) {
91
                case 'activate':
92
                    $this->setFeatureStatus($this->getArgument(), true);
93
                    break;
94
                case 'deactivate':
95
                    $this->setFeatureStatus($this->getArgument(), false);
96
                    break;
97
                case 'flagEntries':
98
                    $this->flagEntries();
99
                    break;
100
                case 'flushCaches':
101
                    $this->flushCaches();
102
                    break;
103
                default:
104
                    $this->cli_help();
105
                    break;
106
            }
107
        } catch (Exception $e) {
108
            return 1;
109
        }
110
111
        return 0;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    private function getAction()
118
    {
119
        return (string)$this->cli_args['_DEFAULT'][1];
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    private function getArgument()
126
    {
127
        return (string)$this->cli_args['_DEFAULT'][2];
128
    }
129
130
    /**
131
     * @return void
132
     */
133
    private function init()
134
    {
135
        $this->cli_validateArgs();
136
    }
137
138
    /**
139
     * check if scheduler is installed
140
     *
141
     * @return boolean
142
     */
143
    private function isSchedulerInstalled()
144
    {
145
        return is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['scheduler']);
146
    }
147
148
    /**
149
     * @return int
150
     */
151
    private function getSchedulerTaskUid()
152
    {
153
        foreach ($this->scheduler->fetchTasksWithCondition() as $task) {
0 ignored issues
show
Bug introduced by
The call to TYPO3\CMS\Scheduler\Sche...tchTasksWithCondition() has too few arguments starting with where. ( Ignorable by Annotation )

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

153
        foreach ($this->scheduler->/** @scrutinizer ignore-call */ fetchTasksWithCondition() as $task) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
154
            if ($task instanceof Tx_FeatureFlag_System_Typo3_Task_FlagEntries) {
155
                $taskUid = $task->getTaskUid();
156
            }
157
        }
158
159
        if (null === $taskUid) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $taskUid does not seem to be defined for all execution paths leading up to this point.
Loading history...
160
            throw new RuntimeException('scheduler task for feature_flag was not found');
161
        }
162
        return $taskUid;
163
    }
164
165
    /**
166
     * @throws RuntimeException
167
     */
168
    private function flagEntries()
169
    {
170
        if ($this->isSchedulerInstalled()) {
171
            $taskUid = $this->getSchedulerTaskUid();
172
            $task = $this->scheduler->fetchTask($taskUid);
173
174
            if ($this->scheduler->isValidTaskObject($task)) {
175
                $this->scheduler->executeTask($task);
176
            } else {
177
                throw new RuntimeException('task-object of feature-flag-task is not valid!');
178
            }
179
        }
180
    }
181
182
    /**
183
     * Enable or disable features. $features can be a comma-separated list of feature names
184
     * @param String $features
185
     */
186
    private function setFeatureStatus($features, $enabled)
187
    {
188
        $features = array_map('trim', explode(',', $features));
189
        foreach ($features as $feature) {
190
            echo $feature;
191
            $this->service->updateFeatureFlag($feature, $enabled);
192
        }
193
        $this->service->flagEntries();
194
    }
195
196
    /**
197
     * Clear all page caches
198
     */
199
    private function flushCaches()
200
    {
201
        /** @var Tx_FeatureFlag_System_Typo3_CacheManager $cacheManager */
202
        $cacheManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class)
203
            ->get(Tx_FeatureFlag_System_Typo3_CacheManager::class);
204
        $cacheManager->clearAllCaches();
205
    }
206
}
207
208
/** @var Tx_FeatureFlag_System_Typo3_Cli $cli */
209
$cli = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Tx_FeatureFlag_System_Typo3_Cli');
210
exit($cli->main($_SERVER['argv']));
211