LeaveFamily::build()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 53
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 53
ccs 30
cts 30
cp 1
rs 9.456
c 0
b 0
f 0
cc 3
nc 1
nop 0
crap 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Api\Helper;
8
use Application\Enum\Relationship;
9
use Application\Enum\UserStatus;
10
use Application\Model\Account;
11
use Application\Model\User;
12
use Application\Repository\AccountRepository;
13
use Application\Service\MessageQueuer;
14
use Cake\Chronos\ChronosDate;
15
use Ecodev\Felix\Api\Field\FieldInterface;
16
use Ecodev\Felix\Service\Mailer;
17
use GraphQL\Type\Definition\Type;
18
use Mezzio\Session\SessionInterface;
19
20
abstract class LeaveFamily implements FieldInterface
21
{
22 3
    public static function build(): iterable
23
    {
24 1
        yield 'leaveFamily' => fn () => [
25 1
            'type' => Type::nonNull(_types()->getOutput(User::class)),
26 1
            'description' => 'Make the given user independent from his family and inactive.',
27 1
            'args' => [
28 1
                'id' => Type::nonNull(_types()->getId(User::class)),
29 1
            ],
30 1
            'resolve' => function ($root, array $args, SessionInterface $session): User {
31
                global $container;
32
                /** @var Mailer $mailer */
33 3
                $mailer = $container->get(Mailer::class);
34
35
                /** @var MessageQueuer $messageQueuer */
36 3
                $messageQueuer = $container->get(MessageQueuer::class);
37
38
                /** @var User $user */
39 3
                $user = $args['id']->getEntity();
40
41
                // Check ACL
42 3
                Helper::throwIfDenied($user, 'update');
43
44
                // Set owner while pretending we are an admin to workaround normal security things
45 2
                $previousCurrentUser = User::getCurrent();
46 2
                User::setCurrent(new User(User::ROLE_ADMINISTRATOR));
47 2
                $user->setOwner(null);
48 2
                User::setCurrent($previousCurrentUser);
49
50 2
                $user->setFamilyRelationship(Relationship::Householder);
51 2
                $user->setStatus(UserStatus::Inactive);
52
53
                // Append a line to internal remarks
54 2
                $internalRemarks = implode(PHP_EOL . PHP_EOL, array_filter([$user->getInternalRemarks(), ChronosDate::now()->toDateString() . ': détaché du ménage par ' . User::getCurrent()->getName()]));
55 2
                $user->setInternalRemarks($internalRemarks);
56
57
                // Create account so the user can top-up money and start purchasing services
58
                /** @var AccountRepository $accountRepository */
59 2
                $accountRepository = _em()->getRepository(Account::class);
60 2
                $accountRepository->getOrCreate($user);
61
62 2
                _em()->flush();
63
64 2
                $message = $messageQueuer->queueLeaveFamily($user);
65 2
                if ($message) {
66 2
                    $mailer->sendMessageAsync($message);
67
                }
68
69 2
                $message = $messageQueuer->queueAdminLeaveFamily($user);
70 2
                if ($message) {
71 2
                    $mailer->sendMessageAsync($message);
72
                }
73
74 2
                return $user;
75 1
            },
76 1
        ];
77
    }
78
}
79