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

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

170
        /** @scrutinizer ignore-call */ 
171
        $listedMetadata = $this->metadataRepository->findByIsListed(true);
Loading history...
171
172
        // get all sortable metadata records
173
        $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

173
        /** @scrutinizer ignore-call */ 
174
        $sortableMetadata = $this->metadataRepository->findByIsSortable(true);
Loading history...
174
175
        // get all documents of given collection
176
        $documents = $this->documentRepository->findSolrByCollection($collection, $this->settings, $searchParams, $listedMetadata);
0 ignored issues
show
Bug introduced by
It seems like $searchParams can also be of type string; however, parameter $searchParams of Kitodo\Dlf\Domain\Reposi...:findSolrByCollection() 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

176
        $documents = $this->documentRepository->findSolrByCollection($collection, $this->settings, /** @scrutinizer ignore-type */ $searchParams, $listedMetadata);
Loading history...
177
178
        $this->view->assign('documents', $documents['documents']);
179
        $this->view->assign('collection', $collection);
180
        $this->view->assign('widgetPage', $widgetPage);
181
        $this->view->assign('lastSearch', $searchParams);
182
        $this->view->assign('sortableMetadata', $sortableMetadata);
183
        $this->view->assign('listedMetadata', $listedMetadata);
184
    }
185
186
    /**
187
     * This is an uncached helper action.
188
     *
189
     * @access protected
190
     *
191
     * @param \Kitodo\Dlf\Domain\Model\Collection $collection: The collection object
192
     *
193
     * @return void
194
     */
195
    public function showSortedAction(\Kitodo\Dlf\Domain\Model\Collection $collection)
196
    {
197
        // if search was triggered, get search parameters from POST variables
198
        $searchParams = $this->getParametersSafely('searchParameter');
199
200
        // output is done by show action
201
        $this->forward('show', null, null, ['searchParameter' => $searchParams, 'collection' => $collection]);
202
203
    }
204
}
205