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

eliashaeussler / typo3-form-consent / 9634187616

23 Jun 2024 02:25PM CUT coverage: 95.438%. Remained the same
9634187616

push

github

eliashaeussler
[TASK] Remove usage of removed FinalizeClassesWithoutChildrenRector

774 of 811 relevant lines covered (95.44%)

14.05 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(
24✔
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
    ) {}
24✔
51

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

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

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

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

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

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

85
        return $consent;
22✔
86
    }
87

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

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

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

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

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

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

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

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