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:00
created

CollectionController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 72
c 2
b 0
f 0
dl 0
loc 178
rs 10
wmc 19

5 Methods

Rating   Name   Duplication   Size   Complexity  
A injectMetadataRepository() 0 3 1
C listAction() 0 73 12
A injectCollectionRepository() 0 3 1
A showAction() 0 43 4
A showSortedAction() 0 7 1
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
        $searchParams = $this->getParametersSafely('searchParameter');
145
146
        // Instaniate the Solr. Without Solr present, we can't do anything.
147
        $solr = Solr::getInstance($this->settings['solrcore']);
148
        if (!$solr->ready) {
149
            $this->logger->error('Apache Solr not available');
150
            return;
151
        }
152
153
        // Pagination of Results: Pass the currentPage to the fluid template to calculate current index of search result.
154
        $widgetPage = $this->getParametersSafely('@widget_0');
155
        if (empty($widgetPage)) {
156
            $widgetPage = ['currentPage' => 1];
157
        }
158
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
                    'collection' => $collection,
165
                    'widgetPage' => $widgetPage
166
                ], $this->settings['targetPid']
167
            );
168
        }
169
170
        // get all metadata records to be shown in results
171
        $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

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

174
        /** @scrutinizer ignore-call */ 
175
        $sortableMetadata = $this->metadataRepository->findByIsSortable(true);
Loading history...
175
176
        // get all documents of given collection
177
        $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

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