1 | <?php |
||
2 | |||
3 | namespace MauticPlugin\MauticCrmBundle\Api; |
||
4 | |||
5 | use MauticPlugin\MauticCrmBundle\Integration\CrmAbstractIntegration; |
||
6 | use MauticPlugin\MauticCrmBundle\Services\TransportInterface; |
||
7 | use Psr\Http\Message\ResponseInterface; |
||
8 | |||
9 | class PipedriveApi extends CrmApi |
||
10 | { |
||
11 | const ORGANIZATIONS_API_ENDPOINT = 'organizations'; |
||
12 | const PERSONS_API_ENDPOINT = 'persons'; |
||
13 | const USERS_API_ENDPOINT = 'users'; |
||
14 | /** |
||
15 | * @var TransportInterface |
||
16 | */ |
||
17 | private $transport; |
||
18 | |||
19 | private $apiFields = []; |
||
20 | |||
21 | /** |
||
22 | * PipedriveApi constructor. |
||
23 | */ |
||
24 | public function __construct(CrmAbstractIntegration $integration, TransportInterface $transport) |
||
25 | { |
||
26 | $this->transport = $transport; |
||
27 | |||
28 | parent::__construct($integration); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @return array |
||
33 | */ |
||
34 | public function createCompany(array $data = []) |
||
35 | { |
||
36 | $params = $this->getRequestParameters($data); |
||
37 | $url = sprintf('%s/%s', $this->integration->getApiUrl(), self::ORGANIZATIONS_API_ENDPOINT); |
||
0 ignored issues
–
show
|
|||
38 | $response = $this->transport->post($url, $params); |
||
39 | |||
40 | return $this->getResponseData($response); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param null $id |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | public function updateCompany(array $data = [], $id = null) |
||
49 | { |
||
50 | $params = $this->getRequestParameters($data); |
||
51 | $url = sprintf('%s/%s/%s', $this->integration->getApiUrl(), self::ORGANIZATIONS_API_ENDPOINT, $id); |
||
52 | $response = $this->transport->put($url, $params); |
||
53 | |||
54 | return $this->getResponseData($response); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param null $id |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | public function removeCompany($id = null) |
||
63 | { |
||
64 | $params = $this->getRequestParameters(); |
||
65 | $url = sprintf('%s/%s/%s', $this->integration->getApiUrl(), self::ORGANIZATIONS_API_ENDPOINT, $id); |
||
66 | $response = $this->transport->delete($url, $params); |
||
67 | |||
68 | return $this->getResponseData($response); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param $data |
||
73 | */ |
||
74 | public function createLead(array $data = []) |
||
75 | { |
||
76 | $params = $this->getRequestParameters($data); |
||
77 | $url = sprintf('%s/%s', $this->integration->getApiUrl(), self::PERSONS_API_ENDPOINT); |
||
78 | $response = $this->transport->post($url, $params); |
||
79 | |||
80 | return $this->getResponseData($response); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param $data |
||
85 | */ |
||
86 | public function updateLead(array $data, $id) |
||
87 | { |
||
88 | $params = $this->getRequestParameters($data); |
||
89 | $url = sprintf('%s/%s/%s', $this->integration->getApiUrl(), self::PERSONS_API_ENDPOINT, $id); |
||
90 | $response = $this->transport->put($url, $params); |
||
91 | |||
92 | return $this->getResponseData($response); |
||
93 | } |
||
94 | |||
95 | public function deleteLead($id) |
||
96 | { |
||
97 | $params = $this->getRequestParameters(); |
||
98 | $url = sprintf('%s/%s/%s', $this->integration->getApiUrl(), self::PERSONS_API_ENDPOINT, $id); |
||
99 | $response = $this->transport->delete($url, $params); |
||
100 | |||
101 | return $this->getResponseData($response); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $email |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function findByEmail($email) |
||
110 | { |
||
111 | $url = sprintf('%s/%s/find', $this->integration->getApiUrl(), self::PERSONS_API_ENDPOINT); |
||
112 | |||
113 | $params = [ |
||
114 | 'query' => array_merge($this->getAuthQuery(), [ |
||
115 | 'term' => $email, |
||
116 | 'search_by_email' => true, |
||
117 | ]), |
||
118 | ]; |
||
119 | |||
120 | $response = $this->transport->get($url, $params); |
||
121 | |||
122 | return $this->getResponseData($response); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param $name |
||
127 | * @param int $start |
||
128 | * @param int $limit |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | public function findCompanyByName($name, $start = 0, $limit = 10) |
||
133 | { |
||
134 | $url = sprintf('%s/%s/find', $this->integration->getApiUrl(), self::ORGANIZATIONS_API_ENDPOINT); |
||
135 | |||
136 | $params = [ |
||
137 | 'query' => array_merge($this->getAuthQuery(), [ |
||
138 | 'term' => $name, |
||
139 | 'start' => $start, |
||
140 | 'limit' => $limit, |
||
141 | ]), |
||
142 | ]; |
||
143 | |||
144 | $response = $this->transport->get($url, $params); |
||
145 | |||
146 | return $this->getResponseData($response); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param string $endpoint |
||
151 | * |
||
152 | * @return mixed |
||
153 | */ |
||
154 | public function getDataByEndpoint(array $query, $endpoint) |
||
155 | { |
||
156 | $params = [ |
||
157 | 'query' => array_merge($this->getAuthQuery(), $query), |
||
158 | ]; |
||
159 | |||
160 | $url = sprintf('%s/%s', $this->integration->getApiUrl(), $endpoint); |
||
161 | |||
162 | $response = $this->transport->get($url, $params); |
||
163 | |||
164 | return json_decode($response->getBody(), true); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @return array |
||
169 | */ |
||
170 | public function getFields($object = null) |
||
171 | { |
||
172 | if (!empty($this->apiFields[$object])) { |
||
173 | return $this->apiFields[$object]; |
||
174 | } |
||
175 | |||
176 | $params = [ |
||
177 | 'query' => $this->getAuthQuery(), |
||
178 | ]; |
||
179 | |||
180 | $url = sprintf('%s/%sFields', $this->integration->getApiUrl(), $object); |
||
181 | |||
182 | $response = $this->transport->get($url, $params); |
||
183 | |||
184 | $this->apiFields[$object] = $response; |
||
185 | |||
186 | $data = $this->getResponseData($response); |
||
187 | |||
188 | return !empty($data) ? $data : []; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return array |
||
193 | */ |
||
194 | private function getResponseData(ResponseInterface $response) |
||
195 | { |
||
196 | $body = json_decode($response->getBody(), true); |
||
197 | |||
198 | return isset($body['data']) ? $body['data'] : []; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @return array |
||
203 | */ |
||
204 | private function getRequestParameters(array $data = []) |
||
205 | { |
||
206 | foreach ($data as $k => $d) { |
||
207 | $data[$k] = str_replace('|', ',', $d); |
||
208 | } |
||
209 | |||
210 | return [ |
||
211 | 'form_params' => $data, |
||
212 | 'query' => $this->getAuthQuery(), |
||
213 | ]; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return array |
||
218 | */ |
||
219 | private function getAuthQuery() |
||
220 | { |
||
221 | $tokenData = $this->integration->getKeys(); |
||
222 | |||
223 | return [ |
||
224 | 'api_token' => $tokenData[$this->integration->getAuthTokenKey()], |
||
225 | ]; |
||
226 | } |
||
227 | } |
||
228 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.