Passed
Push — master ( d59596...9d2c64 )
by Adrien
27:21 queued 13:39
created

UpdateUser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 32
dl 0
loc 55
ccs 33
cts 35
cp 0.9429
rs 10
c 1
b 0
f 0

2 Methods

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