Completed
Pull Request — master (#4)
by Ashley
11:32
created

ClientBuilder::buildWithJwtAuth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 0
cp 0
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Softonic\GraphQL;
4
5
use Eljam\GuzzleJwt\JwtMiddleware;
6
use Eljam\GuzzleJwt\Manager\JwtManager;
7
use Eljam\GuzzleJwt\Strategy\Auth\AuthStrategyInterface;
8
use GuzzleHttp\HandlerStack;
9
use League\OAuth2\Client\Provider\AbstractProvider as OAuth2Provider;
10 2
use Psr\Cache\CacheItemPoolInterface as Cache;
11
12 2
class ClientBuilder
13 2
{
14 2
    public static function build(string $endpoint)
15
    {
16
        return new \Softonic\GraphQL\Client(
17
            new \GuzzleHttp\Client(['base_uri' => $endpoint]),
18 2
            new \Softonic\GraphQL\ResponseBuilder()
19
        );
20
    }
21
22
    public static function buildWithOAuth2Provider(
23
        string $endpoint,
24
        OAuth2Provider $oauthProvider,
25 2
        array $tokenOptions,
26
        Cache $cache
27
    ): Client {
28 2
        $guzzleOptions = [
29 2
            'base_uri' => $endpoint,
30 1
        ];
31 1
32 1
        return new \Softonic\GraphQL\Client(
33 1
            \Softonic\OAuth2\Guzzle\Middleware\ClientBuilder::build(
34
                $oauthProvider,
35 2
                $tokenOptions,
36
                $cache,
37
                $guzzleOptions
38
            ),
39
            new \Softonic\GraphQL\ResponseBuilder()
40
        );
41
    }
42
43
    public static function buildWithJwtAuth(
44
        string $baseEndpoint,
45
        array $tokenOptions,
46
        AuthStrategyInterface $authStrategy
47
    ): Client {
48
        $guzzleOptions = [
49
            'base_uri' => $baseEndpoint,
50
        ];
51
        $jwtManager = new JwtManager(
52
            new \GuzzleHttp\Client($guzzleOptions),
53
            $authStrategy,
54
            $tokenOptions
55
        );
56
        $stack = HandlerStack::create();
57
        $stack->push(new JwtMiddleware($jwtManager));
58
        return new \Softonic\GraphQL\Client(
59
            new \GuzzleHttp\Client([
60
                'handler' => $stack,
61
                'base_uri' => $baseEndpoint
62
            ]),
63
            new \Softonic\GraphQL\ResponseBuilder()
64
        );
65
    }
66
}
67