Completed
Pull Request — master (#4)
by Ashley
14:00
created

ClientBuilder::buildWithJwtAuth()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 0
cp 0
rs 8.8571
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
52
        $authClient = new \GuzzleHttp\Client($guzzleOptions);
53
54
        $jwtManager = new JwtManager(
55
            $authClient,
56
            $authStrategy,
57
            $tokenOptions
58
        );
59
60
        $stack = HandlerStack::create();
61
        $stack->push(new JwtMiddleware($jwtManager));
62
63
        $client = new \GuzzleHttp\Client(['handler' => $stack, 'base_uri' => $baseEndpoint]);
64
65
        return new \Softonic\GraphQL\Client(
66
            $client,
67
            new \Softonic\GraphQL\ResponseBuilder()
68
        );
69
    }
70
}
71