Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
50.00% |
1 / 2 |
CRAP | |
75.00% |
6 / 8 |
ClientFactory | |
0.00% |
0 / 1 |
|
50.00% |
1 / 2 |
2.06 | |
75.00% |
6 / 8 |
__construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
fromOptions | |
100.00% |
1 / 1 |
1 | |
100.00% |
6 / 6 |
<?php | |
/** | |
* @file | |
* Contains \Drupal\Core\Http\ClientFactory. | |
*/ | |
namespace Drupal\Core\Http; | |
use Drupal\Component\Utility\NestedArray; | |
use Drupal\Core\Site\Settings; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\HandlerStack; | |
/** | |
* Helper class to construct a HTTP client with Drupal specific config. | |
*/ | |
class ClientFactory { | |
/** | |
* The handler stack. | |
* | |
* @var \GuzzleHttp\HandlerStack | |
*/ | |
protected $stack; | |
/** | |
* Constructs a new ClientFactory instance. | |
* | |
* @param \GuzzleHttp\HandlerStack $stack | |
* The handler stack. | |
*/ | |
public function __construct(HandlerStack $stack) { | |
$this->stack = $stack; | |
} | |
/** | |
* Constructs a new client object from some configuration. | |
* | |
* @param array $config | |
* The config for the client. | |
* | |
* @return \GuzzleHttp\Client | |
* The HTTP client. | |
*/ | |
public function fromOptions(array $config = []) { | |
$default_config = [ | |
// Security consideration: we must not use the certificate authority | |
// file shipped with Guzzle because it can easily get outdated if a | |
// certificate authority is hacked. Instead, we rely on the certificate | |
// authority file provided by the operating system which is more likely | |
// going to be updated in a timely fashion. This overrides the default | |
// path to the pem file bundled with Guzzle. | |
'verify' => TRUE, | |
'timeout' => 30, | |
'headers' => [ | |
'User-Agent' => 'Drupal/' . \Drupal::VERSION . ' (+https://www.drupal.org/) ' . \GuzzleHttp\default_user_agent(), | |
], | |
'handler' => $this->stack, | |
]; | |
$config = NestedArray::mergeDeep($default_config, Settings::get('http_client_config', []), $config); | |
return new Client($config); | |
} | |
} |