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

AddToMailingList::build()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.667

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 39
ccs 6
cts 18
cp 0.3333
rs 9.52
c 1
b 0
f 0
cc 3
nc 1
nop 0
crap 5.667
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