PoliciesExtension   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 33
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 0 18 4
A getExtendedTypes() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\LegalBundle\Form\Extension;
15
16
use Nucleos\ProfileBundle\Form\Type\ProfileFormType;
17
use Nucleos\ProfileBundle\Form\Type\RegistrationFormType;
18
use Symfony\Component\Form\AbstractTypeExtension;
19
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
20
use Symfony\Component\Form\FormBuilderInterface;
21
use Symfony\Component\Form\FormInterface;
22
use Zikula\LegalBundle\Form\Type\AcceptPoliciesType;
23
use Zikula\LegalBundle\Helper\AcceptPoliciesHelper;
24
use Zikula\UsersBundle\Entity\User;
25
26
class PoliciesExtension extends AbstractTypeExtension
27
{
28
    public function __construct(
29
        private readonly AcceptPoliciesHelper $acceptPoliciesHelper
30
    ) {
31
    }
32
33
    public function buildForm(FormBuilderInterface $builder, array $options): void
34
    {
35
        $policies = $this->acceptPoliciesHelper->getActivePolicies();
36
        foreach ($policies as $policyName => $isEnabled) {
37
            if (!$isEnabled) {
38
                continue;
39
            }
40
            $builder->add($policyName . 'Accepted', CheckboxType::class, [
41
                'label' => $policyName,
42
                'getter' => function (User $user, FormInterface $form): bool {
43
                    $getter = 'get' . ucwords((string) $form->getPropertyPath());
44
45
                    return null !== $user->{$getter}();
46
                },
47
                'setter' => function (User $user, bool $state, FormInterface $form): void {
48
                    $setter = 'set' . ucwords((string) $form->getPropertyPath());
49
                    $nowUTC = new \DateTime('now', new \DateTimeZone('UTC'));
50
                    $user->{$setter}($state ? $nowUTC : null);
51
                },
52
            ]);
53
        }
54
    }
55
56
    public static function getExtendedTypes(): iterable
57
    {
58
        return [ProfileFormType::class, RegistrationFormType::class, AcceptPoliciesType::class];
59
    }
60
}
61