Completed
Push — master ( 2dac38...17d12a )
by Ashleigh
01:38
created

AbstractObject   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 244
Duplicated Lines 12.3 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 6
dl 30
loc 244
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A sendRequest() 0 20 1
A getVersion() 0 4 1
A listOrganisationLimits() 0 4 1
A listAvailableResources() 0 4 1
A listObjects() 0 4 1
A describeObject() 0 4 1
A describeBasicObject() 0 4 1
A runReport() 0 8 1
A query() 0 6 1
A get() 0 14 3
B update() 0 24 4
A create() 16 16 3
A delete() 14 14 3
A getType() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Surge\LaravelSalesforce\Objects;
4
5
use Event;
6
use GuzzleHttp\ClientInterface;
7
use Surge\LaravelSalesforce\Events\RequestSent;
8
use Surge\LaravelSalesforce\Events\ResponseReceived;
9
use Surge\LaravelSalesforce\Exceptions\SalesforceException;
10
use Surge\LaravelSalesforce\Salesforce;
11
12
abstract class AbstractObject implements ObjectInterface
13
{
14
    protected $recordType;
15
16
    protected $salesforce;
17
18
    public function __construct(Salesforce $salesforce)
19
    {
20
        $this->salesforce = $salesforce;
21
    }
22
23
    /**
24
     * @param string $method
25
     * @param string $url
26
     * @param array  $options
27
     *
28
     * @return string
29
     */
30
    protected function sendRequest(string $method, string $url, array $options = []): string
31
    {
32
        Event::fire(new RequestSent([
33
            'options' => $options,
34
            'url'     => $url,
35
            'class'   => get_class($this),
36
            'type'    => 'REQUEST',
37
        ]));
38
39
        $response = json_decode($this->salesforce->client->request($method, $this->salesforce->baseUrl.$url, $options)->getBody());
40
41
        Event::fire(new ResponseReceived([
42
            'options' => $response,
43
            'url'     => $url,
44
            'class'   => get_class($this),
45
            'type'    => 'RESPONSE',
46
        ]));
47
48
        return $response;
49
    }
50
51
    /**
52
     * Get latest version.
53
     *
54
     * @return mixed
55
     */
56
    protected function getVersion()
57
    {
58
        return $this->sendRequest('GET', $this->salesforce->instanceUrl.'/services/data');
59
    }
60
61
    /**
62
     * Get all organisation limits.
63
     */
64
    protected function listOrganisationLimits()
65
    {
66
        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...
67
    }
68
69
    /**
70
     * List all available resources.
71
     *
72
     * @return mixed
73
     */
74
    protected function listAvailableResources()
75
    {
76
        return $this->sendRequest('GET', '');
77
    }
78
79
    /**
80
     * List all objects.
81
     *
82
     * @return mixed
83
     */
84
    protected function listObjects()
85
    {
86
        return $this->sendRequest('GET', '/sobjects');
87
    }
88
89
    /**
90
     * Describe an object.
91
     *
92
     * @param $objectName
93
     *
94
     * @return mixed
95
     */
96
    protected function describeObject($objectName)
97
    {
98
        return $this->sendRequest('GET', '/sobjects/'.$objectName.'/describe');
99
    }
100
101
    /**
102
     * Describe basic object.
103
     *
104
     * @param $objectName
105
     *
106
     * @return mixed
107
     */
108
    protected function describeBasicObject($objectName)
109
    {
110
        return $this->sendRequest('GET', '/sobjects/'.$objectName);
111
    }
112
113
    /**
114
     * Run report.
115
     *
116
     * @param string $id
117
     * @param bool   $includeDetails
118
     * @return mixed
119
     *
120
     */
121
    public function runReport(string $id, bool $includeDetails)
122
    {
123
        return $this->sendRequest(
124
            'GET',
125
            '/analytics/reports/' . $id,
126
            ['query' => ['includeDetails' => $includeDetails]]
127
        );
128
    }
129
130
    /**
131
     * Run Salesforce query.
132
     *
133
     * @param $query
134
     *
135
     * @return mixed
136
     */
137
    protected function query($query)
138
    {
139
        return $this->sendRequest('GET', '/query', ['query' => [
140
            'q' => $query,
141
        ]]);
142
    }
143
144
    /**
145
     * Get record.
146
     *
147
     * @param string $id
148
     *
149
     * @param array  $fields
150
     * @return bool|mixed
151
     */
152
    public function get(string $id, array $fields = [])
153
    {
154
        if (!$id) {
155
            return false;
156
        }
157
158
        $response = $this->sendRequest('GET', "/sobjects/" . $this->getType() . "/$id", ['query' => $fields]);
159
160
        if (!$response) {
161
            return false;
162
        }
163
164
        return $response;
165
    }
166
167
    /**
168
     * Update.
169
     *
170
     * @param string $id
171
     * @param $params
172
     * @return bool|mixed
173
     * @throws SalesforceException
174
     */
175
    public function update(string $id, array $params)
176
    {
177
        if (!$id) {
178
            return false;
179
        }
180
181
        $response = $this->sendRequest(
182
            'PATCH',
183
            "/sobjects/$this->recordType/$id",
184
            [
185
                'json' => $params,
186
            ]
187
        );
188
189
        if (!$response) {
190
            return false;
191
        }
192
193
        if ($response->success !== true) {
194
            throw new SalesforceException($response->errors);
195
        }
196
197
        return $response;
198
    }
199
200
    /**
201
     * Insert new account.
202
     *
203
     * @param $params
204
     *
205
     * @return bool
206
     * @throws SalesforceException
207
     */
208 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...
209
    {
210
        $response = $this->sendRequest('POST', "/sobject/" . $this->getType(), [
211
            'json' => $params,
212
        ]);
213
214
        if (!$response) {
215
            return false;
216
        }
217
218
        if ($response->success !== true) {
219
            throw new SalesforceException($response->errors);
220
        }
221
222
        return $response;
223
    }
224
225
    /**
226
     * Delete a given record
227
     *
228
     * @param string $id
229
     * @return bool
230
     * @throws SalesforceException
231
     */
232 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...
233
    {
234
        $response = $this->sendRequest('DELETE', "/sobjects/" . $this->getType() ."/$id");
235
236
        if (!$response) {
237
            return false;
238
        }
239
240
        if ($response->success !== true) {
241
            throw new SalesforceException($response->errors);
242
        }
243
244
        return $response;
245
    }
246
247
    protected function getType()
248
    {
249
        if (isset($this->type)) {
250
            return $this->type;
0 ignored issues
show
Bug introduced by
The property type 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...
251
        }
252
253
        return get_class($this);
254
    }
255
}
256