Passed
Push — master ( 528c5a...0afffa )
by Sylvain
07:58
created

RequestMembershipEnd::build()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.128

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
nc 1
nop 0
dl 0
loc 30
ccs 12
cts 15
cp 0.8
crap 4.128
rs 9.7
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\DBAL\Types\MembershipType;
8
use Application\Model\User;
9
use Application\Repository\UserRepository;
10
use Application\Service\MessageQueuer;
11
use Ecodev\Felix\Api\Exception;
12
use Ecodev\Felix\Api\Field\FieldInterface;
13
use Ecodev\Felix\Service\Mailer;
14
use GraphQL\Type\Definition\Type;
15
use Mezzio\Session\SessionInterface;
16
17
abstract class RequestMembershipEnd implements FieldInterface
18
{
19 1
    public static function build(): array
20
    {
21
        return [
22 1
            'name' => 'requestMembershipEnd',
23 1
            'type' => Type::nonNull(Type::boolean()),
24 1
            'description' => 'Stop the use membership.',
25
            'args' => [
26
            ],
27 1
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
28 1
                $user = User::getCurrent();
29
30 1
                if (!$user || $user->getMembership() === MembershipType::NONE) {
31
                    throw new Exception('Must be logged in and have a membership');
32
                }
33
34
                global $container;
35
                /** @var Mailer $mailer */
36 1
                $mailer = $container->get(Mailer::class);
37
38
                /** @var MessageQueuer $messageQueuer */
39 1
                $messageQueuer = $container->get(MessageQueuer::class);
40
41
                /** @var UserRepository $repository */
42 1
                $repository = _em()->getRepository(User::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $repository is dead and can be removed.
Loading history...
43 1
                foreach ($messageQueuer->getAllEmailsToNotify() as $adminEmail) {
44
                    $message = $messageQueuer->queueRequestMembershipEnd($adminEmail, $user);
45
                    $mailer->sendMessageAsync($message);
46
                }
47
48 1
                return true;
49 1
            },
50
        ];
51
    }
52
}
53