Completed
Pull Request — master (#4)
by Ashley
07:59 queued 06:58
created

ClientBuilder::buildWithJwtAuth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

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