Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — dev-extbase-fluid (#746)
by Alexander
03:26
created

IndexCommand::execute()   F

Complexity

Conditions 25
Paths 440

Size

Total Lines 121
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 71
nc 440
nop 2
dl 0
loc 121
rs 0.7776
c 0
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
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Command;
14
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Style\SymfonyStyle;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Core\Utility\MathUtility;
21
use Kitodo\Dlf\Command\BaseCommand;
22
use Kitodo\Dlf\Common\Doc;
23
use Kitodo\Dlf\Common\Indexer;
24
use Kitodo\Dlf\Common\Helper;
25
use Kitodo\Dlf\Domain\Model\Document;
26
use Kitodo\Dlf\Domain\Model\Library;
27
28
/**
29
 * CLI Command for indexing single documents into database and Solr.
30
 *
31
 * @author Alexander Bigga <[email protected]>
32
 * @package TYPO3
33
 * @subpackage dlf
34
 * @access public
35
 */
36
class IndexCommand extends BaseCommand
37
{
38
39
    /**
40
     * Configure the command by defining the name, options and arguments
41
     *
42
     * @return void
43
     */
44
    public function configure()
45
    {
46
        $this
47
            ->setDescription('Index single document into database and Solr.')
48
            ->setHelp('')
49
            ->addOption(
50
                'dry-run',
51
                null,
52
                InputOption::VALUE_NONE,
53
                'If this option is set, the files will not actually be processed but the location URI is shown.'
54
            )
55
            ->addOption(
56
                'doc',
57
                'd',
58
                InputOption::VALUE_REQUIRED,
59
                'UID or URL of the document.'
60
            )
61
            ->addOption(
62
                'pid',
63
                'p',
64
                InputOption::VALUE_REQUIRED,
65
                'UID of the page the document should be added to.'
66
            )
67
            ->addOption(
68
                'solr',
69
                's',
70
                InputOption::VALUE_REQUIRED,
71
                '[UID|index_name] of the Solr core the document should be added to.'
72
            )
73
            ->addOption(
74
                'owner',
75
                'o',
76
                InputOption::VALUE_OPTIONAL,
77
                '[UID|index_name] of the Library which should be set as owner of the document.'
78
            );
79
    }
80
81
    /**
82
     * Executes the command to index the given document to db and solr.
83
     *
84
     * @param InputInterface $input The input parameters
85
     * @param OutputInterface $output The Symfony interface for outputs on console
86
     *
87
     * @return int
88
     */
89
    protected function execute(InputInterface $input, OutputInterface $output)
90
    {
91
        $dryRun = $input->getOption('dry-run') != false ? true : false;
92
93
        $io = new SymfonyStyle($input, $output);
94
        $io->title($this->getDescription());
95
96
        $this->initializeRepositories($input->getOption('pid'));
97
98
        if ($this->storagePid == 0) {
99
            $io->error('ERROR: No valid PID (' . $this->storagePid . ') given.');
100
            exit(1);
101
        }
102
103
104
        if (
105
            !empty($input->getOption('solr'))
106
            && !is_array($input->getOption('solr'))
107
        ) {
108
            $allSolrCores = $this->getSolrCores($this->storagePid);
109
            $solrCoreUid = $this->getSolrCoreUid($allSolrCores, $input->getOption('solr'));
110
111
            // Abort if solrCoreUid is empty or not in the array of allowed solr cores.
112
            if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) {
113
                $output_solrCores = [];
114
                foreach ($allSolrCores as $index_name => $uid) {
115
                    $output_solrCores[] = $uid . ' : ' . $index_name;
116
                }
117
                if (empty($output_solrCores)) {
118
                    $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $this->storagePid . ".\n");
119
                    exit(1);
120
                } else {
121
                    $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $output_solrCores) . "\n");
122
                    exit(1);
123
                }
124
            }
125
        } else {
126
            $io->error('ERROR: Required parameter --solr|-s is missing or array.');
127
            exit(1);
128
        }
