HttpClientFactory::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 12
rs 10
1
<?php
2
3
namespace StephaneCoinon\SendGridActivity;
4
5
use Http\Client\Common\Plugin\AuthenticationPlugin;
6
use Http\Client\Common\Plugin\ErrorPlugin;
7
use Http\Client\Common\PluginClient;
8
use Http\Client\HttpClient;
9
use Http\Discovery\HttpClientDiscovery;
10
use Http\Message\Authentication\Bearer;
11
12
class HttpClientFactory
13
{
14
    /**
15
     * Build the HTTP client to talk with the API.
16
     *
17
     * @param string     $apiKey  API key
18
     * @param Plugin[]   $plugins List of additional plugins to use
19
     * @param HttpClient $client  Base HTTP client
20
     *
21
     * @return HttpClient
22
     */
23
    public static function create($apiKey, array $plugins = [], HttpClient $client = null): HttpClient
24
    {
25
        if (!$client) {
26
            $client = HttpClientDiscovery::find();
27
        }
28
29
        // $plugins[] = new ErrorPlugin();
30
        $plugins[] = new AuthenticationPlugin(
31
            new Bearer($apiKey ?? getenv('SENDGRID_API_TOKEN'))
32
        );
33
34
        return new PluginClient($client, $plugins);
35
    }
36
}
37