Completed
Push — master ( 4b948a...4ff52b )
by
unknown
06:16
created

Salesforce::callExistsOnObject()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 4
nop 2
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 (starts_with($method, 'create')) {
43
            return $this->callCreateOnObject($method, $args);
44
        }
45
46
        if (starts_with($method, 'update')) {
47
            return $this->callUpdateOnObject($method, $args);
48
        }
49
50
        if (starts_with($method, 'delete')) {
51
            return $this->callDeleteOnObject($method, $args);
52
        }
53
54
        if (starts_with($method, 'get')) {
55
            return $this->callGetOnObject($method, $args);
56
        }
57
58
        if (starts_with($method, 'exists')) {
59
            return $this->callExistsOnObject($method, $args);
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)
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...
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
    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], $args[1]);
93
        }
94
95
        return (new BaseObject($this, $type))->update($type, $args[0]);
96
    }
97
98 View Code Duplication
    private function callDeleteOnObject($method, $args)
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...
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 callExistsOnObject($method, $args)
123
    {
124
        $type = substr($method, 6);
125
        $class = '\\Surge\\LaravelSalesforce\\Objects\\' . $type;
126
127
        if (class_exists($class)) {
128
            if (isset($args[1]) && isset($args[2])) { //second and third params are optional
129
                return (new $class($this))->exists($args[0], $args[1], $args[2]);
130
            } elseif (isset($args[1])) {
131
                return (new $class($this))->exists($args[0], $args[1]);
132
            } else {
133
                return (new $class($this))->exists($args[0]);
134
            }
135
        }
136
137
        return (new BaseObject($this, $type))->get($args[0]);
138
    }
139
140
    /**
141
     * Run query
142
     *
143
     * @param $query
144
     * @return mixed
145
     */
146
    public function runQuery($query)
147
    {
148
        $response = $this->query($query);
0 ignored issues
show
Documentation Bug introduced by
The method query does not exist on object<Surge\LaravelSalesforce\Salesforce>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
149
150
        if ($response && $response->totalSize > 0) {
151
            return $response->records;
152
        }
153
154
        return false;
155
    }
156
157
    /**
158
     * Run report.
159
     *
160
     * @param string $id
161
     * @param bool   $includeDetails
162
     * @return mixed
163
     *
164
     */
165
    public function getReport(string $id, bool $includeDetails = true)
166
    {
167
        return $this->report($id, $includeDetails);
0 ignored issues
show
Bug introduced by
The method report() does not exist on Surge\LaravelSalesforce\Salesforce. Did you maybe mean getReport()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
168
    }
169
}
170