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.
Completed
Push — dev-extbase-fluid ( ca9337...5837d7 )
by Alexander
03:40 queued 03:35
created

ReindexCommand::getDocumentsToProceed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 55
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 55
rs 9.296
cc 2
nc 2
nop 2

How to fix   Long Method   

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\Database\ConnectionPool;
20
use TYPO3\CMS\Core\Database\Connection;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Core\Utility\MathUtility;
23
use Kitodo\Dlf\Command\BaseCommand;
24
use Kitodo\Dlf\Common\Document;
25
26
/**
27
 * CLI Command for re-indexing collections into database and Solr.
28
 *
29
 * @author Alexander Bigga <[email protected]>
30
 * @package TYPO3
31
 * @subpackage dlf
32
 * @access public
33
 */
34
class ReindexCommand extends BaseCommand
35
{
36
    /**
37
     * Configure the command by defining the name, options and arguments
38
     *
39
     * @return void
40
     */
41
    public function configure()
42
    {
43
        $this
44
            ->setDescription('Reindex a collection into database and Solr.')
45
            ->setHelp('')
46
            ->addOption(
47
                'dry-run',
48
                null,
49
                InputOption::VALUE_NONE,
50
                'If this option is set, the files will not actually be processed but the location URI is shown.'
51
            )
52
            ->addOption(
53
                'coll',
54
                'c',
55
                InputOption::VALUE_REQUIRED,
56
                'UID of the collection.'
57
            )
58
            ->addOption(
59
                'pid',
60
                'p',
61
                InputOption::VALUE_REQUIRED,
62
                'UID of the page the documents should be added to.'
63
            )
64
            ->addOption(
65
                'solr',
66
                's',
67
                InputOption::VALUE_REQUIRED,
68
                '[UID|index_name] of the Solr core the document should be added to.'
69
            )
70
            ->addOption(
71
                'owner',
72
                'o',
73
                InputOption::VALUE_OPTIONAL,
74
                '[UID|index_name] of the Library which should be set as owner of the documents.'
75
            )
76
            ->addOption(
77
                'all',
78
                'a',
79
                InputOption::VALUE_NONE,
80
                'Reindex all documents on the given page.'
81
            );
82
    }
83
84
    /**
85
     * Executes the command to index the given document to db and solr.
86
     *
87
     * @param InputInterface $input The input parameters
88
     * @param OutputInterface $output The Symfony interface for outputs on console
89
     *
90
     * @return int
91
     */
92
    protected function execute(InputInterface $input, OutputInterface $output)
93
    {
94
        $dryRun = $input->getOption('dry-run') != false ? true : false;
95
96
        $io = new SymfonyStyle($input, $output);
97
        $io->title($this->getDescription());
98
99
        $startingPoint = $this->initializeDocumentRepository($input->getOption('pid'));
100
101
        if ($startingPoint === false) {
102
            $io->error('ERROR: No valid PID (' . $startingPoint . ') given.');
0 ignored issues
show
Bug introduced by
Are you sure $startingPoint of type false|integer can be used in concatenation? ( Ignorable by Annotation )

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

102
            $io->error('ERROR: No valid PID (' . /** @scrutinizer ignore-type */ $startingPoint . ') given.');
Loading history...
103
            exit(1);
104
        }
105
106
        if (
107
            !empty($input->getOption('solr'))
108
            && !is_array($input->getOption('solr'))
109
        ) {
110
            $allSolrCores = $this->getSolrCores($startingPoint);
111
            $solrCoreUid = $this->getSolrCoreUid($allSolrCores, $input->getOption('solr'));
112
113
            // Abort if solrCoreUid is empty or not in the array of allowed solr cores.
114
            if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) {
115
                $output_solrCores = [];
116
                foreach ($allSolrCores as $index_name => $uid) {
117
                    $output_solrCores[] = $uid . ' : ' . $index_name;
118
                }
119
                if (empty($output_solrCores)) {
120
                    $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $startingPoint . ".\n");
121
                    exit(1);
122
                } else {
123
                    $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $output_solrCores) . "\n");
124
                    exit(1);
125
                }
126
            }
127
        } else {
128
            $io->error('ERROR: Required parameter --solr|-s is missing or array.');
129
            exit(1);
130
        }
131
132
        if (!empty($input->getOption('owner'))) {
133
            if (MathUtility::canBeInterpretedAsInteger($input->getOption('owner'))) {
134
                $owner = MathUtility::forceIntegerInRange((int) $input->getOption('owner'), 1);
135
            } else {
136
                $owner = (string) $input->getOption('owner');
137
            }
138
        } else {
139
            $owner = null;
140
        }
141
142
        if (!empty($input->getOption('all'))) {
143
            // Get all documents.
144
            $documents = $this->documentRepository->findAll();
145
        } elseif (
146
            !empty($input->getOption('coll'))
147
            && !is_array($input->getOption('coll'))
148
        ) {
149
            // "coll" may be a single integer or a comma-separated list of integers.
150
            if (empty(array_filter(GeneralUtility::intExplode(',', $input->getOption('coll'), true)))) {
151
                $io->error('ERROR: Parameter --coll|-c is not a valid comma-separated list of collection UIDs.');
152
                exit(1);
153
            }
154
            // Get all documents of given collections.
155
            $documents = $this->documentRepository->findAllByCollectionsLimited(GeneralUtility::intExplode(',', $input->getOption('coll'), true), 0);
156
        } else {
157
            $io->error('ERROR: One of parameters --all|-a or --coll|-c must be given.');
158
            exit(1);
159
        }
160
161
        foreach ($documents as $id => $document) {
162
            $doc = Document::getInstance($document->getUid(), ['storagePid' => $startingPoint], true);
163
            if ($doc->ready) {
164
                if ($dryRun) {
165
                    $io->writeln('DRY RUN: Would index ' . $id . '/' . count($documents) . ' ' . $doc->uid . ' ("' . $doc->location . '") on PID ' . $startingPoint . ' and Solr core ' . $solrCoreUid . '.');
166
                } else {
167
                    if ($io->isVerbose()) {
168
                        $io->writeln(date('Y-m-d H:i:s') . ' Indexing ' . $id . '/' . count($documents) . ' ' . $doc->uid . ' ("' . $doc->location . '") on PID ' . $startingPoint . ' and Solr core ' . $solrCoreUid . '.');
169
                    }
170
                    // ...and save it to the database...
171
                    if (!$doc->save($startingPoint, $solrCoreUid, $owner)) {
172
                        $io->error('ERROR: Document "' . $id . '" not saved and indexed.');
173
                    }
174
                }
175
            } else {
176
                $io->error('ERROR: Document "' . $id . '" could not be loaded.');
177
            }
178
            // Clear document registry to prevent memory exhaustion.
179
            Document::clearRegistry();
180
        }
181
182
        $io->success('All done!');
183
184
        return 0;
185
    }
186
}
187