tx_dlf_cli::main()   F
last analyzed

Complexity

Conditions 17
Paths 0

Size

Total Lines 167
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 17
eloc 67
nc 0
nop 0
dl 0
loc 167
rs 2
c 2
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
if (!defined('TYPO3_cliMode')) {
13
14
    die('You cannot run this script directly!');
15
16
}
17
18
/**
19
 * CLI script for the 'dlf' extension.
20
 *
21
 * @author	Sebastian Meyer <[email protected]>
22
 * @package	TYPO3
23
 * @subpackage	tx_dlf
24
 * @access	public
25
 */
26
class tx_dlf_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...
27
28
    /**
29
     * This is the return code.
30
     *
31
     * @var	integer
32
     * @access protected
33
     */
34
    protected $return = 0;
35
36
    /**
37
     * Main function of the script.
38
     *
39
     * @access	public
40
     *
41
     * @return	void
42
     */
43
    public function main() {
44
45
        switch ((string) $this->cli_args['_DEFAULT'][1]) {
46
47
            // (Re-)Index a single document.
48
            case 'index':
49
50
                // Add command line arguments.
51
                $this->cli_options[] = array ('-doc UID/URL', 'UID or (properly encoded) URL of the document.');
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...
52
53
                $this->cli_options[] = array ('-pid UID', 'UID of the page the document should be added to.');
54
55
                $this->cli_options[] = array ('-core UID', 'UID of the Solr core the document should be added to.');
56
57
                // Check the command line arguments.
58
                $this->cli_validateArgs();
59
60
                if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->cli_args['-doc'][0])
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Utility\MathUtility 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...
61
                    && !\TYPO3\CMS\Core\Utility\GeneralUtility::isValidUrl($this->cli_args['-doc'][0])) {
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Utility\GeneralUtility 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...
62
63
                    $this->cli_echo('ERROR: "'.$this->cli_args['-doc'][0].'" is not a valid document UID or URL.'.LF, TRUE);
0 ignored issues
show
Bug introduced by
The constant LF was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
64
65
                    $this->return = 1;
66
67
                }
68
69
                if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->cli_args['-pid'][0])) {
70
71
                    $this->cli_echo('ERROR: "'.$this->cli_args['-pid'][0].'" is not a valid page UID.'.LF, TRUE);
72
73
                    $this->return = 1;
74
75
                }
76
77
                if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->cli_args['-core'][0])) {
78
79
                    $this->cli_echo('ERROR: "'.$this->cli_args['-core'][0].'" is not a valid core UID.'.LF, TRUE);
80
81
                    $this->return = 1;
82
83
                }
84
85
                if ($this->return > 0) {
86
87
                    break;
88
89
                }
90
91
                // Get the document...
92
                $doc = & tx_dlf_document::getInstance($this->cli_args['-doc'][0], $this->cli_args['-pid'][0], TRUE);
93
94
                if ($doc->ready) {
95
96
                    // ...and save it to the database...
97
                    if (!$doc->save(intval($this->cli_args['-pid'][0]), intval($this->cli_args['-core'][0]))) {
98
99
                        $this->cli_echo('ERROR: Document "'.$this->cli_args['-doc'][0].'" not saved and indexed.'.LF, TRUE);
100
101
                        $this->return = 1;
102
103
                    }
104
105
                } else {
106
107
                    $this->cli_echo('ERROR: Document "'.$this->cli_args['-doc'][0].'" could not be loaded.'.LF, TRUE);
108
109
                    $this->return = 1;
110
111
                }
112
113
                break;
114
115
            // Re-index all documents of a collection.
116
            case 'reindex':
117
118
                // Add command line arguments.
119
                $this->cli_options[] = array ('-coll UID', 'UID of the collection.');
120
121
                $this->cli_options[] = array ('-pid UID', 'UID of the page the document should be added to.');
122
123
                $this->cli_options[] = array ('-core UID', 'UID of the Solr core the document should be added to.');
