Completed
Push — master ( 4762fe...4246d1 )
by
unknown
01:30
created

AbstractObject::report()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Surge\LaravelSalesforce\Objects;
4
5
use GuzzleHttp\Exception\ClientException;
6
use Surge\LaravelSalesforce\Events\RequestSent;
7
use Surge\LaravelSalesforce\Events\ResponseReceived;
8
use Surge\LaravelSalesforce\Exceptions\SalesforceException;
9
use Surge\LaravelSalesforce\Salesforce;
10
11
abstract class AbstractObject implements ObjectInterface
12
{
13
    protected $salesforce;
14
15
    public function __construct(Salesforce $salesforce)
16
    {
17
        $this->salesforce = $salesforce;
18
    }
19
20
    /**
21
     * @param string $method
22
     * @param string $url
23
     * @param array  $options
24
     *
25
     * @return object
26
     */
27
    protected function sendRequest(string $method, string $url, array $options = [])
28
    {
29
        event(new RequestSent([
30
            'data' => $options,
31
            'url'     => $url,
32
            'class'   => get_class($this),
33
            'type'    => 'REQUEST',
34
        ]));
35
36
        try {
37
            $response = json_decode(
38
                $this->salesforce->client->request($method, $this->salesforce->baseUrl . $url, $options)
39
                    ->getBody());
40
        } catch (ClientException $e) {
41
            throw new SalesforceException($e->getMessage());
42
        }
43
44
45
        event(new ResponseReceived([
46
            'data' => $response,
47
            'url'     => $url,
48
            'class'   => get_class($this),
49
            'type'    => 'RESPONSE',
50
        ]));
51
52
        return $response;
53
    }
54
55
    protected function getType()
56
    {
57
        if (isset($this->type)) {
58
            return $this->type;
59
        }
60
61
        return (new \ReflectionClass($this))->getShortName();
62
    }
63
64
    /**
65
     * Get latest version.
66
     *
67
     * @return mixed
68
     */
69
    public function getVersion()
70
    {
71
        return $this->sendRequest('GET', $this->salesforce->instanceUrl . '/services/data');
72
    }
73
74
    /**
75
     * Get all organisation limits.
76
     */
77
    public function listOrganisationLimits()
78
    {
79
        return $this->sendRequest('GET', $this->salesforce->instanceUrl . $this->version['url'] . '/limits');
0 ignored issues
show
Bug introduced by
The property version does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
80
    }
81
82
    /**
83
     * List all available resources.
84
     *
85
     * @return mixed
86
     */
87
    public function listAvailableResources()
88
    {
89
        return $this->sendRequest('GET', '');
90
    }
91
92
    /**
93
     * List all objects.
94
     *
95
     * @return mixed
96
     */
97
    public function listObjects()
98
    {
99
        return $this->sendRequest('GET', '/sobjects');
100
    }
101
102
    /**
103
     * Describe an object.
104
     *
105
     * @param $objectName
106
     *
107
     * @return mixed
108
     */
109
    public function describeObject($objectName)
110
    {
111
        return $this->sendRequest('GET', '/sobjects/' . $objectName . '/describe');
112
    }
113
114
    /**
115
     * Describe basic object.
116
     *
117
     * @param $objectName
118
     *
119
     * @return mixed
120
     */
121
    public function describeBasicObject($objectName)
122
    {
123
        return $this->sendRequest('GET', '/sobjects/' . $objectName);
124
    }
125
126
    /**
127
     * Run Salesforce query.
128
     *
129
     * @param $query
130
     *
131
     * @return mixed
132
     */
133
    public function query($query)
134
    {
135
        return $this->sendRequest('GET', '/query', [
136
            'query' => [
137
                'q' => $query,
138
            ],
139
        ]);
140
    }
141
142
    /**
143
     * Get record.
144
     *
145
     * @param string $id
146
     *
147
     * @param array  $fields
148
     */
149
    public function get(string $id, array $fields = [])
150
    {
151
        return $this->sendRequest('GET', "/sobjects/" . $this->getType() . "/$id", ['query' => $fields]);
152
    }
153
154
    /**
155
     * Update.
156
     *
157
     * @param  string $id
158
     * @param        $params
159
     * @return void
160
     */
161
    public function update(string $id, array $params)
162
    {
163
        $this->sendRequest('PATCH', "/sobjects/" . $this->getType() . "/$id",
164
            [
165
                'json' => $params,
166
            ]
167
        );
168
    }
169
170
    /**
171
     * Insert new account.
172
     *
173
     * @param $params
174
     *
175
     * @throws SalesforceException
176
     */
177 View Code Duplication
    public function create(array $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179
        $response = $this->sendRequest('POST', "/sobjects/" . $this->getType(), [
180
            'json' => $params,
181
        ]);
182
183
        if ($response->success !== true) {
184
            throw new SalesforceException($response->errors);
185
        }
186
187
        return $response;
188
    }
189
190
    /**
191
     * Delete a given record
192
     *
193
     * @param string $id
194
     * @throws SalesforceException
195
     */
196 View Code Duplication
    public function delete(string $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
    {
198
        $response = $this->sendRequest('DELETE', "/sobjects/" . $this->getType() . "/$id");
199
200
        if ($response->success !== true) {
201
            throw new SalesforceException($response->errors);
202
        }
203
204
        return $response;
205
    }
206
207
    public function report(string $id, bool $includeDetails = true)
208
    {
209
        return $this->sendRequest(
210
            'GET',
211
            '/analytics/reports/' . $id,
212
            ['query' => ['includeDetails' => $includeDetails]]
213
        );
214
    }
215
}
216