These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace GuzzleHttp\Command\Guzzle; |
||
3 | |||
4 | use GuzzleHttp\ClientInterface; |
||
5 | use GuzzleHttp\Command\CommandInterface; |
||
6 | use GuzzleHttp\Command\Guzzle\Handler\ValidatedDescriptionHandler; |
||
7 | use GuzzleHttp\Command\ServiceClient; |
||
8 | use GuzzleHttp\HandlerStack; |
||
9 | |||
10 | /** |
||
11 | * Default Guzzle web service client implementation. |
||
12 | */ |
||
13 | class GuzzleClient extends ServiceClient |
||
14 | { |
||
15 | /** @var array $config */ |
||
16 | private $config; |
||
17 | |||
18 | /** @var DescriptionInterface Guzzle service description */ |
||
19 | private $description; |
||
20 | |||
21 | /** |
||
22 | * The client constructor accepts an associative array of configuration |
||
23 | * options: |
||
24 | * |
||
25 | * - defaults: Associative array of default command parameters to add to |
||
26 | * each command created by the client. |
||
27 | * - validate: Specify if command input is validated (defaults to true). |
||
28 | * Changing this setting after the client has been created will have no |
||
29 | * effect. |
||
30 | * - process: Specify if HTTP responses are parsed (defaults to true). |
||
31 | * Changing this setting after the client has been created will have no |
||
32 | * effect. |
||
33 | * - response_locations: Associative array of location types mapping to |
||
34 | * ResponseLocationInterface objects. |
||
35 | * |
||
36 | * @param ClientInterface $client HTTP client to use. |
||
37 | * @param DescriptionInterface $description Guzzle service description |
||
38 | * @param callable $commandToRequestTransformer |
||
39 | * @param callable $responseToResultTransformer |
||
40 | * @param HandlerStack $commandHandlerStack |
||
41 | * @param array $config Configuration options |
||
42 | */ |
||
43 | 18 | public function __construct( |
|
44 | ClientInterface $client, |
||
45 | DescriptionInterface $description, |
||
46 | callable $commandToRequestTransformer = null, |
||
47 | callable $responseToResultTransformer = null, |
||
48 | HandlerStack $commandHandlerStack = null, |
||
49 | array $config = [] |
||
50 | ) { |
||
51 | 18 | $this->config = $config; |
|
52 | 18 | $this->description = $description; |
|
53 | 18 | $serializer = $this->getSerializer($commandToRequestTransformer); |
|
54 | 18 | $deserializer = $this->getDeserializer($responseToResultTransformer); |
|
55 | |||
56 | 18 | parent::__construct($client, $serializer, $deserializer, $commandHandlerStack); |
|
57 | 18 | $this->processConfig($config); |
|
58 | 18 | } |
|
59 | |||
60 | /** |
||
61 | * Returns the command if valid; otherwise an Exception |
||
62 | * @param string $name |
||
63 | * @param array $args |
||
64 | * @return CommandInterface |
||
65 | * @throws \InvalidArgumentException |
||
66 | */ |
||
67 | 15 | public function getCommand($name, array $args = []) |
|
68 | { |
||
69 | 15 | if (!$this->description->hasOperation($name)) { |
|
70 | 3 | $name = ucfirst($name); |
|
71 | 3 | if (!$this->description->hasOperation($name)) { |
|
72 | 1 | throw new \InvalidArgumentException( |
|
73 | 1 | "No operation found named {$name}" |
|
74 | 1 | ); |
|
75 | } |
||
76 | 2 | } |
|
77 | |||
78 | // Merge in default command options |
||
79 | 14 | $args += $this->getConfig('defaults'); |
|
80 | |||
81 | 14 | return parent::getCommand($name, $args); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Return the description |
||
86 | * |
||
87 | * @return DescriptionInterface |
||
88 | */ |
||
89 | 1 | public function getDescription() |
|
90 | { |
||
91 | 1 | return $this->description; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Returns the passed Serializer when set, a new instance otherwise |
||
96 | * |
||
97 | * @param callable|null $commandToRequestTransformer |
||
98 | * @return \GuzzleHttp\Command\Guzzle\Serializer |
||
99 | */ |
||
100 | 18 | private function getSerializer($commandToRequestTransformer) |
|
101 | { |
||
102 | return $commandToRequestTransformer ==! null |
||
103 | 18 | ? $commandToRequestTransformer |
|
104 | 18 | : new Serializer($this->description); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Returns the passed Deserializer when set, a new instance otherwise |
||
109 | * |
||
110 | * @param callable|null $responseToResultTransformer |
||
111 | * @return \GuzzleHttp\Command\Guzzle\Deserializer |
||
112 | */ |
||
113 | 18 | private function getDeserializer($responseToResultTransformer) |
|
114 | { |
||
115 | 18 | $process = (! isset($this->config['process']) || $this->config['process'] === true); |
|
116 | |||
117 | return $responseToResultTransformer ==! null |
||
118 | 18 | ? $responseToResultTransformer |
|
119 | 18 | : new Deserializer($this->description, $process); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Get the config of the client |
||
124 | * |
||
125 | * @param array|string $option |
||
126 | * @return mixed |
||
127 | */ |
||
128 | 15 | public function getConfig($option = null) |
|
129 | { |
||
130 | return $option === null |
||
131 | 15 | ? $this->config |
|
132 | 15 | : (isset($this->config[$option]) ? $this->config[$option] : []); |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param $option |
||
137 | * @param $value |
||
138 | */ |
||
139 | 1 | public function setConfig($option, $value) |
|
140 | { |
||
141 | 1 | $this->config[$option] = $value; |
|
142 | 1 | } |
|
143 | |||
144 | /** |
||
145 | * Prepares the client based on the configuration settings of the client. |
||
146 | * |
||
147 | * @param array $config Constructor config as an array |
||
148 | */ |
||
149 | 18 | protected function processConfig(array $config) |
|
150 | { |
||
151 | // set defaults as an array if not provided |
||
152 | 18 | if (!isset($config['defaults'])) { |
|
153 | 18 | $config['defaults'] = []; |
|
154 | 18 | } |
|
155 | |||
156 | // Add the handlers based on the configuration option |
||
157 | 18 | $stack = $this->getHandlerStack(); |
|
158 | |||
159 | 18 | if (!isset($config['validate']) || $config['validate'] === true) { |
|
160 | 17 | $stack->push(new ValidatedDescriptionHandler($this->description), 'validate_description'); |
|
161 | 17 | } |
|
162 | |||
163 | 18 | if (!isset($config['process']) || $config['process'] === true) { |
|
0 ignored issues
–
show
|
|||
164 | // TODO: This belongs to the Deserializer and should be handled there. |
||
165 | // Question: What is the result when the Deserializer is bypassed? |
||
166 | // Possible answer: The raw response. |
||
167 | 13 | } |
|
168 | 18 | } |
|
169 | } |
||
170 |
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.