Passed
Pull Request — master (#15)
by Jeff
08:02
created

UserCrudController::configureFields()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 19
rs 9.4222
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_INDEX === $pageName) {
56
            return [$domain, $name, $enabled, $sendOnly, $admin];
57
        } elseif (Crud::PAGE_DETAIL === $pageName) {
58
            return [$id, $name, $plainPassword, $admin, $enabled, $sendOnly, $quota, $domain];
59
        } elseif (Crud::PAGE_NEW === $pageName) {
60
            return [$domain, $name, $admin, $enabled, $sendOnly, $quota, $plainPassword];
61
        } elseif (Crud::PAGE_EDIT === $pageName) {
62
            return [$domain, $name, $admin, $enabled, $sendOnly, $quota, $plainPassword];
63
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return iterable. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
64
    }
65
66
    public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void
67
    {
68
        $this->passwordService->processUserPassword($entityInstance);
69
70
        parent::updateEntity($entityManager, $entityInstance);
71
    }
72
73
    public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
74
    {
75
        $this->passwordService->processUserPassword($entityInstance);
76
77
        parent::persistEntity($entityManager, $entityInstance);
78
    }
79
}
80