Passed
Push — master ( 1d5fb4...e5c3c4 )
by Sam
06:33 queued 01:55
created

UpdateUser::build()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3.009

Importance

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