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

SubscribeNewsletter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 2
eloc 16
dl 0
loc 26
ccs 14
cts 16
cp 0.875
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 24 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Service\MessageQueuer;
8
use Ecodev\Felix\Api\Field\FieldInterface;
9
use Ecodev\Felix\Api\Scalar\EmailType;
10
use Ecodev\Felix\Service\Mailer;
11
use GraphQL\Type\Definition\Type;
12
use Mezzio\Session\SessionInterface;
13
14
abstract class SubscribeNewsletter implements FieldInterface
15
{
16 1
    public static function build(): array
17
    {
18 1
        return [
19 1
            'name' => 'subscribeNewsletter',
20 1
            'type' => Type::nonNull(Type::boolean()),
21 1
            'description' => 'Subscribe to newsletter.',
22 1
            'args' => [
23 1
                'email' => Type::nonNull(_types()->get(EmailType::class)),
24 1
            ],
25 1
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
26 1
                $email = $args['email'];
27
                global $container;
28
                /** @var Mailer $mailer */
29 1
                $mailer = $container->get(Mailer::class);
30
31
                /** @var MessageQueuer $messageQueuer */
32 1
                $messageQueuer = $container->get(MessageQueuer::class);
33
34 1
                foreach ($messageQueuer->getAllEmailsToNotify() as $adminEmail) {
35
                    $message = $messageQueuer->queueNewsletterSubscription($adminEmail, $email);
36
                    $mailer->sendMessageAsync($message);
37
                }
38
39 1
                return true;
40 1
            },
41 1
        ];
42
    }
43
}
44