Passed
Push — master ( c86f31...6475fd )
by Sam
10:34
created

AddToMailingList::build()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 24
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 37
rs 9.536
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
    public static function build(): array
15
    {
16
        return [
17
            'name' => 'addToMailingList',
18
            'type' => Type::nonNull(Type::boolean()),
19
            'description' => 'Subscribe e-mail to infomaniak mailing list',
20
            'args' => [
21
                'destination' => Type::nonNull(_types()->get('string')),
22
                'email' => Type::nonNull(_types()->get('string')),
23
            ],
24
            '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(Client::MAILINGLIST,
40
                    [
41
                        'id' => $destination,
42
                        'action' => Action::IMPORTCONTACT,
43
                        'params' => [
44
                            'contacts' => [
45
                                ['email' => $email],
46
                            ],
47
                        ],
48
                    ]);
49
50
                return $response->success();
51
            },
52
        ];
53
    }
54
}
55