1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Surge\LaravelSalesforce; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use Surge\LaravelSalesforce\Objects\BaseObject; |
7
|
|
|
|
8
|
|
|
class Salesforce |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $objName; |
14
|
|
|
|
15
|
|
|
public $baseUrl; |
16
|
|
|
|
17
|
|
|
public $instanceUrl; |
18
|
|
|
|
19
|
|
|
public $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Salesforce constructor. |
23
|
|
|
* |
24
|
|
|
* @param ClientInterface $client |
25
|
|
|
* @param string $url |
26
|
|
|
* @param string $instanceUrl |
27
|
|
|
*/ |
28
|
|
|
public function __construct(ClientInterface $client, string $url, string $instanceUrl) |
29
|
|
|
{ |
30
|
|
|
$this->client = $client; |
31
|
|
|
$this->baseUrl = $url; |
32
|
|
|
$this->instanceUrl = $instanceUrl; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $method |
37
|
|
|
* @param array $args |
38
|
|
|
* @return bool|mixed|string |
39
|
|
|
*/ |
40
|
|
|
public function __call($method, $args) |
41
|
|
|
{ |
42
|
|
|
if (0 === strpos($method, 'create')) { |
43
|
|
|
return $this->callCreateOnObject($method, $args); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (0 === strpos($method, 'update')) { |
47
|
|
|
return $this->callUpdateOnObject($method, $args); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (0 === strpos($method, 'delete')) { |
51
|
|
|
return $this->callDeleteOnObject($method, $args); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (0 === strpos($method, 'get')) { |
55
|
|
|
return $this->callGetOnObject($method, $args); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (0 === strpos($method, 'init')) { |
59
|
|
|
return $this->callInitObject($method); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$class = new BaseObject($this); |
63
|
|
|
|
64
|
|
|
return call_user_func_array([$class, $method], $args); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Create object dynamically |
69
|
|
|
* |
70
|
|
|
* @param $method |
71
|
|
|
* @param $args |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
private function callCreateOnObject($method, $args) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$type = substr($method, 6); |
77
|
|
|
$class = '\\Surge\\LaravelSalesforce\\Objects\\' . $type; |
78
|
|
|
|
79
|
|
|
if (class_exists($class)) { |
80
|
|
|
return (new $class($this))->create($args[0]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return (new BaseObject($this, $type))->create($args[0]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
private function callUpdateOnObject($method, $args) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$type = substr($method, 6); |
89
|
|
|
$class = '\\Surge\\LaravelSalesforce\\Objects\\' . $type; |
90
|
|
|
|
91
|
|
|
if (class_exists($class)) { |
92
|
|
|
return (new $class($this))->update($args[0]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return (new BaseObject($this, $type))->update($type, $args[0]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
private function callDeleteOnObject($method, $args) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$type = substr($method, 6); |
101
|
|
|
$class = '\\Surge\\LaravelSalesforce\\Objects\\' . $type; |
102
|
|
|
|
103
|
|
|
if (class_exists($class)) { |
104
|
|
|
return (new $class($this))->delete($args[0]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return (new BaseObject($this, $type))->delete($args[0]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function callGetOnObject($method, $args) |
111
|
|
|
{ |
112
|
|
|
$type = substr($method, 3); |
113
|
|
|
$class = '\\Surge\\LaravelSalesforce\\Objects\\' . $type; |
114
|
|
|
|
115
|
|
|
if (class_exists($class)) { |
116
|
|
|
return (new $class($this))->get($args[0]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return (new BaseObject($this, $type))->get($args[0]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function callInitObject($method) |
123
|
|
|
{ |
124
|
|
|
$type = substr($method, 4); |
125
|
|
|
$class = '\\Surge\\LaravelSalesforce\\Objects\\' . $type; |
126
|
|
|
|
127
|
|
|
if (class_exists($class)) { |
128
|
|
|
return new $class($this); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return new BaseObject($this, $type); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
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.