Completed
Push — master ( 724228...dcb94b )
by Tobias
08:49
created

ManagementFactory::getTokenStruct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.675

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
ccs 7
cts 10
cp 0.7
rs 8.8571
cc 5
eloc 10
nc 4
nop 0
crap 5.675
1
<?php
2
3
namespace Happyr\Auth0Bundle\Factory;
4
5
use Auth0\SDK\API\Authentication;
6
use Auth0\SDK\API\Management;
7
use Auth0\SDK\Exception\CoreException;
8
use Http\Client\HttpClient;
9
use Psr\Cache\CacheItemPoolInterface;
10
use Psr\Log\LoggerInterface;
11
12
class ManagementFactory
13
{
14
    /**
15
     * @var CacheItemPoolInterface|null
16
     */
17
    private $cacheItemPool;
18
19
    /**
20
     * @var Authentication
21
     */
22
    private $authentication;
23
24
    /**
25
     * @var string
26
     */
27
    private $domain;
28
29
    /**
30
     * @var HttpClient|null
31
     */
32
    private $httpClient;
33
34
    /**
35
     * @var LoggerInterface|null
36
     */
37
    private $logger;
38
39
    /**
40
     * ManagementFactory constructor.
41
     */
42 1
    public function __construct(
43
        Authentication $authentication,
44
        $domain,
45
        CacheItemPoolInterface $cacheItemPool = null,
46
        HttpClient $httpClient = null,
47
        LoggerInterface $logger = null
48
    ) {
49 1
        $this->cacheItemPool = $cacheItemPool;
50 1
        $this->authentication = $authentication;
51 1
        $this->domain = $domain;
52 1
        $this->httpClient = $httpClient;
53 1
        $this->logger = $logger;
54 1
    }
55
56
    /**
57
     * @return Management
58
     */
59 1
    public function create()
60
    {
61 1
        if ($this->cacheItemPool) {
62
            $accessToken = $this->getCachedAccessToken();
63
        } else {
64 1
            $accessToken = $this->getUncachedAccessToken();
65
        }
66
67 1
        return new Management($accessToken, $this->domain, $this->httpClient);
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    protected function getCachedAccessToken()
74
    {
75
        $item = $this->cacheItemPool->getItem('auth0_management_access_token');
76
77
        if (!$item->isHit()) {
78
            $token = $this->getTokenStruct();
79
80
            $item->set($token['access_token']);
81
            $item->expiresAfter((int) $token['expires_in']);
82
            $this->cacheItemPool->save($item);
83
        }
84
85
        return $item->get();
86
    }
87
88
    /**
89
     * @return string
90
     */
91 1
    protected function getUncachedAccessToken()
92
    {
93 1
        if ($this->logger) {
94
            $this->logger->warning('Using the Auth0 management API without using an access token cache. This increases the number of API requests.');
95
        }
96
97 1
        return $this->getTokenStruct()['access_token'];
98
    }
99
100
    /**
101
     * @return array
102
     *
103
     * @throws CoreException
104
     */
105 1
    protected function getTokenStruct()
106
    {
107 1
        $token = $this->authentication->clientCredentials([
108 1
            'audience' => sprintf('https://%s/api/v2/', $this->domain),
109 1
        ]);
110
111 1
        if (isset($token['error'])) {
112
            throw new CoreException($token['error_description']);
113
        }
114
115 1
        if (empty($token['access_token'])) {
116
            throw new CoreException('Could not get access token');
117
        }
118
119 1
        if ($this->logger) {
120
            $this->logger->debug(sprintf('Got new access token for Auth0 managment API. Scope: "%s" ', isset($token['scope']) ? $token['scope'] : 'no scope'));
121
        }
122
123
        return $token;
124
    }
125
}
126