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

eliashaeussler / typo3-form-consent / 8314210214

17 Mar 2024 08:44AM UTC coverage: 95.358% (-1.4%) from 96.782%
8314210214

push

github

eliashaeussler
[RELEASE] Release of EXT:form_consent 2.1.0

760 of 797 relevant lines covered (95.36%)

14.05 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-2024 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 JsonException;
28
use TYPO3\CMS\Core;
29
use TYPO3\CMS\Extbase;
30
use TYPO3\CMS\Form;
31

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

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

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

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

72
        Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($requestParameters, $uploadedFiles);
22✔
73

74
        return Type\JsonType::fromArray($requestParameters);
22✔
75
    }
76

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

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

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

98
        return [
3✔
99
            'submittedFile' => [
3✔
100
                'resourcePointer' => $hashedResourcePointer,
3✔
101
            ],
3✔
102
        ];
3✔
103
    }
104
}
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