BatchApiSF::results()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 20
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 28
rs 9.6
1
<?php
2
3
namespace SalesforceBulkApi\api;
4
5
use SalesforceBulkApi\dto\BatchInfoDto;
6
use SalesforceBulkApi\dto\JobInfoDto;
7
use SalesforceBulkApi\dto\ResultAtBatchDto;
8
use SalesforceBulkApi\exceptions\ApiRequestException;
9
use SalesforceBulkApi\exceptions\ApiResponseException;
10
use SalesforceBulkApi\exceptions\HttpClientException;
11
use SalesforceBulkApi\exceptions\SFClientException;
12
use SalesforceBulkApi\helpers\ApiHelper;
13
use SalesforceBulkApi\services\ApiSalesforce;
14
use GuzzleHttp\Psr7\Request;
15
16
class BatchApiSF
17
{
18
    /**
19
     * @var string
20
     */
21
    public static $endpoint = 'https://%s.salesforce.com/services/async/%s/job/%s/batch';
22
23
    /**
24
     * @param ApiSalesforce $api
25
     * @param JobInfoDto    $job
26
     * @param string (json) $data
0 ignored issues
show
Bug introduced by
The type SalesforceBulkApi\api\json was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
     *
28
     * @return BatchInfoDto
29
     * @throws \Exception
30
     */
31
    public static function addToJob(ApiSalesforce $api, JobInfoDto $job, $data)
32
    {
33
        $version  = $api->getLoginParams()->getApiVersion();
34
        $instance = $api->getSession()->getInstance();
35
        $request  = new Request(
36
            'POST',
37
            sprintf(self::$endpoint, $instance, $version, $job->getId()),
38
            [
39
                'Content-Type'   => 'application/json; charset=UTF8',
40
                'X-SFDC-Session' => $api->getSession()->getSessionId()
41
            ],
42
            $data
43
        );
44
        $response = ApiHelper::getResponse($request, $api);
45
46
        return new BatchInfoDto($response->getBody()->getContents());
47
    }
48
49
    /**
50
     * @param ApiSalesforce $api
51
     * @param BatchInfoDto  $batch
52
     *
53
     * @return BatchInfoDto
54
     * @throws \Exception
55
     */
56
    public static function info(ApiSalesforce $api, BatchInfoDto $batch)
57
    {
58
        $version  = $api->getLoginParams()->getApiVersion();
59
        $instance = $api->getSession()->getInstance();
60
        $request  = new Request(
61
            'GET',
62
            sprintf(self::$endpoint, $instance, $version, $batch->getJobId()) . '/'
63
            . $batch->getId(),
64
            [
65
                'Content-Type'   => 'application/json; charset=UTF8',
66
                'X-SFDC-Session' => $api->getSession()->getSessionId()
67
            ]
68
        );
69
        $response = ApiHelper::getResponse($request, $api);
70
71
        return new BatchInfoDto($response->getBody()->getContents());
72
    }
73
74
    /**
75
     * @param ApiSalesforce $api
76
     * @param JobInfoDto    $job
77
     *
78
     * @return BatchInfoDto[]
79
     * @throws SFClientException
80
     * @throws \Exception
81
     */
82
    public static function infoForAllInJob(ApiSalesforce $api, JobInfoDto $job)
83
    {
84
        $version  = $api->getLoginParams()->getApiVersion();
85
        $instance = $api->getSession()->getInstance();
86
        $request  = new Request(
87
            'GET',
88
            sprintf(self::$endpoint, $instance, $version, $job->getId()),
89
            [
90
                'Content-Type'   => 'application/json; charset=UTF8',
91
                'X-SFDC-Session' => $api->getSession()->getSessionId()
92
            ]
93
        );
94
        $response = ApiHelper::getResponse($request, $api);
95
        try {
96
            $data   = json_decode($response->getBody()->getContents(), true);
97
            $result = [];
98
            foreach ($data['batchInfo'] as $item) {
99
                $result[$item['id']] = new BatchInfoDto($item);
100
            }
101
        } catch (\Exception $e) {
102
            $msg = 'SF batch api at parse of response error: ' . $e->getMessage()
103
                . ' ; Response = ' . $response->getBody()->getContents();
104
            $api->addError($msg);
105
            throw new SFClientException($msg);
106
        }
107
108
        return $result;
109
    }
110
111
    /**
112
     * @param ApiSalesforce $api
113
     * @param BatchInfoDto  $batch
114
     *
115
     * @return ResultAtBatchDto[]
116
     * @throws SFClientException
117
     * @throws ApiRequestException
118
     * @throws ApiResponseException
119
     * @throws HttpClientException
120
     */
121
    public static function results(ApiSalesforce $api, BatchInfoDto $batch)
122
    {
123
        $version  = $api->getLoginParams()->getApiVersion();
124
        $instance = $api->getSession()->getInstance();
125
        $request  = new Request(
126
            'GET',
127
            sprintf(self::$endpoint, $instance, $version, $batch->getJobId())
128
            . '/' . $batch->getId() . '/result',
129
            [
130
                'Content-Type'   => 'application/json; charset=UTF8',
131
                'X-SFDC-Session' => $api->getSession()->getSessionId()
132
            ]
133
        );
134
        $response = ApiHelper::getResponse($request, $api);
135
        try {
136
            $data   = json_decode($response->getBody()->getContents(), true);
137
            $result = [];
138
            foreach ($data as $item) {
139
                $result[] = new ResultAtBatchDto($item);
140
            }
141
        } catch (\Exception $e) {
142
            $msg = 'SF batch api at parse of response error: ' . $e->getMessage()
143
                . ' ; Response = ' . $response->getBody()->getContents();
144
            $api->addError($msg);
145
            throw new SFClientException($msg);
146
        }
147
148
        return $result;
149
    }
150
151
    /**
152
     * @param ApiSalesforce $api
153
     * @param BatchInfoDto  $batch
154
     * @param string        $batchResultId
155
     *
156
     * @return array
157
     * @throws SFClientException
158
     * @throws \Exception
159
     */
160
    public static function result(ApiSalesforce $api, BatchInfoDto $batch, $batchResultId)
161
    {
162
        $version  = $api->getLoginParams()->getApiVersion();
163
        $instance = $api->getSession()->getInstance();
164
        $request  = new Request(
165
            'GET',
166
            sprintf(self::$endpoint, $instance, $version, $batch->getJobId())
167
            . '/' . $batch->getId() . '/result/' . $batchResultId,
168
            [
169
                'Content-Type'   => 'application/json; charset=UTF8',
170
                'X-SFDC-Session' => $api->getSession()->getSessionId()
171
            ]
172
        );
173
        $response = ApiHelper::getResponse($request, $api);
174
        try {
175
            $data   = json_decode($response->getBody()->getContents(), true);
176
            $result = [];
177
            foreach ($data as $item) {
178
                $result[] = $item;
179
            }
180
        } catch (\Exception $e) {
181
            $msg = 'SF batch api at parse of response error: ' . $e->getMessage()
182
                . ' ; Response = ' . $response->getBody()->getContents();
183
            $api->addError($msg);
184
            throw new SFClientException($msg);
185
        }
186
187
        return $result;
188
    }
189
}