UserCrudController::configureFields()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 24
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the mailserver-admin package.
6
 * (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin>
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace App\Controller\Admin;
12
13
use App\Entity\User;
14
use App\Service\PasswordService;
15
use Doctrine\ORM\EntityManagerInterface;
16
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
17
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
18
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
19
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
20
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
21
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
22
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
23
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
24
25
class UserCrudController extends AbstractCrudController
26
{
27
    private PasswordService $passwordService;
28
29
    public function __construct(PasswordService $passwordService)
30
    {
31
        $this->passwordService = $passwordService;
32
    }
33
34
    public static function getEntityFqcn(): string
35
    {
36
        return User::class;
37
    }
38
39
    public function configureCrud(Crud $crud): Crud
40
    {
41
        return $crud->setSearchFields(['name']);
42
    }
43
44
    public function configureFields(string $pageName): iterable
45
    {
46
        $domain = AssociationField::new('domain');
47
        $name = TextField::new('name');
48
        $admin = BooleanField::new('admin');
49
        $enabled = BooleanField::new('enabled');
50
        $sendOnly = BooleanField::new('sendOnly')->setHelp('Send only accounts are not allowed to receive mails');
51
        $quota = IntegerField::new('quota')->setHelp('How much space the account can use (in megabytes)');
52
        $plainPassword = Field::new('plainPassword')->setLabel('Change password');
53
        $id = IdField::new('id', 'ID');
54
55
        if (Crud::PAGE_DETAIL === $pageName) {
56
            return [$id, $name, $plainPassword, $admin, $enabled, $sendOnly, $quota, $domain];
57
        }
58
59
        if (Crud::PAGE_NEW === $pageName) {
60
            return [$domain, $name, $admin, $enabled, $sendOnly, $quota, $plainPassword];
61
        }
62
63
        if (Crud::PAGE_EDIT === $pageName) {
64
            return [$domain, $name, $admin, $enabled, $sendOnly, $quota, $plainPassword];
65
        }
66
67
        return [$domain, $name, $enabled, $sendOnly, $admin];
68
    }
69
70
    public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void
71
    {
72
        $this->passwordService->processUserPassword($entityInstance);
73
74
        parent::updateEntity($entityManager, $entityInstance);
75
    }
76
77
    public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
78
    {
79
        $this->passwordService->processUserPassword($entityInstance);
80
81
        parent::persistEntity($entityManager, $entityInstance);
82
    }
83
}
84