Contacts::asyncCreate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * A php library for using the Emarsys API.
5
 *
6
 * @link      https://github.com/quitoque/emarsys-php-client
7
 * @package   emarsys-php-client
8
 * @license   MIT
9
 * @copyright Copyright (c) 2017 Quitoque <[email protected]>
10
 */
11
12
namespace Emarsys\Api;
13
14
use Http\Promise\Promise;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * Class Contacts.
19
 *
20
 * @author Claude Khedhiri <[email protected]>
21
 */
22
class Contacts extends AbstractApi
23
{
24
    /**
25
     * @param array $contact
26
     *
27
     * @return ResponseInterface
28
     */
29
    public function create(array $contact)
30
    {
31
        return $this->sendRequest($this->createCreateRequest($contact));
32
    }
33
34
    /**
35
     * @param array $contact
36
     *
37
     * @return Promise
38
     */
39
    public function asyncCreate(array $contact)
40
    {
41
        return $this->sendAsyncRequest($this->createCreateRequest($contact));
42
    }
43
44
    /**
45
     * @param array $contact
46
     *
47
     * @return ResponseInterface
48
     */
49
    public function update(array $contact)
50
    {
51
        return $this->sendRequest($this->createUpdateRequest($contact));
52
    }
53
54
    /**
55
     * @param array $contact
56
     *
57
     * @return Promise
58
     */
59
    public function asyncUpdate(array $contact)
60
    {
61
        return $this->sendAsyncRequest($this->createUpdateRequest($contact));
62
    }
63
64
    /**
65
     * @param array $contact
66
     *
67
     * @return ResponseInterface
68
     */
69
    public function updateOrCreate(array $contact)
70
    {
71
        return $this->sendRequest($this->createUpdateRequest($contact, 1));
72
    }
73
74
    /**
75
     * @param array $contact
76
     *
77
     * @return Promise
78
     */
79
    public function asyncUpdateOrCreate(array $contact)
80
    {
81
        return $this->sendAsyncRequest($this->createUpdateRequest($contact, 1));
82
    }
83
84
    /**
85
     * @param array $contacts
86
     *
87
     * @return \Psr\Http\Message\RequestInterface
88
     */
89
    private function createCreateRequest(array $contacts)
90
    {
91
        return $this->createRequest('POST', '/contact', $contacts);
92
    }
93
94
    /**
95
     * @param array $contacts
96
     * @param int   $createIfNotExists
97
     *
98
     * @return \Psr\Http\Message\RequestInterface
99
     */
100
    private function createUpdateRequest(array $contacts, $createIfNotExists = 0)
101
    {
102
        return $this->createRequest('PUT', "/contact?create_if_not_exists=$createIfNotExists", $contacts);
103
    }
104
}
105