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

IdentifierGenerator::generateUniqueIdentifier()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 2
cts 2
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 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