Passed
Push — master ( 6f1745...57b5b6 )
by
unknown
34:44 queued 14:32
created

ResourcePublicationSlot   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 23
c 1
b 0
f 0
dl 0
loc 47
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getStreamUrl() 0 15 3
A add() 0 6 2
A getPublicUrl() 0 10 4
A has() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Form\Slot;
19
20
use TYPO3\CMS\Core\Core\Environment;
21
use TYPO3\CMS\Core\Resource\Event\GeneratePublicUrlForResourceEvent;
22
use TYPO3\CMS\Core\Resource\File;
23
use TYPO3\CMS\Core\Resource\FileInterface;
24
use TYPO3\CMS\Core\Resource\ProcessedFile;
25
use TYPO3\CMS\Core\Resource\ResourceInterface;
26
use TYPO3\CMS\Core\SingletonInterface;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
use TYPO3\CMS\Core\Utility\PathUtility;
29
30
/**
31
 * A PSR-14 event listener for using FAL resources in public (frontend)
32
 *
33
 * @internal will be renamed at some point.
34
 */
35
class ResourcePublicationSlot implements SingletonInterface
36
{
37
    /**
38
     * @var list<string>
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Form\Slot\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
     */
40
    protected $fileIdentifiers = [];
41
42
    public function getPublicUrl(GeneratePublicUrlForResourceEvent $event): void
43
    {
44
        $resource = $event->getResource();
45
        if (!$resource instanceof FileInterface
46
            || !$this->has($resource)
47
            || $event->getStorage()->getDriverType() !== 'Local'
48
        ) {
49
            return;
50
        }
51
        $event->setPublicUrl($this->getStreamUrl($event->getResource()));
52
    }
53
54
    public function add(FileInterface $resource): void
55
    {
56
        if ($this->has($resource)) {
57
            return;
58
        }
59
        $this->fileIdentifiers[] = $resource->getIdentifier();
60
    }
61
62
    public function has(FileInterface $resource): bool
63
    {
64
        return in_array($resource->getIdentifier(), $this->fileIdentifiers, true);
0 ignored issues
show
Bug introduced by
$this->fileIdentifiers of type TYPO3\CMS\Form\Slot\list is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

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

64
        return in_array($resource->getIdentifier(), /** @scrutinizer ignore-type */ $this->fileIdentifiers, true);
Loading history...
65
    }
66
67
    protected function getStreamUrl(ResourceInterface $resource): string
68
    {
69
        $queryParameterArray = ['eID' => 'dumpFile', 't' => ''];
70
        if ($resource instanceof File) {
71
            $queryParameterArray['f'] = $resource->getUid();
72
            $queryParameterArray['t'] = 'f';
73
        } elseif ($resource instanceof ProcessedFile) {
74
            $queryParameterArray['p'] = $resource->getUid();
75
            $queryParameterArray['t'] = 'p';
76
        }
77
78
        $queryParameterArray['token'] = GeneralUtility::hmac(implode('|', $queryParameterArray), 'resourceStorageDumpFile');
79
        $publicUrl = GeneralUtility::locationHeaderUrl(PathUtility::getAbsoluteWebPath(Environment::getPublicPath() . '/index.php'));
80
        $publicUrl .= '?' . http_build_query($queryParameterArray, '', '&', PHP_QUERY_RFC3986);
81
        return $publicUrl;
82
    }
83
}
84