Completed
Pull Request — master (#4)
by Ashley
07:59 queued 06:58
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 26
cts 26
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
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