ApiSalesforce::addError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SalesforceBulkApi\services;
4
5
use SalesforceBulkApi\api\UserApiSF;
6
use SalesforceBulkApi\dto\LoginResponseDto;
7
use GuzzleHttp\Client;
8
use SalesforceBulkApi\conf\LoginParams;
9
use GuzzleHttp\Psr7\Request;
10
use GuzzleHttp\Psr7\Response;
11
12
class ApiSalesforce
13
{
14
    /**
15
     * @var LoginParams
16
     */
17
    private $loginParams;
18
19
    /**
20
     * @var Client
21
     */
22
    private $httpClient;
23
24
    /**
25
     * @var Response
26
     */
27
    private $lastResponse;
28
29
    /**
30
     * @var array
31
     */
32
    private $errors = [];
33
34
    /**
35
     * @var LoginResponseDto
36
     */
37
    private $session;
38
39
    /**
40
     * @param LoginParams $loginParams
41
     * @param array       $guzzleHttpClientConfig
42
     * @throws \Exception
43
     */
44
    public function __construct(LoginParams $loginParams, array $guzzleHttpClientConfig = ['timeout' => 3])
45
    {
46
        $this->loginParams = $loginParams;
47
        $this->httpClient  = new Client($guzzleHttpClientConfig);
48
        $this->session     = UserApiSF::login($this);
49
    }
50
51
    /**
52
     * @param Request $request
53
     * @param array   $options
54
     *
55
     * @return Response
56
     * @throws \Exception
57
     */
58
    public function send(Request $request, array $options = [])
59
    {
60
        try {
61
            $this->lastResponse = $this->httpClient->send($request, $options);
62
        } catch (\Exception $e) {
63
            $requestInfo    = [
64
                'uri'     => $request->getUri(),
65
                'method'  => $request->getMethod(),
66
                'headers' => $request->getHeaders(),
67
                'body'    => (string)$request->getBody()
68
            ];
69
            $errorMsg       =
70
                'ApiSalesforce request error: ' . $e->getCode() . ' ; ' . $e->getMessage() . ' ; Request info: '
71
                . json_encode($requestInfo);
72
            $this->errors[] = $errorMsg;
73
74
            throw new \Exception($errorMsg);
75
        }
76
77
        return clone $this->lastResponse;
78
    }
79
80
    /**
81
     * @return LoginParams
82
     */
83
    public function getLoginParams()
84
    {
85
        return $this->loginParams;
86
    }
87
88
    /**
89
     * @return Response
90
     */
91
    public function getLastResponse()
92
    {
93
        return $this->lastResponse;
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    public function getErrors()
100
    {
101
        return $this->errors;
102
    }
103
104
    /**
105
     * @param string $msg
106
     */
107
    public function addError($msg)
108
    {
109
        $this->errors[] = (string)$msg;
110
    }
111
112
    /**
113
     * @return LoginResponseDto
114
     */
115
    public function getSession()
116
    {
117
        return $this->session;
118
    }
119
}