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

Companies::updateByDomain()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 26
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 20
nc 2
nop 4
crap 6
1
<?php
2
3
namespace flipbox\hubspot\modules\resources\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\Transform\Factory;
10
use Flipbox\Transform\Transformers\TransformerInterface;
11
12
class Companies extends AbstractResource
13
{
14
15
    /**
16
     * @param $data
17
     * @param callable|TransformerInterface        $transformer
18
     * @param AuthenticationStrategyInterface|null $authenticationStrategy
19
     * @return array|bool
20
     */
21
    public function create(
22
        $data,
23
        callable $transformer,
24
        AuthenticationStrategyInterface $authenticationStrategy = null
25
    ) {
26
        $payload = Factory::item($transformer, $data);
27
28
        $response = HubSpot::getInstance()->http()->companies()->create(
29
            $payload,
30
            $authenticationStrategy
31
        );
32
33
        // Interpret response
34
        if ($response->getStatusCode() !== 200) {
35
            $body = Json::decodeIfJson($response->getBody()->getContents());
36
            HubSpot::warning(
37
                sprintf(
38
                    "Unable to create company: %s, errors: %s",
39
                    Json::encode($payload),
40
                    Json::encode($body)
41
                )
42
            );
43
            return [
44
                false,
45
                $body
46
            ];
47
        }
48
49
        return [
50
            true,
51
            Json::decodeIfJson($response->getBody()->getContents())
52
        ];
53
    }
54
55
    /**
56
     * @param int                                  $id
57
     * @param $data
58
     * @param callable|TransformerInterface        $transformer
59
     * @param AuthenticationStrategyInterface|null $authenticationStrategy
60
     * @return bool|array
61
     */
62
    public function updateById(
63
        int $id,
64
        $data,
65
        callable $transformer,
66
        AuthenticationStrategyInterface $authenticationStrategy = null
67
    ) {
68
        $payload = Factory::item($transformer, $data);
69
70
        $response = HubSpot::getInstance()->http()->companies()->updateById(
71
            $id,
72
            $payload,
73
            $authenticationStrategy
74
        );
75
76
        if ($response->getStatusCode() !== 200) {
77
            $body = Json::decodeIfJson($response->getBody()->getContents());
78
            HubSpot::warning(
79
                sprintf(
80
                    "Unable to update company with id %s: %s, errors: %s",
81
                    $id,
82
                    Json::encode($payload),
83
                    Json::encode($body)
84
                )
85
            );
86
            return null;
87
        }
88
89
        return Json::decodeIfJson($response->getBody()->getContents());
90
    }
91
92
    /**
93
     * @param string                               $domain
94
     * @param $data
95
     * @param callable|TransformerInterface        $transformer
96
     * @param AuthenticationStrategyInterface|null $authenticationStrategy
97
     * @return bool|array
98
     */
99
    public function updateByDomain(
100
        string $domain,
101
        $data,
102
        callable $transformer,
103
        AuthenticationStrategyInterface $authenticationStrategy = null
104
    ) {
105
        $payload = Factory::item($transformer, $data);
106
107
        $response = HubSpot::getInstance()->http()->companies()->updateByDomain(
108
            $domain,
109
            Factory::item($transformer, $data),
110
            $authenticationStrategy
111
        );
112
113
        if ($response->getStatusCode() !== 200) {
114
            $body = Json::decodeIfJson($response->getBody()->getContents());
115
            HubSpot::warning(
116
                sprintf(
117
                    "Unable to update company with domain %s: %s, errors: %s",
118
                    $domain,
119
                    Json::encode($payload),
120
                    Json::encode($body)
121
                )
122
            );
123
            return null;
124
        }
125
126
        return Json::decodeIfJson($response->getBody()->getContents());
127
    }
128
129
    /**
130
     * @param int                                  $id
131
     * @param callable|TransformerInterface $transformer
132
     * @param AuthenticationStrategyInterface|null $authenticationStrategy
133
     * @param CacheStrategyInterface|null          $cacheStrategy
134
     * @return array|null
135
     */
136
    public function getById(
137
        int $id,
138
        callable $transformer,
139
        AuthenticationStrategyInterface $authenticationStrategy = null,
140
        CacheStrategyInterface $cacheStrategy = null
141
    ) {
142
        // Get contact
143
        $response = HubSpot::getInstance()->http()->companies()->getById(
144
            $id,
145
            $authenticationStrategy,
146
            $cacheStrategy
147
        );
148
149
        if ($response->getStatusCode() !== 200) {
150
            $body = Json::decodeIfJson($response->getBody()->getContents());
151
            HubSpot::warning(
152
                sprintf(
153
                    "Unable to get company with id %s, errors: %s",
154
                    $id,
155
                    Json::encode($body)
156
                )
157
            );
158
            return null;
159
        }
160
161
        return Factory::item(
162
            $transformer,
163
            Json::decodeIfJson($response->getBody()->getContents())
164
        );
165
    }
166
167
    /**
168
     * @param string                               $domain
169
     * @param callable|TransformerInterface $transformer
170
     * @param AuthenticationStrategyInterface|null $authenticationStrategy
171
     * @param CacheStrategyInterface|null          $cacheStrategy
172
     * @return array|null
173
     */
174
    public function getByDomain(
175
        string $domain,
176
        callable $transformer,
177
        AuthenticationStrategyInterface $authenticationStrategy = null,
178
        CacheStrategyInterface $cacheStrategy = null
179
    ) {
180
        // Get response
181
        $response = HubSpot::getInstance()->http()->companies()->getByDomain(
182
            $domain,
183
            $authenticationStrategy,
184
            $cacheStrategy
185
        );
186
187
        if ($response->getStatusCode() !== 200) {
188
            $body = Json::decodeIfJson($response->getBody()->getContents());
189
            HubSpot::warning(
190
                sprintf(
191
                    "Unable to get company with domain %s, errors: %s",
192
                    $domain,
193
                    Json::encode($body)
194
                )
195
            );
196
            return null;
197
        }
198
199
        return Factory::item(
200
            $transformer,
201
            Json::decodeIfJson($response->getBody()->getContents())
202
        );
203
    }
204
}
205