Passed
Push — master ( f3b94d...fdd73f )
by nicolas
01:01
created

ContactCreator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 63
rs 10
ccs 30
cts 30
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addToList() 0 12 1
B create() 0 28 4
1
<?php
2
3
namespace Dekalee\MailjetBundle\Creator;
4
5
use Doctrine\Common\Inflector\Inflector;
6
use Mailjet\Client;
7
use Mailjet\Resources;
8
9
/**
10
 * Class ContactCreator
11
 */
12
class ContactCreator
13
{
14
    protected $client;
15
16
    /**
17
     * @param Client $client
18
     */
19 3
    public function __construct(Client $client)
20
    {
21 3
        $this->client = $client;
22 3
    }
23
24
    /**
25
     * @param string $listName
26
     * @param string $contact
27
     * @param array  $parameters
28
     */
29 1
    public function create($listName, $contact, array $parameters = [])
30
    {
31
        $body = [
32
            'Email' => $contact
33 1
        ];
34 1
        $this->client->post(Resources::$Contact, ['body' => $body]);
35
36 1
        $name = Inflector::classify($listName);
37 1
        $data = [];
38 1
        foreach (['content', 'subject'] as $item) {
39 1
            if (array_key_exists($item, $parameters)) {
40
                $body = [
41 1
                    'Datatype' => "str",
42 1
                    'Name' => $item . "_" . $name,
43
                    'NameSpace' => "static"
44 1
                ];
45 1
                $this->client->post(Resources::$Contactmetadata, ['body' => $body]);
46 1
                $data['Data'][] = [
47 1
                    'Name' => $item . "_" . $name,
48 1
                    'value' => substr($parameters[$item], 0, 1000),
49
                ];
50 1
            }
51 1
        }
52
53 1
        if (!empty($data)) {
54 1
            $this->client->put(Resources::$Contactdata, ['id' => $contact, 'body' => $data]);
55 1
        }
56 1
    }
57
58
    /**
59
     * @param int    $contactListId
60
     * @param string $contact
61
     */
62 1
    public function addToList($contactListId, $contact)
63
    {
64
        $body = [
65
            'ContactsLists' => [
66
                [
67 1
                    'ListID' => $contactListId,
68
                    'Action' => "addforce"
69 1
                ]
70 1
            ]
71 1
        ];
72 1
        $this->client->post(Resources::$ContactManagecontactslists, ['id' => $contact, 'body' => $body]);
73 1
    }
74
}
75