Completed
Push — master ( 6835a6...4d776f )
by Nate
12:26
created

Contacts::batch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 16
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
crap 2
1
<?php
2
3
namespace flipbox\hubspot\modules\http\services;
4
5
use craft\helpers\Json;
6
use flipbox\hubspot\authentication\AuthenticationStrategyInterface;
7
use flipbox\hubspot\cache\CacheStrategyInterface;
8
use flipbox\hubspot\HubSpot;
9
use Flipbox\Relay\HubSpot\Segment\Contacts\Batch;
10
use Flipbox\Relay\HubSpot\Segment\Contacts\Create;
11
use Flipbox\Relay\HubSpot\Segment\Contacts\GetByEmail;
12
use Flipbox\Relay\HubSpot\Segment\Contacts\GetById;
13
use Flipbox\Relay\HubSpot\Segment\Contacts\UpdateByEmail;
14
use Flipbox\Relay\HubSpot\Segment\Contacts\UpdateById;
15
use Psr\Http\Message\ResponseInterface;
16
17
class Contacts extends AbstractResource
18
{
19
    /**
20
     * @param array                                $payload
21
     * @param AuthenticationStrategyInterface|null $authenticationStrategy
22
     * @return ResponseInterface
23
     */
24
    public function create(
25
        array $payload,
26
        AuthenticationStrategyInterface $authenticationStrategy = null
27
    ) {
28
        // Create runner segments
29
        $segments = new Create(
30
            [
31
            'payload' => $payload,
32
            'logger' => $this->getLogger()
33
            ]
34
        );
35
36
        // Prepend authorization
37
        $this->prependAuthenticationMiddleware(
38
            $segments,
39
            $authenticationStrategy
40
        );
41
42
        return $segments->run();
43
    }
44
45
    /**
46
     * @param string                               $email
47
     * @param array                                $payload
48
     * @param AuthenticationStrategyInterface|null $authenticationStrategy
49
     * @return ResponseInterface
50
     */
51
    public function updateByEmail(
52
        string $email,
53
        array $payload,
54
        AuthenticationStrategyInterface $authenticationStrategy = null
55
    ) {
56
        // Create runner segments
57
        $segments = new UpdateByEmail(
58
            [
59
            'email' => $email,
60
            'payload' => $payload,
61
            'logger' => $this->getLogger()
62
            ]
63
        );
64
65
        // Prepend authorization
66
        $this->prependAuthenticationMiddleware(
67
            $segments,
68
            $authenticationStrategy
69
        );
70
71
        return $segments->run();
72
    }
73
74
    /**
75
     * @param int                                  $id
76
     * @param array                                $payload
77
     * @param AuthenticationStrategyInterface|null $authenticationStrategy
78
     * @return ResponseInterface
79
     */
80
    public function updateById(
81
        int $id,
82
        array $payload,
83
        AuthenticationStrategyInterface $authenticationStrategy = null
84
    ) {
85
        // Create runner segments
86
        $segments = new UpdateById(
87
            [
88
            'id' => $id,
89
            'payload' => $payload,
90
            'logger' => $this->getLogger()
91
            ]
92
        );
93
94
        // Prepend authorization
95
        $this->prependAuthenticationMiddleware(
96
            $segments,
97
            $authenticationStrategy
98
        );
99
100
        return $segments->run();
101
    }
102
103
    /**
104
     * @param array                                $payload
105
     * @param AuthenticationStrategyInterface|null $authenticationStrategy
106
     * @return ResponseInterface
107
     */
108
    public function batch(
109
        array $payload,
110
        AuthenticationStrategyInterface $authenticationStrategy = null
111
    ) {
112
        // Create runner segments
113
        $segments = new Batch(
114
            [
115
                'payload' => $payload,
116
                'logger' => $this->getLogger()
117
            ]
118
        );
119
120
        // Prepend authorization
121
        $this->prependAuthenticationMiddleware(
122
            $segments,
123
            $authenticationStrategy
124
        );
125
126
        return $segments->run();
127
    }
128
129
    /**
130
     * @param int                                  $id
131
     * @param AuthenticationStrategyInterface|null $authenticationStrategy
132
     * @param CacheStrategyInterface|null          $cacheStrategy
133
     * @return ResponseInterface
134
     */
135
    public function getById(
136
        int $id,
137
        AuthenticationStrategyInterface $authenticationStrategy = null,
138
        CacheStrategyInterface $cacheStrategy = null
139
    ) {
140
        // Create runner segments
141
        $segments = new GetById(
142
            [
143
            'id' => $id,
144
            'logger' => $this->getLogger(),
145
            'cache' => $this->resolveCacheStrategy($cacheStrategy)->getPool()
146
            ]
147
        );
148
149
        // Prepend authorization
150
        $this->prependAuthenticationMiddleware(
151
            $segments,
152
            $authenticationStrategy
153
        );
154
155
        return $segments->run();
156
    }
157
158
    /**
159
     * @param string                               $email
160
     * @param AuthenticationStrategyInterface|null $authenticationStrategy
161
     * @param CacheStrategyInterface|null          $cacheStrategy
162
     * @return ResponseInterface
163
     */
164
    public function getByEmail(
165
        string $email,
166
        AuthenticationStrategyInterface $authenticationStrategy = null,
167
        CacheStrategyInterface $cacheStrategy = null
168
    ) {
169
        // Create runner segments
170
        $segments = new GetByEmail(
171
            [
172
            'email' => $email,
173
            'logger' => $this->getLogger(),
174
            'cache' => $this->resolveCacheStrategy($cacheStrategy)->getPool()
175
            ]
176
        );
177
178
        // Prepend authorization
179
        $this->prependAuthenticationMiddleware(
180
            $segments,
181
            $authenticationStrategy
182
        );
183
184
        return $segments->run();
185
    }
186
}
187