Failed Conditions
Push — master ( 0b3886...ea22ef )
by Adrien
04:31
created

DeleteUsers::build()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 29
ccs 18
cts 18
cp 1
rs 9.7333
c 1
b 0
f 0
cc 3
nc 1
nop 0
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Api\Helper;
8
use Application\Model\User;
9
use Ecodev\Felix\Api\Field\FieldInterface;
10
use GraphQL\Type\Definition\Type;
11
12
abstract class DeleteUsers implements FieldInterface
13
{
14 1
    public static function build(): iterable
15
    {
16 1
        yield 'deleteUsers' => fn () => [
17 1
            'type' => Type::nonNull(Type::boolean()),
18 1
            'description' => 'Delete one or several existing user with all its logs, messages and tags',
19 1
            'args' => [
20 1
                'ids' => Type::nonNull(Type::listOf(Type::nonNull(_types()->getId(User::class)))),
21 1
            ],
22 1
            'resolve' => function ($root, array $args): bool {
23 1
                foreach ($args['ids'] as $id) {
24
                    /** @var User $user */
25 1
                    $user = $id->getEntity();
26
27
                    // Check ACL
28 1
                    Helper::throwIfDenied($user, 'delete');
29
30 1
                    $account = $user->getAccount();
31 1
                    if ($account?->getOwner() === $user) {
32 1
                        $account->setName(_tr('Anonyme %id%', ['id' => '#' . $user->getId()]));
33 1
                        $account->setIban('');
34
                    }
35
36
                    // Do it
37 1
                    _em()->remove($user);
38
                }
39
40 1
                _em()->flush();
41
42 1
                return true;
43 1
            },
44 1
        ];
45
    }
46
}
47