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

eliashaeussler / typo3-form-consent / 10870239851

15 Sep 2024 10:26AM UTC coverage: 94.444% (-1.1%) from 95.545%
10870239851

Pull #308

github

web-flow
Merge 72446869e into 6bd6b6892
Pull Request #308: [FEATURE] Add support for TYPO3 v13.3

3 of 13 new or added lines in 2 files covered. (23.08%)

17 existing lines in 2 files now uncovered.

816 of 864 relevant lines covered (94.44%)

15.03 hits per line

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

86.49
/Classes/Domain/Factory/ConsentFactory.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\Domain\Factory;
25

26
use EliasHaeussler\Typo3FormConsent\Domain;
27
use EliasHaeussler\Typo3FormConsent\Event;
28
use EliasHaeussler\Typo3FormConsent\Service;
29
use EliasHaeussler\Typo3FormConsent\Type;
30
use Psr\EventDispatcher;
31
use TYPO3\CMS\Core;
32
use TYPO3\CMS\Extbase;
33
use TYPO3\CMS\Form;
34

35
/**
36
 * ConsentFactory
37
 *
38
 * @author Elias Häußler <elias@haeussler.dev>
39
 * @license GPL-2.0-or-later
40
 */
41
final class ConsentFactory
42
{
43
    public function __construct(
26✔
44
        private readonly Extbase\Configuration\ConfigurationManagerInterface $configurationManager,
45
        private readonly Core\Context\Context $context,
46
        private readonly EventDispatcher\EventDispatcherInterface $eventDispatcher,
47
        private readonly Type\Transformer\FormRequestTypeTransformer $formRequestTypeTransformer,
48
        private readonly Type\Transformer\FormValuesTypeTransformer $formValuesTypeTransformer,
49
        private readonly Service\HashService $hashService,
50
    ) {}
26✔
51

52
    public function createFromForm(
25✔
53
        Domain\Finishers\FinisherOptions $finisherOptions,
54
        Form\Domain\Finishers\FinisherContext $finisherContext,
55
    ): Domain\Model\Consent {
56
        $formRuntime = $finisherContext->getFormRuntime();
25✔
57
        $submitDate = $this->getSubmitDate();
25✔
58
        $approvalPeriod = $finisherOptions->getApprovalPeriod();
25✔
59

60
        $consent = Core\Utility\GeneralUtility::makeInstance(Domain\Model\Consent::class)
25✔
61
            ->setEmail($finisherOptions->getRecipientAddress())
25✔
62
            ->setDate($submitDate)
25✔
63
            ->setData($this->formValuesTypeTransformer->transform($formRuntime))
25✔
64
            ->setFormPersistenceIdentifier($formRuntime->getFormDefinition()->getPersistenceIdentifier())
25✔
65
            ->setOriginalRequestParameters($this->formRequestTypeTransformer->transform($formRuntime))
25✔
66
            ->setOriginalContentElementUid($this->getCurrentContentElementUid($formRuntime->getRequest()))
25✔
67
            ->setState(Type\ConsentStateType::createNew())
25✔
68
            ->setValidUntil($this->calculateExpiryDate($approvalPeriod, $submitDate))
25✔
69
        ;
25✔
70

71
        if (($storagePid = $finisherOptions->getStoragePid()) > 0) {
24✔
UNCOV
72
            $consent->setPid($storagePid);
×
73
        }
74

75
        $consent->setValidationHash($this->hashService->generate($consent));
24✔
76

77
        // Dispatch ModifyConsent event
78
        $this->eventDispatcher->dispatch(new Event\ModifyConsentEvent($consent, $finisherContext));
24✔
79

80
        // Re-generate validation hash if consent has changed in the meantime
81
        if (!$this->hashService->isValid($consent)) {
24✔
UNCOV
82
            $consent->setValidationHash($this->hashService->generate($consent));
×
83
        }
84

85
        return $consent;
24✔
86
    }
87

88
    private function getSubmitDate(): \DateTime
25✔
89
    {
90
        return new \DateTime('@' . $this->context->getPropertyFromAspect('date', 'timestamp', time()));
25✔
91
    }
92

93
    private function calculateExpiryDate(int $approvalPeriod, \DateTime $submitDate): ?\DateTime
24✔
94
    {
95
        // Early return if invalid approval period is given
96
        if ($approvalPeriod <= 0) {
24✔
UNCOV
97
            return null;
×
98
        }
99

100
        $target = $submitDate->getTimestamp() + $approvalPeriod;
24✔
101

102
        return new \DateTime('@' . $target);
24✔
103
    }
104

105
    private function getCurrentContentElementUid(Extbase\Mvc\RequestInterface $request): int
24✔
106
    {
107
        $contentObjectRenderer = $request->getAttribute('currentContentObject');
24✔
108

109
        // @todo Remove if support for TYPO3 v11 is dropped
110
        if ($contentObjectRenderer === null && \method_exists($this->configurationManager, 'getContentObject')) {
24✔
UNCOV
111
            $contentObjectRenderer = $this->configurationManager->getContentObject();
×
112
        }
113

114
        if ($contentObjectRenderer !== null) {
24✔
115
            return (int)($contentObjectRenderer->data['uid'] ?? 0);
24✔
116
        }
117

UNCOV
118
        return 0;
×
119
    }
120
}
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