Completed
Push — master ( 6e3bdf...de7f23 )
by Tobias
05:09
created

ManagementFactory::getTokenStruct()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 11.1032

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
ccs 9
cts 16
cp 0.5625
rs 6.7272
cc 7
eloc 15
nc 6
nop 0
crap 11.1032
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
        try {
108 1
            $token = $this->authentication->clientCredentials(
109
                [
110 1
                    'audience' => sprintf('https://%s/api/v2/', $this->domain),
111
                ]
112 1
            );
113 1
        } catch (\Auth0\SDK\Exception $e) {
0 ignored issues
show
Bug introduced by
The class Auth0\SDK\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
114
            throw $e;
115
        } catch (\Exception $e) {
116
            throw new CoreException('Could not get access token', 0, $e);
117
        }
118
119 1
        if (isset($token['error'])) {
120
            throw new CoreException($token['error_description']);
121
        }
122
123 1
        if (empty($token['access_token'])) {
124
            throw new CoreException('Could not get access token');
125
        }
126
127 1
        if ($this->logger) {
128
            $this->logger->debug(sprintf('Got new access token for Auth0 management API. Scope: "%s" ', isset($token['scope']) ? $token['scope'] : 'no scope'));
129
        }
130
131 1
        return $token;
132
    }
133
}
134