Completed
Pull Request — master (#987)
by
unknown
04:24
created

IdentifierGenerator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 21
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A generateUniqueIdentifier() 0 15 4
1
<?php declare(strict_types=1);
2
3
namespace League\OAuth2\Server\IdentifierGenerator;
4
5
use Error;
6
use Exception;
7
use League\OAuth2\Server\Exception\OAuthServerException;
8
use League\OAuth2\Server\IdentifierGenerator\IdentifierGeneratorInterface;
9
use TypeError;
10
11
class IdentifierGenerator implements IdentifierGeneratorInterface
12
{
13
    /**
14
     * @inheritdoc
15
     */
16 2
    public function generateUniqueIdentifier($length = 40)
17
    {
18
        try {
19 2
            return bin2hex(random_bytes($length));
20
            // @codeCoverageIgnoreStart
21
        } catch (TypeError $e) {
22
            throw OAuthServerException::serverError('An unexpected error has occurred', $e);
23
        } catch (Error $e) {
24
            throw OAuthServerException::serverError('An unexpected error has occurred', $e);
25
        } catch (Exception $e) {
26
            // If you get this message, the CSPRNG failed hard.
27
            throw OAuthServerException::serverError('Could not generate a random string', $e);
28
        }
29
        // @codeCoverageIgnoreEnd
30
    }
31
}
32