124
125
                // Check the command line arguments.
126
                $this->cli_validateArgs();
127
128
                if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->cli_args['-coll'][0])) {
129
130
                    $this->cli_echo('ERROR: "'.$this->cli_args['-coll'][0].'" is not a valid collection UID.'.LF, TRUE);
131
132
                    $this->return = 1;
133
134
                }
135
136
                if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->cli_args['-pid'][0])) {
137
138
                    $this->cli_echo('ERROR: "'.$this->cli_args['-pid'][0].'" is not a valid page UID.'.LF, TRUE);
139
140
                    $this->return = 1;
141
142
                }
143
144
                if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->cli_args['-core'][0])) {
145
146
                    $this->cli_echo('ERROR: "'.$this->cli_args['-core'][0].'" is not a valid core UID.'.LF, TRUE);
147
148
                    $this->return = 1;
149
150
                }
151
152
                if ($this->return > 0) {
153
154
                    break;
155
156
                }
157
158
                // Get the collection.
159
                $result = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
160
                    'tx_dlf_documents.uid AS uid',
161
                    'tx_dlf_documents',
162
                    'tx_dlf_relations',
163
                    'tx_dlf_collections',
164
                    'AND tx_dlf_collections.uid='.intval($this->cli_args['-coll'][0]).' AND tx_dlf_collections.pid='.intval($this->cli_args['-pid'][0]).' AND tx_dlf_relations.ident='.$GLOBALS['TYPO3_DB']->fullQuoteStr('docs_colls', 'tx_dlf_relations').tx_dlf_helper::whereClause('tx_dlf_documents').tx_dlf_helper::whereClause('tx_dlf_collections'),
165
                    '',
166
                    '',
167
                    ''
168
                );
169
170
                while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
171
172
                    // Get the document...
173
                    $doc = & tx_dlf_document::getInstance($resArray['uid'], $this->cli_args['-pid'][0], TRUE);
174
175
                    if ($doc->ready) {
176
177
                        // ...and save it to the database...
178
                        if (!$doc->save(intval($this->cli_args['-pid'][0]), intval($this->cli_args['-core'][0]))) {
179
180
                            $this->cli_echo('ERROR: Document "'.$resArray['uid'].'" not saved and indexed.'.LF, TRUE);
181
182
                            $this->return = 1;
183
184
                        }
185
186
                    } else {
187
188
                        $this->cli_echo('ERROR: Document "'.$resArray['uid'].'" could not be loaded.'.LF, TRUE);
189
190
                        $this->return = 1;
191
192
                    }
193
194
                    // Clear document registry to prevent memory exhaustion.
195
                    tx_dlf_document::clearRegistry();
196
197
                }
198
199
                break;
200
201
            default:
202
203
                $this->cli_help();
204
205
                break;
206
207
        }
208
209
        exit ($this->return);
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...
210
211
    }
212
213
    public function __construct() {
214
215
        // Set basic information about the script.
216
        $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...
217
            'name' => 'Command Line Interface for Kitodo.Presentation',
218
            'synopsis' => '###OPTIONS###',
219
            'description' => 'Currently the only tasks available are "index" and "reindex".'.LF.'Try "/PATH/TO/TYPO3/cli_dispatch.phpsh dlf TASK" for more options.',
0 ignored issues
show
Bug introduced by
The constant LF was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
220
            'examples' => '/PATH/TO/TYPO3/cli_dispatch.phpsh dlf TASK -ARG1=VALUE1 -ARG2=VALUE2',
221
            'options' => '',
222
            'license' => 'GNU GPL - free software!',
223
            'author' => 'Kitodo. Key to digital objects e.V. <[email protected]>',
224
        );
225
226
        // Run parent constructor.
227
        parent::__construct();
228
229
    }
230
231
}
232
233
$SOBE = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_dlf_cli');
234
235
$SOBE->main();
236