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

AddToMailingList   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 26
dl 0
loc 41
ccs 6
cts 18
cp 0.3333
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 39 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Ecodev\Felix\Api\Field\FieldInterface;
8
use GraphQL\Type\Definition\Type;
9
use Infomaniak\ClientApiNewsletter\Action;
10
use Infomaniak\ClientApiNewsletter\Client;
11
12
abstract class AddToMailingList implements FieldInterface
13
{
14 1
    public static function build(): array
15
    {
16
        return [
17 1
            'name' => 'addToMailingList',
18 1
            'type' => Type::nonNull(Type::boolean()),
19
            'description' => 'Subscribe e-mail to infomaniak mailing list',
20
            'args' => [
21 1
                'destination' => Type::nonNull(_types()->get('string')),
22 1
                'email' => Type::nonNull(_types()->get('string')),
23
            ],
24 1
            'resolve' => function ($root, array $args): bool {
25
                global $container;
26
                $config = $container->get('config');
27
                $key = $config['infomaniak-api'] ?? null;
28
                $secret = $config['infomaniak-secret'] ?? null;
29
30
                if (!$key || !$secret) {
31
                    return false;
32
                }
33
34
                $destination = $args['destination'];
35
                $email = $args['email'];
36
37
                $client = new Client($key, $secret);
38
39
                $response = $client->post(
40
                    Client::MAILINGLIST,
41
                    [
42
                        'id' => $destination,
43
                        'action' => Action::IMPORTCONTACT,
44
                        'params' => [
45
                            'contacts' => [
46
                                ['email' => $email],
47
                            ],
48
                        ],
49
                    ]
50
                );
51
52
                return $response->success();
53
            },
54
        ];
55
    }
56
}
57