Passed
Pull Request — master (#2)
by nicolas
07:19
created

ContactListCreator::addContactToList()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 46
rs 8.9411
cc 1
eloc 28
nc 1
nop 3
1
<?php
2
3
namespace Dekalee\MailjetBundle\Creator;
4
5
use Dekalee\MailjetBundle\Convertor\ContactListConvertor;
6
use Doctrine\Common\Inflector\Inflector;
7
use Mailjet\Client;
8
use Mailjet\Resources;
9
10
/**
11
 * Class ContactListCreator
12
 */
13
class ContactListCreator
14
{
15
    protected $client;
16
    protected $contactListConvertor;
17
18
    /**
19
     * @param ContactListConvertor $contactListConvertor
20
     * @param Client               $client
21
     */
22
    public function __construct(ContactListConvertor $contactListConvertor, Client $client)
23
    {
24
        $this->client = $client;
25
        $this->contactListConvertor = $contactListConvertor;
26
    }
27
28
    /**
29
     * @param string $name
30
     * @param string $contact
31
     * @param array  $parameters
32
     *
33
     * @throws \Dekalee\MailjetBundle\Exception\ContactListNotCreated
34
     */
35
    public function addContactToList($name, $contact, array $parameters = [])
36
    {
37
        $contactListId = $this->contactListConvertor->convert($name);
38
39
        $body = [
40
            'Email' => $contact
41
        ];
42
43
        $response = $this->client->post(Resources::$Contact, ['body' => $body]);
44
        var_dump($response->success());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($response->success()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
45
        print_r($response->getData());
46
47
48
        $body = [
49
            'Datatype' => "str",
50
            'Name' => "content_" . $name,
51
            'NameSpace' => "static"
52
        ];
53
        $response = $this->client->post(Resources::$Contactmetadata, ['body' => $body]);
54
        var_dump($response->success());
55
        print_r($response->getData());
56
57
        $body = [
58
            'Data' => [
59
                [
60
                    'Name' => "content_" . $name,
61
                    'value' => $parameters['content']
62
                ],
63
            ]
64
        ];
65
        $response = $this->client->put(Resources::$Contactdata, ['id' => $contact, 'body' => $body]);
66
        var_dump($response->success());
67
        print_r($response->getData());
68
69
        $body = [
70
            'ContactsLists' => [
71
                [
72
                    'ListID' => $contactListId,
73
                    'Action' => "addforce"
74
                ]
75
            ]
76
        ];
77
        $response = $this->client->post(Resources::$ContactManagecontactslists, ['id' => $contact, 'body' => $body]);
78
        var_dump($response->success());
79
        print_r($response->getData());
80
    }
81
}
82