129
130
        if (
131
            empty($input->getOption('doc'))
132
            || is_array($input->getOption('doc'))
133
            || (
134
                !MathUtility::canBeInterpretedAsInteger($input->getOption('doc'))
135
                && !GeneralUtility::isValidUrl($input->getOption('doc'))
136
            )
137
        ) {
138
            $io->error('ERROR: Required parameter --doc|-d is not a valid document UID or URL.');
139
            exit(1);
140
        }
141
142
        if (!empty($input->getOption('owner'))) {
143
            if (MathUtility::canBeInterpretedAsInteger($input->getOption('owner'))) {
144
                $this->owner = $this->libraryRepository->findByUid(MathUtility::forceIntegerInRange((int) $input->getOption('owner'), 1));
145
            } else {
146
                $this->owner = $this->libraryRepository->findOneByIndexName((string) $input->getOption('owner'));
0 ignored issues
show
Bug introduced by
The method findOneByIndexName() does not exist on Kitodo\Dlf\Domain\Repository\LibraryRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

146
                /** @scrutinizer ignore-call */ 
147
                $this->owner = $this->libraryRepository->findOneByIndexName((string) $input->getOption('owner'));
Loading history...
147
            }
148
        } else {
149
            $this->owner = null;
150
        }
151
152
        // Try to find existing document in database
153
        if (MathUtility::canBeInterpretedAsInteger($input->getOption('doc'))) {
154
155
            $document = $this->documentRepository->findByUid($input->getOption('doc'));
156
157
            if ($document === null) {
158
                $io->error('ERROR: Document with UID "' . $input->getOption('doc') . '" could not be found on PID ' . $this->storagePid . ' .');
159
                exit(1);
160
            } else {
161
                $doc = Doc::getInstance($document->getLocation(), ['storagePid' => $this->storagePid], true);
162
            }
163
164
        } else if (GeneralUtility::isValidUrl($input->getOption('doc'))) {
165
            $doc = Doc::getInstance($input->getOption('doc'), ['storagePid' => $this->storagePid], true);
166
167
            if ($doc->recordId) {
168
                $document = $this->documentRepository->findOneByRecordId($doc->recordId);
0 ignored issues
show
Bug introduced by
The method findOneByRecordId() does not exist on Kitodo\Dlf\Domain\Repository\DocumentRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

168
                /** @scrutinizer ignore-call */ 
169
                $document = $this->documentRepository->findOneByRecordId($doc->recordId);
Loading history...
169
            }
170
171
            if ($document === null) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $document does not seem to be defined for all execution paths leading up to this point.
Loading history...
172
                // create new Document object
173
                $document = $this->objectManager->get(Document::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object\ObjectManager::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

173
                $document = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(Document::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
174
            }
175
176
            // now there must exist a document object
177
            if ($document) {
178
                $document->setLocation($input->getOption('doc'));
179
            }
180
        }
181
182
        if ($doc === null) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $doc does not seem to be defined for all execution paths leading up to this point.
Loading history...
183
            $io->error('ERROR: Document "' . $input->getOption('doc') . '" could not be loaded.');
184
            exit(1);
185
        }
186
187
        $document->setDoc($doc);
188
189
        if ($this->owner !== null) {
190
            $document->setOwner($this->owner);
191
        }
192
193
        $document->setSolrcore($solrCoreUid);
194
195
        if ($dryRun) {
196
            $io->section('DRY RUN: Would index ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . ' and Solr core ' . $solrCoreUid . '.');
197
        } else {
198
            if ($io->isVerbose()) {
199
                $io->section('Indexing ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . ' and Solr core ' . $solrCoreUid . '.');
200
            }
201
            // save to database
202
            $this->saveToDatabase($document);
203
            // add to index
204
            Indexer::add($document, $solrCoreUid);
205
        }
206
207
        $io->success('All done!');
208
209
        return 0;
210
    }
211
212
}
213