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

ClientBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 7
dl 0
loc 55
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 7 1
A buildWithOAuth2Provider() 0 20 1
A buildWithJwtAuth() 0 23 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 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