Completed
Pull Request — master (#149)
by Pierre
02:33
created

GuzzleClient   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.92%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 4
dl 0
loc 160
ccs 47
cts 49
cp 0.9592
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A getCommand() 0 16 3
A getDescription() 0 4 1
A getSerializer() 0 6 2
B getDeserializer() 0 11 5
A getConfig() 0 6 3
A setConfig() 0 4 1
B processConfig() 0 20 6
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 19
    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 19
        $this->config = $config;
52 19
        $this->description = $description;
53 19
        $serializer = $this->getSerializer($commandToRequestTransformer);
54 19
        $deserializer = $this->getDeserializer($responseToResultTransformer);
55
56 19
        parent::__construct($client, $serializer, $deserializer, $commandHandlerStack);
57 19
        $this->processConfig($config);
58 19
    }
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 16
    public function getCommand($name, array $args = [])
68
    {
69 16
        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 15
        $args += $this->getConfig('defaults');
80
81 15
        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 19
    private function getSerializer($commandToRequestTransformer)
101
    {
102
        return $commandToRequestTransformer ==! null
103 19
            ? $commandToRequestTransformer
104 19
            : 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 19
    private function getDeserializer($responseToResultTransformer)
114
    {
115 19
        $process = (! isset($this->config['process']) || $this->config['process'] === true);
116 19
        $locations = isset($this->config['response_locations']) && is_array($this->config['response_locations']) ?
117 19
            $this->config['response_locations'] : []
118 19
        ;
119
120
        return $responseToResultTransformer ==! null
121 19
            ? $responseToResultTransformer
122 19
            : new Deserializer($this->description, $process, $locations);
123
    }
124
125
    /**
126
     * Get the config of the client
127
     *
128
     * @param array|string $option
129
     * @return mixed
130
     */
131 16
    public function getConfig($option = null)
132
    {
133
        return $option === null
134 16
            ? $this->config
135 16
            : (isset($this->config[$option]) ? $this->config[$option] : []);
136
    }
137
138
    /**
139
     * @param $option
140
     * @param $value
141
     */
142 1
    public function setConfig($option, $value)
143
    {
144 1
        $this->config[$option] = $value;
145 1
    }
146
147
    /**
148
     * Prepares the client based on the configuration settings of the client.
149
     *
150
     * @param array $config Constructor config as an array
151
     */
152 19
    protected function processConfig(array $config)
153
    {
154
        // set defaults as an array if not provided
155 19
        if (!isset($config['defaults'])) {
156 19
            $config['defaults'] = [];
157 19
        }
158
159
        // Add the handlers based on the configuration option
160 19
        $stack = $this->getHandlerStack();
161
162 19
        if (!isset($config['validate']) || $config['validate'] === true) {
163 18
            $stack->push(new ValidatedDescriptionHandler($this->description), 'validate_description');
164 18
        }
165
166 19
        if (!isset($config['process']) || $config['process'] === true) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

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 the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
167
            // TODO: This belongs to the Deserializer and should be handled there.
168
            // Question: What is the result when the Deserializer is bypassed?
169
            // Possible answer: The raw response.
170 14
        }
171 19
    }
172
}
173