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

UpdateUser::privateInformation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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