HTTPClientFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 16 1
A createOAuth2Middleware() 0 22 1
A createCacheMiddleware() 0 8 1
1
<?php
2
3
namespace LibLynx\Connect\HTTPClient;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\HandlerStack;
7
use GuzzleHttp\Client as GuzzleClient;
8
use kamermans\OAuth2\GrantType\ClientCredentials;
9
use kamermans\OAuth2\OAuth2Middleware;
10
use Kevinrob\GuzzleCache\CacheMiddleware;
11
use Kevinrob\GuzzleCache\Storage\Psr16CacheStorage;
12
use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy;
13
use Psr\SimpleCache\CacheInterface;
14
15
/**
16
 * Provides the means for creating an OAuth2-capable, cache-aware HTTP client
17
 *
18
 * This can also be modified to provide the means to test the client
19
 *
20
 * @package LibLynx\Connect
21
 */
22
class HTTPClientFactory
23
{
24
    /** @var CacheInterface */
25
    protected $cache;
26
27
    /** @var callable RequestStack handler for API requests */
28
    private $apiHandler = null;
29
30
    /** @var callable RequestStack handler for OAuth2 requests */
31
    private $oauth2Handler = null;
32
33
    public function __construct(callable $apiHandler = null, callable $oauth2Handler = null)
34
    {
35
        $this->apiHandler = $apiHandler;
36
        $this->oauth2Handler = $oauth2Handler;
37
    }
38
39
    public function create($apiRoot, $clientId, $clientSecret, CacheInterface $cache) : ClientInterface
40
    {
41
        //create our handler stack (which may be mocked in tests) and add the oauth and cache middleware
42
        $handlerStack = HandlerStack::create($this->apiHandler);
43
        $handlerStack->push($this->createOAuth2Middleware($apiRoot, $clientId, $clientSecret, $cache));
44
        $handlerStack->push($this->createCacheMiddleware($cache), 'cache');
45
46
        //now we can make our client
47
        $client = new GuzzleClient([
48
            'handler' => $handlerStack,
49
            'auth' => 'oauth',
50
            'base_uri' => $apiRoot
51
        ]);
52
53
        return $client;
54
    }
55
56
    protected function createOAuth2Middleware($apiRoot, $id, $secret, CacheInterface $cache): OAuth2Middleware
57
    {
58
        $handlerStack = HandlerStack::create($this->oauth2Handler);
59
60
        // Authorization client - this is used to request OAuth access tokens
61
        $reauth_client = new GuzzleClient([
62
            'handler' => $handlerStack,
63
            // URL for access_token request
64
            'base_uri' => $apiRoot . '/oauth/v2/token',
65
        ]);
66
        $reauth_config = [
67
            "client_id" => $id,
68
            "client_secret" => $secret
69
        ];
70
        $grant_type = new ClientCredentials($reauth_client, $reauth_config);
71
        $oauth = new OAuth2Middleware($grant_type);
72
73
        //use our cache to store tokens
74
        $oauth->setTokenPersistence(new SimpleCacheTokenPersistence($cache));
75
76
        return $oauth;
77
    }
78
79
    protected function createCacheMiddleware(CacheInterface $cache): CacheMiddleware
80
    {
81
        return new CacheMiddleware(
82
            new PrivateCacheStrategy(
83
                new Psr16CacheStorage($cache)
84
            )
85
        );
86
    }
87
}
88