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 — master (#758)
by
unknown
02:49
created

PageViewProxy::main()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 23
c 7
b 0
f 0
dl 0
loc 42
rs 9.2408
cc 5
nc 5
nop 1
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\Plugin\Eid;
14
15
use Kitodo\Dlf\Common\StdOutStream;
16
use Psr\Http\Message\RequestFactoryInterface;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
20
use TYPO3\CMS\Core\Http\JsonResponse;
21
use TYPO3\CMS\Core\Http\RequestFactory;
22
use TYPO3\CMS\Core\Http\Response;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Core\Utility\MathUtility;
25
26
/**
27
 * eID image proxy for plugin 'Page View' of the 'dlf' extension
28
 *
29
 * Supported query parameters:
30
 * - `url` (mandatory): The URL to be proxied
31
 * - `header` (optional)
32
 *   - `1`: Request headers and body (via GET)
33
 *   - `2`: Request only headers (via HEAD)
34
 *
35
 * @author Alexander Bigga <[email protected]>
36
 * @package TYPO3
37
 * @subpackage dlf
38
 * @access public
39
 */
40
class PageViewProxy
41
{
42
    /**
43
     * @var RequestFactoryInterface
44
     */
45
    protected $requestFactory;
46
47
    /**
48
     * @var mixed
49
     */
50
    protected $extConf;
51
52
    public function __construct()
53
    {
54
        $this->requestFactory = GeneralUtility::makeInstance(RequestFactory::class);
55
        $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf');
56
    }
57
58
    /**
59
     * Get headers to be sent to the proxied target.
60
     *
61
     * @return array
62
     */
63
    protected function getRequestHeaders(): array
64
    {
65
        return [
66
            'User-Agent' => $this->extConf['useragent'] ?? 'Kitodo.Presentation Proxy',
67
        ];
68
    }
69
70
    /**
71
     * The main method of the eID script
72
     *
73
     * @access public
74
     *
75
     * @param ServerRequestInterface $request
76
     * @return ResponseInterface
77
     */
78
    public function main(ServerRequestInterface $request)
79
    {
80
        $queryParams = $request->getQueryParams();
81
        $url = (string) ($queryParams['url'] ?? '');
82
83
        if (!GeneralUtility::isValidUrl($url)) {
84
            return new JsonResponse(['message' => 'Did not receive a valid URL.'], 400);
85
        }
86
87
        // This has previously been used as parameter for `GeneralUtility::getUrl()`
88
        // A value of 0 would indicate not to fetch headers, which we'll ignore.
89
        $header = (int) ($queryParams['header'] ?? 1);
90
        $headerOnly = MathUtility::forceIntegerInRange($header, 0, 2, 0) === 2;
91
92
        $httpMethod = $headerOnly ? 'HEAD' : 'GET';
93
        try {
94
            $response = $this->requestFactory->request($url, $httpMethod, [
0 ignored issues
show
Bug introduced by
The method request() does not exist on Psr\Http\Message\RequestFactoryInterface. ( Ignorable by Annotation )

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

94
            /** @scrutinizer ignore-call */ 
95
            $response = $this->requestFactory->request($url, $httpMethod, [

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
                'headers' => $this->getRequestHeaders(),
96
97
                // For performance, don't download content up-front. Rather, we'll
98
                // download and upload simultaneously.
99
                // https://docs.guzzlephp.org/en/6.5/request-options.html#stream
100
                'stream' => true,
101
102
                // Don't throw exceptions when a non-success status code is
103
                // received. We handle these manually.
104
                'http_errors' => false,
105
            ]);
106
        } catch (\Exception $e) {
107
            return new JsonResponse(['message' => 'Could not fetch resource of given URL.'], 500);
108
        }
109
110
        $body = new StdOutStream($response->getBody());
111
112
        return GeneralUtility::makeInstance(Response::class)
113
            ->withStatus($response->getStatusCode())
114
            ->withHeader('Access-Control-Allow-Methods', 'GET')
115
            ->withHeader('Access-Control-Allow-Origin', (string) ($request->getHeaderLine('Origin') ? : '*'))
116
            ->withHeader('Access-Control-Max-Age', '86400')
117
            ->withHeader('Content-Type', (string) $response->getHeader('Content-Type')[0])
118
            ->withHeader('Last-Modified', (string) $response->getHeader('Last-Modified')[0])
119
            ->withBody($body);
120
    }
121
}
122