Completed
Push — master ( d8465d...85c396 )
by Stefano
03:17
created

GuzzleClient::defaultCommandFactory()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 1
nop 1
crap 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A GuzzleClient::getConfig() 0 6 3
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 17
    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 17
        $this->config = $config;
52 17
        $this->description = $description;
53 17
        $serializer = $this->getSerializer($commandToRequestTransformer);
54 17
        $deserializer = $this->getDeserializer($responseToResultTransformer);
55
56 17
        parent::__construct($client, $serializer, $deserializer, $commandHandlerStack);
57 17
        $this->processConfig($config);
58 17
    }
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 14
    public function getCommand($name, array $args = [])
68
    {
69 14
        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 13
        $args += $this->getConfig('defaults');
80
81 13
        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 17
    private function getSerializer($commandToRequestTransformer)
101
    {
102
        return $commandToRequestTransformer ==! null
103 17
            ? $commandToRequestTransformer
104 17
            : 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 17
    private function getDeserializer($responseToResultTransformer)
114
    {
115 17
        $process = (! isset($this->config['process']) || $this->config['process'] === true);
116
117
        return $responseToResultTransformer ==! null
118 17
            ? $responseToResultTransformer
119 17
            : 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 14
    public function getConfig($option = null)
129
    {
130
        return $option === null
131 14
            ? $this->config
132 14
            : (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 17
    protected function processConfig(array $config)
150
    {
151
        // set defaults as an array if not provided
152 17
        if (!isset($config['defaults'])) {
153 17
            $config['defaults'] = [];
154 17
        }
155
156
        // Add the handlers based on the configuration option
157 17
        $stack = $this->getHandlerStack();
158
159 17
        if (!isset($config['validate']) || $config['validate'] === true) {
160 16
            $stack->push(new ValidatedDescriptionHandler($this->description));
161 16
        }
162
163 17
        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...
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 12
        }
168 17
    }
169
}
170