Completed
Push — 3.x ( 459302...396840 )
by Ryota
16s queued 13s
created

ClientFactory::createHttpClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Polidog\Chatwork\Client;
6
7
use GuzzleHttp\Handler\CurlHandler;
8
use GuzzleHttp\HandlerStack;
9
use GuzzleHttp\Middleware;
10
use Psr\Http\Message\RequestInterface;
11
12
final class ClientFactory
13
{
14
    /**
15
     * HttpOptions.
16
     *
17
     * @var array
18
     */
19
    private static $httpOptions = [
20
        'base_uri' => 'https://api.chatwork.com/',
21
        'defaults' => [
22
            'timeout' => 60,
23
            'debug' => false,
24
        ],
25
        'headers' => [
26
            'User-Agent' => 'php-chatwork-api v2',
27
            'Accept' => 'application/json',
28
        ],
29
    ];
30
31
    public static function createHttpClient(string $chatworkToken, array $middlewares = []): \GuzzleHttp\Client
32
    {
33
        $stack = new HandlerStack();
34
        $stack->setHandler(new CurlHandler());
35
        $stack->push(Middleware::mapRequest(static fn (RequestInterface $request) => $request->withHeader('X-ChatWorkToken', $chatworkToken)));
36
        foreach ($middlewares as $middleware) {
37
            $stack->push($middleware);
38
        }
39
40
        $options = array_merge(self::$httpOptions, ['handler' => $stack]);
41
        return new \GuzzleHttp\Client($options);
42
    }
43
}
44