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 (#754)
by Alexander
03:43
created

CollectionController::listAction()   C

Complexity

Conditions 12
Paths 73

Size

Total Lines 73
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 42
c 0
b 0
f 0
nc 73
nop 0
dl 0
loc 73
rs 6.9666

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
namespace Kitodo\Dlf\Controller;
13
14
use Kitodo\Dlf\Common\DocumentList;
15
use Kitodo\Dlf\Common\Helper;
16
use Kitodo\Dlf\Common\Solr;
17
use Kitodo\Dlf\Domain\Model\Collection;
18
use Kitodo\Dlf\Domain\Model\Document;
19
use Kitodo\Dlf\Domain\Model\Metadata;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Core\Utility\MathUtility;
22
use Kitodo\Dlf\Domain\Repository\CollectionRepository;
23
use Kitodo\Dlf\Domain\Repository\MetadataRepository;
24
25
class CollectionController extends AbstractController
26
{
27
    /**
28
     * @var CollectionRepository
29
     */
30
    protected $collectionRepository;
31
32
    /**
33
     * @param CollectionRepository $collectionRepository
34
     */
35
    public function injectCollectionRepository(CollectionRepository $collectionRepository)
36
    {
37
        $this->collectionRepository = $collectionRepository;
38
    }
39
40
    /**
41
     * @var MetadataRepository
42
     */
43
    protected $metadataRepository;
44
45
    /**
46
     * @param MetadataRepository $metadataRepository
47
     */
48
    public function injectMetadataRepository(MetadataRepository $metadataRepository)
49
    {
50
        $this->metadataRepository = $metadataRepository;
51
    }
52
53
    /**
54
     * Show a list of collections
55
     *
56
     * @return void
57
     */
58
    public function listAction()
59
    {
60
        $solr = Solr::getInstance($this->settings['solrcore']);
61
62
        if (!$solr->ready) {
63
            $this->logger->error('Apache Solr not available');
64
            return;
65
        }
66
        // We only care about the UID and partOf in the results and want them sorted
67
        $params['fields'] = 'uid,partof';
68
        $params['sort'] = ['uid' => 'asc'];
69
        $collections = [];
70
71
        // Sort collections according to order in plugin flexform configuration
72
        if ($this->settings['collections']) {
73
            $sortedCollections = [];
74
            foreach (GeneralUtility::intExplode(',', $this->settings['collections']) as $uid) {
75
                $sortedCollections[$uid] = $this->collectionRepository->findByUid($uid);
76
            }
77
            $collections = $sortedCollections;
78
        } else {
79
            $collections = $this->collectionRepository->findAll();
80
        }
81
82
        if (count($collections) == 1 && empty($this->settings['dont_show_single'])) {
83
            $this->forward('show', null, null, ['collection' => array_pop($collections)]);
0 ignored issues
show
Bug introduced by
It seems like $collections can also be of type TYPO3\CMS\Extbase\Persistence\QueryResultInterface; however, parameter $array of array_pop() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

83
            $this->forward('show', null, null, ['collection' => array_pop(/** @scrutinizer ignore-type */ $collections)]);
Loading history...
84
        }
85
86
        $processedCollections = [];
87
88
        // Process results.
89
        foreach ($collections as $collection) {
90
            $solr_query = '';
91
            if ($collection->getIndexSearch() != '') {
92
                $solr_query .= '(' . $collection->getIndexSearch() . ')';
93
            } else {
94
                $solr_query .= 'collection:("' . Solr::escapeQuery($collection->getIndexName()) . '")';
95
            }
96
            $params['query'] = $solr_query . ' AND partof:0 AND toplevel:true';
97
            $partOfNothing = $solr->search_raw($params);
98
99
            $params['query'] = $solr_query . ' AND NOT partof:0 AND toplevel:true';
100
            $partOfSomething = $solr->search_raw($params);
101
            // Titles are all documents that are "root" elements i.e. partof == 0
102
            $collectionInfo['titles'] = [];
103
            foreach ($partOfNothing as $doc) {
104
                $collectionInfo['titles'][$doc->uid] = $doc->uid;
105
            }
106
            // Volumes are documents that are both
107
            // a) "leaf" elements i.e. partof != 0
108
            // b) "root" elements that are not referenced by other documents ("root" elements that have no descendants)
109
            $collectionInfo['volumes'] = $collectionInfo['titles'];
110
            foreach ($partOfSomething as $doc) {
111
                $collectionInfo['volumes'][$doc->uid] = $doc->uid;
112
                // If a document is referenced via partof, it’s not a volume anymore.
113
                unset($collectionInfo['volumes'][$doc->partof]);
114
            }
115
116
            // Generate random but unique array key taking priority into account.
117
            do {
118
                $_key = ($collectionInfo['priority'] * 1000) + mt_rand(0, 1000);
119
            } while (!empty($processedCollections[$_key]));
120
121
            $processedCollections[$_key]['collection'] = $collection;
122
            $processedCollections[$_key]['info'] = $collectionInfo;
123
        }
124
125
        // Randomize sorting?
126
        if (!empty($this->settings['randomize'])) {
127
            ksort($processedCollections, SORT_NUMERIC);
128
        }
129
130
        $this->view->assign('collections', $processedCollections);
131
    }
132
133
    /**
134
     * Show a single collection with description and all its documents.
135
     *
136
     * @access protected
137
     *
138
     * @param \Kitodo\Dlf\Domain\Model\Collection $collection: The collection object
139
     *
140
     * @return void
141
     */
142
    public function showAction(\Kitodo\Dlf\Domain\Model\Collection $collection)
143
    {
144
        // Instaniate the Solr. Without Solr present, we can't do anything.
145
        $solr = Solr::getInstance($this->settings['solrcore']);
146
        if (!$solr->ready) {
147
            $this->logger->error('Apache Solr not available');
148
            return;
149
        }
150
151
        // Check if it's a virtual collection with an Solr query set.
152
        if ($collection->getIndexSearch() != '') {
153
            $solr_query = '(' . $collection->getIndexSearch() . ')';
154
        } else {
155
            $solr_query = 'collection:("' . Solr::escapeQuery($collection->getIndexName()) . '") AND toplevel:true';
156
        }
157
158
        // We only fetch the UIDs of the found toplevel documents.
159
        $params['fields'] = 'uid';
160
        $params['sort'] = ['uid' => 'asc'];
161
        $params['query'] = $solr_query;
162
        $solrResult = $solr->search_raw($params);
163
164
        // Initialize array
165
        $documentSet = [];
166
        foreach ($solrResult as $doc) {
167
            if ($doc->uid) {
168
                $documentSet[] = $doc->uid;
169
            }
170
        }
171
        $documentSet = array_unique($documentSet);
172
173
        $this->settings['documentSets'] = implode(',', $documentSet);
174
175
        // Now find document objects for the given UIDs.
176
        $documents = $this->documentRepository->findDocumentsBySettings($this->settings);
177
178
        // If a targetPid is given, the results will be shown by ListView on the target page.
179
        if (!empty($this->settings['targetPid'])) {
180
            $this->redirect('main', 'ListView', null,
181
                [
182
                    'searchParameter' => $searchParams,
183
                    'widgetPage' => $widgetPage,
184
                    'solrcore' => $this->settings['solrcore']
185
                ], $this->settings['targetPid']
186
            );
187
        }
188
189
        // Pagination of Results: Pass the currentPage to the fluid template to calculate current index of search result.
190
        $widgetPage = $this->getParametersSafely('@widget_0');
191
        if (empty($widgetPage)) {
192
            $widgetPage = ['currentPage' => 1];
193
        }
194
195
        // get all sortable metadata records
196
        $sortableMetadata = $this->metadataRepository->findByIsSortable(true);
0 ignored issues
show
Bug introduced by
The method findByIsSortable() does not exist on Kitodo\Dlf\Domain\Repository\MetadataRepository. 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

196
        /** @scrutinizer ignore-call */ 
197
        $sortableMetadata = $this->metadataRepository->findByIsSortable(true);
Loading history...
197
198
        // get all metadata records to be shown in results
199
        $listedMetadata = $this->metadataRepository->findByIsListed(true);
0 ignored issues
show
Bug introduced by
The method findByIsListed() does not exist on Kitodo\Dlf\Domain\Repository\MetadataRepository. 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

199
        /** @scrutinizer ignore-call */ 
200
        $listedMetadata = $this->metadataRepository->findByIsListed(true);
Loading history...
200
201
        $this->view->assign('documents', $documents);
202
        $this->view->assign('collection', $collection);
203
        $this->view->assign('widgetPage', $widgetPage);
204
        $this->view->assign('sortableMetadata', $sortableMetadata);
205
        $this->view->assign('listedMetadata', $listedMetadata);
206
207
    }
208
}
209