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

eliashaeussler / typo3-form-consent / 13956821170

19 Mar 2025 09:12PM UTC coverage: 83.781% (-9.2%) from 93.012%
13956821170

Pull #354

github

web-flow
Merge b5968a39f into 881eb0c1f
Pull Request #354: Mark form finishers as approval/dismissal dependant via form editor

8 of 101 new or added lines in 4 files covered. (7.92%)

780 of 931 relevant lines covered (83.78%)

12.84 hits per line

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

0.0
/Classes/Domain/Variants/ConsentVariantManager.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\Domain\Variants;
25

26
class ConsentVariantManager
27
{
28
    private const CONSENT_VARIANT_IDENTIFIERS = [
29
        'approval' => '1265789653-post-consent-approval-variant',
30
        'dismissal' => '1265789653-post-consent-dismissal-variant'
31
    ];
32
    public const FINISHER_CONSENT_FLAGS = [
33
        'approval' => 'needsApproval',
34
        'dismissal' => 'needsDismissal'
35
    ];
36
    public const CONSENT_VARIANT_CONDITIONS = [
37
        'approval' => 'isConsentApproved()',
38
        'dismissal' => 'isConsentDismissed()'
39
    ];
40

41
    /**
42
     * Used before showing the form definition in editor
43
     * @param array $formDefinition
44
     * @return array
45
     */
NEW
46
    public function streamlineFormFinishers(array $formDefinition): array
×
47
    {
NEW
48
        $consentFinishers = $this->getConsentVariantFinishers($formDefinition);
×
NEW
49
        $formDefinition = $this->mergeFinishers(
×
NEW
50
            $formDefinition,
×
NEW
51
            $consentFinishers
×
NEW
52
        );
×
NEW
53
        return $this->removeConsentVariants($formDefinition);
×
54
    }
55

56
    /**
57
     * Used before saving the form definition
58
     * @param array $form
59
     * @return array
60
     */
NEW
61
    public function addConsentVariants(array $form = []): array
×
62
    {
NEW
63
        if (!$this->formHasConsentFinisher($form)) {
×
NEW
64
            return $form;
×
65
        }
66
        // reset
NEW
67
        $form = $this->removeConsentVariants($form);
×
68

NEW
69
        foreach (['approval', 'dismissal'] as $condition) {
×
NEW
70
            $finishersWithCondition = $this->findFinishersWithConsentCondition(
×
NEW
71
                $form,
×
NEW
72
                self::FINISHER_CONSENT_FLAGS[$condition]
×
NEW
73
            );
×
NEW
74
            $form = $this->createConsentVariant($form, $finishersWithCondition, $condition);
×
NEW
75
            $form = $this->removeFinishersWithConsentConditionFromDefault($form, $finishersWithCondition);
×
76
        }
77

NEW
78
        return $form;
×
79
    }
80

NEW
81
    protected function createConsentVariant(array $form, array $finishersWithApprovalCondition, string $condition): array
×
82
    {
83
        // exit if no finishers in variant
NEW
84
        if (empty($finishersWithApprovalCondition)) {
×
NEW
85
            return $form;
×
86
        }
NEW
87
        $form['variants'][] = [
×
NEW
88
            'identifier' => self::CONSENT_VARIANT_IDENTIFIERS[$condition],
×
NEW
89
            'condition' => self::CONSENT_VARIANT_CONDITIONS[$condition],
×
NEW
90
            'finishers' => $finishersWithApprovalCondition
×
NEW
91
        ];
×
NEW
92
        return $form;
×
93
    }
94

95
    /**
96
     * removes all consent relates form variants, used as reset before rebuilding the variants
97
     * @param array $form
98
     * @return array
99
     */
NEW
100
    protected function removeConsentVariants(array $form = []): array
×
101
    {
NEW
102
        foreach ($form['variants'] ?? [] as $key => $variant) {
×
NEW
103
            if (in_array(($variant['identifier'] ?? ''), self::CONSENT_VARIANT_IDENTIFIERS)) {
×
NEW
104
                unset($form['variants'][$key]);
×
105
            }
106
        }
NEW
107
        $form['variants'] = array_values($form['variants'] ?? []);
×
NEW
108
        if (empty($form['variants'])) {
×
NEW
109
            unset($form['variants']);
×
110
        }
NEW
111
        return $form;
×
112
    }
113

114
    /**
115
     * @param array $form
116
     * @return array
117
     */
NEW
118
    protected function getConsentVariantFinishers(array $form): array
×
119
    {
NEW
120
        $variantFinishers = [];
×
NEW
121
        foreach ($form['variants'] ?? [] as $variant) {
×
NEW
122
            if (in_array(($variant['identifier'] ?? ''), self::CONSENT_VARIANT_IDENTIFIERS)) {
×
NEW
123
                $variantFinishers = array_merge($variantFinishers, $variant['finishers'] ?? []);
×
124
            }
125
        }
NEW
126
        return $variantFinishers;
×
127
    }
128

129
    /**
130
     * Since variants are not shown in the form editor,
131
     * we combine the default finishers with the variants finishers
132
     * @param array $formDefinition
133
     * @param array $variantFinishers
134
     * @return array
135
     */
NEW
136
    protected function mergeFinishers(array $formDefinition, array $variantFinishers): array
×
137
    {
NEW
138
        $formDefinition['finishers'] = array_merge($formDefinition['finishers'] ?? [], $variantFinishers);
×
NEW
139
        return $formDefinition;
×
140
    }
141

142
    /**
143
     * @param array $form
144
     * @param $consentOptionValue
145
     * @return array
146
     */
NEW
147
    protected function findFinishersWithConsentCondition(array $form, $consentOptionValue): array
×
148
    {
NEW
149
        $finishersWithConsentCondition = [];
×
NEW
150
        foreach ((array)($form['finishers'] ?? []) as $finisher) {
×
NEW
151
            $options = (array)($finisher['options'] ?? []);
×
NEW
152
            if (($options['consentCondition'] ?? '') === $consentOptionValue) {
×
NEW
153
                $finishersWithConsentCondition[] = $finisher;
×
154
            }
155
        }
NEW
156
        return $finishersWithConsentCondition;
×
157
    }
158

159
    /**
160
     * @param array $form
161
     * @param $finishersWithConsentCondition
162
     * @return array
163
     */
NEW
164
    protected function removeFinishersWithConsentConditionFromDefault(array $form, $finishersWithConsentCondition): array
×
165
    {
NEW
166
        $form['finishers'] = array_values(array_udiff(
×
NEW
167
            $form['finishers'] ?? [],
×
NEW
168
            $finishersWithConsentCondition,
×
NEW
169
            function ($a, $b) {
×
NEW
170
                return $a <=> $b;
×
NEW
171
            }));
×
NEW
172
        return $form;
×
173
    }
174

175
    /**
176
     * @param array $form
177
     * @return bool
178
     */
NEW
179
    protected function formHasConsentFinisher(array $form): bool
×
180
    {
NEW
181
        foreach ($form['finishers'] ?? [] as $finisher) {
×
NEW
182
            if ($finisher['identifier'] ?? '' === 'Consent') {
×
NEW
183
                return true;
×
184
            }
185
        }
NEW
186
        return false;
×
187
    }
188
}
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

© 2026 Coveralls, Inc