Failed Conditions
Push — master ( ce2a97...d73709 )
by Adrien
13:38 queued 11:33
created

UpdateUser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 32.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 31
dl 0
loc 54
ccs 11
cts 34
cp 0.3235
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 38 3
A privateInformation() 0 10 2
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 Application\Service\MessageQueuer;
10
use Ecodev\Felix\Api\Field\FieldInterface;
11
use Ecodev\Felix\Service\Mailer;
12
use GraphQL\Type\Definition\Type;
13
use Mezzio\Session\SessionInterface;
14
15
abstract class UpdateUser implements FieldInterface
16
{
17 1
    public static function build(): iterable
18
    {
19 1
        yield 'updateUser' => fn () => [
20 1
            'type' => Type::nonNull(_types()->getOutput(User::class)),
21 1
            'description' => 'Update an existing orderLine.',
22 1
            'args' => [
23 1
                'id' => Type::nonNull(_types()->getId(User::class)),
24 1
                'input' => Type::nonNull(_types()->getPartialInput(User::class)),
25 1
            ],
26 1
            'resolve' => function ($root, array $args, SessionInterface $session): User {
27
                global $container;
28
                /** @var Mailer $mailer */
29
                $mailer = $container->get(Mailer::class);
30
31
                /** @var MessageQueuer $messageQueuer */
32
                $messageQueuer = $container->get(MessageQueuer::class);
33
34
                $user = $args['id']->getEntity();
35
36
                // Check ACL
37
                Helper::throwIfDenied($user, 'update');
38
39
                $before = self::privateInformation($user);
40
                // Do it
41
                $input = $args['input'];
42
                Helper::hydrate($user, $input);
43
44
                _em()->flush();
45
46
                $after = self::privateInformation($user);
47
                if ($before !== $after) {
48
                    foreach ($messageQueuer->getAllEmailsToNotify() as $adminEmail) {
49
                        $message = $messageQueuer->queueUpdatedUser($adminEmail, $user, $before, $after);
50
                        $mailer->sendMessageAsync($message);
51
                    }
52
                }
53
54
                return $user;
55 1
            },
56 1
        ];
57
    }
58
59
    private static function privateInformation(User $user): array
60
    {
61
        return [
62
            'Prénom' => $user->getFirstName(),
63
            'Nom de famille' => $user->getLastName(),
64
            'Rue' => $user->getStreet(),
65
            'NPA' => $user->getPostcode(),
66
            'Localité' => $user->getLocality(),
67
            'Pays' => $user->getCountry() ? $user->getCountry()->getName() : null,
68
            'Téléphone' => $user->getPhone(),
69
        ];
70
    }
71
}
72