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
Push — master ( e8de59...244c2c )
by
unknown
04:17
created

isIiifManifestWithNewspaperRelatedType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\ExpressionLanguage;
14
15
use Kitodo\Dlf\Common\AbstractDocument;
16
use Kitodo\Dlf\Common\IiifManifest;
17
use Kitodo\Dlf\Domain\Model\Document;
18
use Kitodo\Dlf\Domain\Repository\DocumentRepository;
19
use Psr\Log\LoggerAwareInterface;
20
use Psr\Log\LoggerAwareTrait;
21
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
22
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
23
use TYPO3\CMS\Core\ExpressionLanguage\RequestWrapper;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Core\Utility\MathUtility;
26
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
27
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
28
use TYPO3\CMS\Extbase\Object\ObjectManager;
29
30
/**
31
 * Provider class for additional "getDocumentType" function to the ExpressionLanguage.
32
 *
33
 * @package TYPO3
34
 * @subpackage dlf
35
 *
36
 * @access public
37
 */
38
class DocumentTypeFunctionProvider implements ExpressionFunctionProviderInterface, LoggerAwareInterface
39
{
40
    use LoggerAwareTrait;
41
42
    /**
43
     * @access public
44
     *
45
     * @return ExpressionFunction[] An array of Function instances
46
     */
47
    public function getFunctions(): array
48
    {
49
        return [
50
            $this->getDocumentTypeFunction(),
51
        ];
52
    }
53
54
    /**
55
     * This holds the current document
56
     *
57
     * @var Document
58
     * @access protected
59
     */
60
    protected Document $document;
61
62
    /**
63
     * @var ConfigurationManager
64
     */
65
    protected $configurationManager;
66
67
    public function injectConfigurationManager(ConfigurationManager $configurationManager): void
68
    {
69
        $this->configurationManager = $configurationManager;
70
    }
71
72
    /**
73
     * @var DocumentRepository
74
     */
75
    protected $documentRepository;
76
77
    /**
78
     * @param DocumentRepository $documentRepository
79
     */
80
    public function injectDocumentRepository(DocumentRepository $documentRepository): void
81
    {
82
        $this->documentRepository = $documentRepository;
83
    }
84
    /**
85
     * Initialize the extbase repositories
86
     *
87
     * @access protected
88
     *
89
     * @param int $storagePid The storage pid
90
     *
91
     * @return void
92
     */
93
    protected function initializeRepositories(int $storagePid): void
94
    {
95
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
96
        // TODO: change it as it is deprecated since 10.4 and will be removed in 12.x
97
        // TODO: necessary to test calendar view after updating this code
98
        $configurationManager = $objectManager->get(ConfigurationManager::class);
99
        $this->injectConfigurationManager($configurationManager);
100
        $frameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
101
        $frameworkConfiguration['persistence']['storagePid'] = MathUtility::forceIntegerInRange((int) $storagePid, 0);
102
        $this->configurationManager->setConfiguration($frameworkConfiguration);
103
        $this->documentRepository = GeneralUtility::makeInstance(DocumentRepository::class);
104
    }
105
106
    /**
107
     * Shortcut function to access field values
108
     *
109
     * @access protected
110
     *
111
     * @return ExpressionFunction
112
     */
113
    protected function getDocumentTypeFunction(): ExpressionFunction
114
    {
115
        return new ExpressionFunction(
116
            'getDocumentType',
117
            function()
118
            {
119
                // Not implemented, we only use the evaluator
120
            },
121
            function($arguments, $cPid)
122
            {
123
                /** @var RequestWrapper $requestWrapper */
124
                $requestWrapper = $arguments['request'];
125
                $queryParams = $requestWrapper->getQueryParams();
126
127
                $type = 'undefined';
128
129
                // It happens that $queryParams is an empty array or does not contain a key 'tx_dlf'
130
                // in case of other contexts. In this case we have to return here to avoid log messages.
131
                if (empty($queryParams) || !isset($queryParams['tx_dlf'])) {
132
                    return $type;
133
                }
134
135
                // object type if model parameter is not empty so we assume that it is a 3d object
136
                if (!empty($queryParams['tx_dlf']['model'])) {
137
                    return 'object';
138
                }
139
140
                // Load document with current plugin parameters.
141
                $this->loadDocument($queryParams['tx_dlf'], $cPid);
142
                if (!isset($this->document) || $this->document->getCurrentDocument() === null) {
143
                    return $type;
144
                }
145
146
                // Set PID for metadata definitions.
147
                $this->document->getCurrentDocument()->cPid = $cPid;
148
149
                $metadata = $this->document->getCurrentDocument()->getToplevelMetadata($cPid);
150
151
                if (!empty($metadata['type'][0])
152
                    && !$this->isIiifManifestWithNewspaperRelatedType($metadata['type'][0])) {
153
                    $type = $metadata['type'][0];
154
                }
155
156
                return $type;
157
            });
158
    }
159
160
    /**
161
     * Loads the current document into $this->document
162
     *
163
     * @access protected
164
     *
165
     * @param array $requestData The request data
166
     * @param int $pid Storage Pid
167
     *
168
     * @return void
169
     */
170
    protected function loadDocument(array $requestData, int $pid): void
171
    {
172
        // Try to get document format from database
173
        if (!empty($requestData['id'])) {
174
            $this->initializeRepositories($pid);
175
            $doc = null;
176
            if (MathUtility::canBeInterpretedAsInteger($requestData['id'])) {
177
                // find document from repository by uid
178
                $this->document = $this->documentRepository->findOneByIdAndSettings((int) $requestData['id'], ['storagePid' => $pid]);
179
                if ($this->document) {
180
                    $doc = AbstractDocument::getInstance($this->document->getLocation(), ['storagePid' => $pid], true);
181
                } else {
182
                    $this->logger->error('Invalid UID "' . $requestData['id'] . '" or PID "' . $pid . '" for document loading');
183
                }
184
            } elseif (GeneralUtility::isValidUrl($requestData['id'])) {
185
                $doc = AbstractDocument::getInstance($requestData['id'], ['storagePid' => $pid], true);
186
                if ($doc !== null) {
187
                    if ($doc->recordId) {
188
                        $this->document = $this->documentRepository->findOneByRecordId($doc->recordId);
189
                    }
190
                    if (!isset($this->document)) {
191
                        // create new dummy Document object
192
                        $this->document = GeneralUtility::makeInstance(Document::class);
193
                    }
194
                    $this->document->setLocation($requestData['id']);
195
                } else {
196
                    $this->logger->error('Invalid location given "' . $requestData['id'] . '" for document loading');
197
                }
198
            }
199
            if ($this->document !== null && $doc !== null) {
200
                $this->document->setCurrentDocument($doc);
201
            }
202
        } elseif (!empty($requestData['recordId'])) {
203
            $this->document = $this->documentRepository->findOneByRecordId($requestData['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

203
            /** @scrutinizer ignore-call */ 
204
            $this->document = $this->documentRepository->findOneByRecordId($requestData['recordId']);
Loading history...
204
            if ($this->document !== null) {
205
                $doc = AbstractDocument::getInstance($this->document->getLocation(), ['storagePid' => $pid], true);
206
                if ($doc !== null) {
207
                    $this->document->setCurrentDocument($doc);
208
                } else {
209
                    $this->logger->error('Failed to load document with record ID "' . $requestData['recordId'] . '"');
210
                }
211
            }
212
        } else {
213
            $this->logger->error('Empty UID or invalid PID "' . $pid . '" for document loading');
214
        }
215
    }
216
217
    /**
218
     * Check if is IIIF Manifest with newspaper related type.
219
     *
220
     * Calendar plugin does not support IIIF (yet). Abort for all newspaper related types.
221
     *
222
     * @access private
223
     *
224
     * @param string $type The metadata type
225
     * @return bool
226
     */
227
    private function isIiifManifestWithNewspaperRelatedType(string $type): bool
228
    {
229
        return ($this->document->getCurrentDocument() instanceof IiifManifest
230
            && in_array($type, ['newspaper', 'ephemera', 'year', 'issue']));
231
    }
232
}
233