ApiHelper   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A getResponse() 0 21 4
1
<?php
2
3
namespace SalesforceBulkApi\helpers;
4
5
use SalesforceBulkApi\exceptions\ApiRequestException;
6
use SalesforceBulkApi\exceptions\ApiResponseException;
7
use SalesforceBulkApi\exceptions\HttpClientException;
8
use SalesforceBulkApi\services\ApiSalesforce;
9
use GuzzleHttp\Psr7\Request;
10
use GuzzleHttp\Psr7\Response;
11
12
class ApiHelper
13
{
14
    /**
15
     * @param Request       $request
16
     * @param ApiSalesforce $service
17
     *
18
     * @return Response
19
     * @throws ApiRequestException
20
     * @throws ApiResponseException
21
     * @throws HttpClientException
22
     */
23
    public static function getResponse(Request $request, ApiSalesforce $service)
24
    {
25
        try {
26
            $response = $service->send($request);
27
        } catch (\Exception $e) {
28
            throw new HttpClientException($e->getMessage());
29
        }
30
        if ($response->getStatusCode() == 500) {
31
            $error = 'API error: ' . $response->getBody()->getContents();
32
            $service->addError($error);
33
            throw new ApiRequestException($error);
34
        }
35
        if ($response->getStatusCode() >= 300) {
36
            $error =
37
                'API error: Status = ' . $response->getStatusCode() . ' ; ReasonPhrase = '
38
                . $response->getReasonPhrase() . ' ; Body = ' . $response->getBody()->getContents();
39
            $service->addError($error);
40
            throw new ApiResponseException($error);
41
        }
42
43
        return $response;
44
    }
45
}