1 | <?php |
||||
2 | |||||
3 | namespace MauticPlugin\MauticCrmBundle\Api; |
||||
4 | |||||
5 | use Mautic\EmailBundle\Helper\MailHelper; |
||||
6 | use Mautic\PluginBundle\Exception\ApiErrorException; |
||||
7 | |||||
8 | class HubspotApi extends CrmApi |
||||
9 | { |
||||
10 | protected $requestSettings = [ |
||||
11 | 'encode_parameters' => 'json', |
||||
12 | ]; |
||||
13 | |||||
14 | protected function request($operation, $parameters = [], $method = 'GET', $object = 'contacts') |
||||
15 | { |
||||
16 | $hapikey = $this->integration->getHubSpotApiKey(); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
17 | $url = sprintf('%s/%s/%s/?hapikey=%s', $this->integration->getApiUrl(), $object, $operation, $hapikey); |
||||
18 | $request = $this->integration->makeRequest($url, $parameters, $method, $this->requestSettings); |
||||
19 | if (isset($request['status']) && 'error' == $request['status']) { |
||||
20 | $message = $request['message']; |
||||
21 | if (isset($request['validationResults'])) { |
||||
22 | $message .= " \n ".print_r($request['validationResults'], true); |
||||
0 ignored issues
–
show
Are you sure
print_r($request['validationResults'], true) of type string|true can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
23 | } |
||||
24 | if (isset($request['validationResults'][0]['error']) && 'PROPERTY_DOESNT_EXIST' == $request['validationResults'][0]['error']) { |
||||
25 | $this->createProperty($request['validationResults'][0]['name']); |
||||
26 | $this->request($operation, $parameters, $method, $object); |
||||
27 | } else { |
||||
28 | throw new ApiErrorException($message); |
||||
29 | } |
||||
30 | } |
||||
31 | |||||
32 | return $request; |
||||
33 | } |
||||
34 | |||||
35 | /** |
||||
36 | * @return mixed |
||||
37 | */ |
||||
38 | public function getLeadFields($object = 'contacts') |
||||
39 | { |
||||
40 | if ('company' == $object) { |
||||
41 | $object = 'companies'; //hubspot company object name |
||||
42 | } |
||||
43 | |||||
44 | return $this->request('v2/properties', [], 'GET', $object); |
||||
45 | } |
||||
46 | |||||
47 | /** |
||||
48 | * Creates Hubspot lead. |
||||
49 | * |
||||
50 | * @return mixed |
||||
51 | */ |
||||
52 | public function createLead(array $data, $lead, $updateLink = false) |
||||
53 | { |
||||
54 | /* |
||||
55 | * As Hubspot integration requires a valid email |
||||
56 | * If the email is not valid we don't proceed with the request |
||||
57 | */ |
||||
58 | $email = $data['email']; |
||||
59 | $result = []; |
||||
60 | //Check if the is a valid email |
||||
61 | MailHelper::validateEmail($email); |
||||
62 | //Format data for request |
||||
63 | $formattedLeadData = $this->integration->formatLeadDataForCreateOrUpdate($data, $lead, $updateLink); |
||||
64 | if ($formattedLeadData) { |
||||
65 | $result = $this->request('v1/contact/createOrUpdate/email/'.$email, $formattedLeadData, 'POST'); |
||||
66 | } |
||||
67 | |||||
68 | return $result; |
||||
69 | } |
||||
70 | |||||
71 | /** |
||||
72 | * gets Hubspot contact. |
||||
73 | * |
||||
74 | * @return mixed |
||||
75 | */ |
||||
76 | public function getContacts($params = []) |
||||
77 | { |
||||
78 | return $this->request('v1/lists/recently_updated/contacts/recent?', $params, 'GET', 'contacts'); |
||||
79 | } |
||||
80 | |||||
81 | /** |
||||
82 | * gets Hubspot company. |
||||
83 | * |
||||
84 | * @return mixed |
||||
85 | */ |
||||
86 | public function getCompanies($params, $id) |
||||
87 | { |
||||
88 | if ($id) { |
||||
89 | return $this->request('v2/companies/'.$id, $params, 'GET', 'companies'); |
||||
90 | } |
||||
91 | |||||
92 | return $this->request('v2/companies/recent/modified', $params, 'GET', 'companies'); |
||||
93 | } |
||||
94 | |||||
95 | /** |
||||
96 | * @param $propertyName |
||||
97 | * @param string $object |
||||
98 | * |
||||
99 | * @return mixed|string |
||||
100 | */ |
||||
101 | public function createProperty($propertyName, $object = 'properties') |
||||
102 | { |
||||
103 | return $this->request('v1/contacts/properties', ['name' => $propertyName, 'groupName' => 'contactinformation', 'type' => 'string'], 'POST', $object); |
||||
104 | } |
||||
105 | } |
||||
106 |