Issues (3627)

plugins/MauticCrmBundle/Api/ZohoApi.php (2 issues)

1
<?php
2
3
namespace MauticPlugin\MauticCrmBundle\Api;
4
5
use Mautic\PluginBundle\Exception\ApiErrorException;
6
7
class ZohoApi extends CrmApi
8
{
9
    /**
10
     * @param string $operation
11
     * @param string $method
12
     * @param bool   $json
13
     * @param array  $settings
14
     *
15
     * @return array
16
     *
17
     * @throws ApiErrorException
18
     */
19
    protected function request($operation, array $parameters = [], $method = 'GET', $json = false, $settings = [])
20
    {
21
        $tokenData = $this->integration->getKeys();
22
23
        $url = sprintf('%s/%s', $tokenData['api_domain'].'/crm/v2', $operation);
24
25
        if (!isset($settings['headers'])) {
26
            $settings['headers'] = [];
27
        }
28
        $settings['headers']['Authorization'] = 'Zoho-oauthtoken '.$tokenData['access_token'];
29
30
        if ($json) {
31
            $settings['Content-Type']      = 'application/json';
32
            $settings['encode_parameters'] = 'json';
33
        }
34
35
        $response = $this->integration->makeRequest($url, $parameters, $method, $settings);
36
37
        if (isset($response['status']) && 'error' === $response['status']) {
38
            throw new ApiErrorException($response['message']);
39
        }
40
41
        return $response;
42
    }
43
44
    /**
45
     * @param string $object
46
     *
47
     * @return array
48
     *
49
     * @throws ApiErrorException
50
     */
51
    public function getLeadFields($object = 'Leads')
52
    {
53
        if ('company' == $object) {
54
            $object = 'Accounts'; // Zoho object name
55
        }
56
57
        return $this->request('settings/fields?module='.$object, [], 'GET');
58
    }
59
60
    /**
61
     * @param string $object
62
     *
63
     * @return array
64
     *
65
     * @throws ApiErrorException
66
     */
67
    public function createLead(array $data, $object = 'Leads')
68
    {
69
        $parameters['data'] = $data;
70
71
        return $this->request($object, $parameters, 'POST', true);
72
    }
73
74
    /**
75
     * @param string $object
76
     *
77
     * @return array
78
     *
79
     * @throws ApiErrorException
80
     */
81
    public function updateLead(array $data, $object = 'Leads')
82
    {
83
        $parameters['data'] = $data;
84
85
        return $this->request($object, $parameters, 'PUT', true);
86
    }
87
88
    /**
89
     * @param string $object
90
     * @param null   $id
91
     *
92
     * @return array
93
     *
94
     * @throws ApiErrorException
95
     */
96
    public function getLeads(array $params, $object, $id = null)
97
    {
98
        if (!isset($params['selectColumns'])) {
99
            $params['selectColumns'] = 'All';
100
            $params['newFormat']     = 1;
101
        }
102
103
        $settings = [];
104
        if ($params['lastModifiedTime']) {
105
            $settings['headers'] = [
106
                'If-Modified-Since' => $params['lastModifiedTime'],
107
            ];
108
        }
109
110
        if ($id) {
0 ignored issues
show
$id is of type null, thus it always evaluated to false.
Loading history...
111
            if (is_array($id)) {
112
                $params['id'] = implode(';', $id);
113
            } else {
114
                $params['id'] = $id;
115
            }
116
117
            $data = $this->request($object, $params, 'GET', false, $settings);
118
        } else {
119
            $data = $this->request($object, $params, 'GET', false, $settings);
120
        }
121
122
        return $data;
123
    }
124
125
    /**
126
     * @param null $id
127
     *
128
     * @return array
129
     *
130
     * @throws ApiErrorException
131
     */
132
    public function getCompanies(array $params, $id = null)
133
    {
134
        if (!isset($params['selectColumns'])) {
135
            $params['selectColumns'] = 'All';
136
        }
137
138
        $settings = [];
139
        if ($params['lastModifiedTime']) {
140
            $settings['headers'] = [
141
                'If-Modified-Since' => $params['lastModifiedTime'],
142
            ];
143
        }
144
145
        if ($id) {
0 ignored issues
show
$id is of type null, thus it always evaluated to false.
Loading history...
146
            $params['id'] = $id;
147
148
            $data = $this->request('Accounts', $params, 'GET', false, $settings);
149
        } else {
150
            $data = $this->request('Accounts', $params, 'GET', false, $settings);
151
        }
152
153
        return $data;
154
    }
155
156
    /**
157
     * @param string $searchColumn
158
     * @param string $searchValue
159
     * @param string $object
160
     *
161
     * @return mixed|string
162
     *
163
     * @throws ApiErrorException
164
     */
165
    public function getSearchRecords($searchColumn, $searchValue, $object = 'Leads')
166
    {
167
        $parameters = [
168
            'criteria' => '('.$searchColumn.':equals:'.$searchValue.')',
169
        ];
170
171
        return $this->request($object.'/search', $parameters, 'GET', false);
172
    }
173
}
174