• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / typo3-form-consent / 14044237598

24 Mar 2025 07:31PM UTC coverage: 96.506% (+3.5%) from 93.012%
14044237598

push

github

web-flow
[TASK] Update all dependencies

801 of 830 relevant lines covered (96.51%)

15.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.94
/Classes/Type/Transformer/FormRequestTypeTransformer.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "form_consent".
7
 *
8
 * Copyright (C) 2021-2025 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\Typo3FormConsent\Type\Transformer;
25

26
use EliasHaeussler\Typo3FormConsent\Type;
27
use TYPO3\CMS\Core;
28
use TYPO3\CMS\Extbase;
29
use TYPO3\CMS\Form;
30

31
/**
32
 * FormRequestTypeTransformer
33
 *
34
 * @author Elias Häußler <elias@haeussler.dev>
35
 * @license GPL-2.0-or-later
36
 */
37
final class FormRequestTypeTransformer implements TypeTransformer
38
{
39
    /**
40
     * @return Type\JsonType<string, array<string, array<string, mixed>>>
41
     * @throws \JsonException
42
     */
43
    public function transform(Form\Domain\Runtime\FormRuntime $formRuntime): Type\JsonType
25✔
44
    {
45
        $request = $formRuntime->getRequest();
25✔
46
        $pluginNamespace = strtolower('tx_' . $request->getControllerExtensionName() . '_' . $request->getPluginName());
25✔
47

48
        // Handle submitted form values
49
        $requestParameters = [];
25✔
50
        if (\is_array($request->getParsedBody())) {
25✔
51
            $requestParameters = $request->getParsedBody();
25✔
52
        }
53

54
        // Handle uploaded files
55
        $uploadedFiles = $request->getUploadedFiles();
25✔
56
        array_walk_recursive($uploadedFiles, function (&$value, string $elementIdentifier) use ($formRuntime): void {
25✔
57
            $file = $formRuntime[$elementIdentifier];
4✔
58
            if ($file instanceof Extbase\Domain\Model\FileReference || $file instanceof Core\Resource\FileReference) {
4✔
59
                $value = $this->transformUploadedFile($file);
4✔
60
            }
61
        });
25✔
62

63
        // Prepend plugin namespace to uploaded files array if not exists
64
        // This is necessary since TYPO3 12.0, see https://github.com/typo3/typo3/commit/02198ea257b9f03f910b3b120392ab63fe792a8b
65
        if (!isset($uploadedFiles[$pluginNamespace])) {
25✔
66
            $uploadedFiles = [
25✔
67
                $pluginNamespace => $uploadedFiles,
25✔
68
            ];
25✔
69
        }
70

71
        Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($requestParameters, $uploadedFiles);
25✔
72

73
        return Type\JsonType::fromArray($requestParameters);
25✔
74
    }
75

76
    /**
77
     * @return array{submittedFile: array{resourcePointer: string}}
78
     */
79
    private function transformUploadedFile(Core\Resource\FileReference|Extbase\Domain\Model\FileReference $file): array
4✔
80
    {
81
        if ($file instanceof Extbase\Domain\Model\FileReference) {
4✔
82
            $file = $file->getOriginalResource();
4✔
83
        }
84

85
        $file = $file->getOriginalFile();
4✔
86
        $resourcePointer = 'file:' . $file->getUid();
4✔
87

88
        if (class_exists(Core\Crypto\HashService::class)) {
4✔
89
            $hashService = Core\Utility\GeneralUtility::makeInstance(Core\Crypto\HashService::class);
4✔
90
            $hashedResourcePointer = $hashService->appendHmac($resourcePointer, Form\Security\HashScope::ResourcePointer->prefix());
4✔
91
        } else {
92
            // @todo Remove once support for TYPO3 v12 is dropped
93
            $hashService = Core\Utility\GeneralUtility::makeInstance(Extbase\Security\Cryptography\HashService::class);
×
94
            $hashedResourcePointer = $hashService->appendHmac($resourcePointer);
×
95
        }
96

97
        return [
4✔
98
            'submittedFile' => [
4✔
99
                'resourcePointer' => $hashedResourcePointer,
4✔
100
            ],
4✔
101
        ];
4✔
102
    }
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc