Completed
Push — master ( ebd7bb...65be67 )
by Tony Karavasilev (Тони
04:40
created

IdentifierOutputTrait::getGloballyUniqueId()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 10
cc 3
nc 4
nop 3
crap 3
1
<?php
2
3
/**
4
 * Trait implementation of unique string identifier format generation for generator services.
5
 */
6
7
namespace CryptoManana\Core\Traits\Randomness;
8
9
use \CryptoManana\Core\StringBuilder as StringBuilder;
10
11
/**
12
 * Trait IdentifierOutputTrait - Reusable implementation of `IdentifierOutputInterface`.
13
 *
14
 * @see \CryptoManana\Core\Interfaces\Randomness\IdentifierOutputInterface The abstract specification.
15
 *
16
 * @package CryptoManana\Core\Traits\Randomness
17
 */
18
trait IdentifierOutputTrait
19
{
20
    /**
21
     * Generate a random version 4 Globally Unique Identifier (GUID) standard string.
22
     *
23
     * Note: The identifier string uses 32 alphanumeric characters and 4 hyphens (optional).
24
     *
25
     * @param string $prefix Optional prefix for output strings (default => '').
26
     * @param bool $withDashes Flag for using dashes format (default => true).
27
     * @param bool $upperCase Flag for using uppercase format (default => false).
28
     *
29
     * @return string Randomly generated GUID string representing a 128-bit number.
30
     * @throws \Exception Validation errors.
31
     */
32 3
    public function getGloballyUniqueId($prefix = '', $withDashes = true, $upperCase = false)
33
    {
34 3
        $tmp = $this->getBytes(16);
0 ignored issues
show
Bug introduced by
It seems like getBytes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        /** @scrutinizer ignore-call */ 
35
        $tmp = $this->getBytes(16);
Loading history...
35
36 3
        $tmp[6] = StringBuilder::getChr(StringBuilder::getOrd($tmp[6]) & 0x0f | 0x40);
37 3
        $tmp[8] = StringBuilder::getChr(StringBuilder::getOrd($tmp[8]) & 0x3f | 0x80);
38
39 3
        $id = vsprintf('%s%s-%s-%s-%s-%s%s%s', StringBuilder::stringSplit(bin2hex($tmp), 4));
40
41 3
        $id = ($withDashes) ? $id : StringBuilder::stringReplace('-', '', $id);
42
43 3
        $id = ($upperCase) ? StringBuilder::stringToUpper($id) : $id;
44
45 3
        return StringBuilder::stringFullTrimming($prefix) . $id;
46
    }
47
48
    /**
49
     * Generate a strong Universally Unique Identifier (UUID) string in hexadecimal or alphanumeric format.
50
     *
51
     * Note: The identifier string is exactly 128 characters long.
52
     *
53
     * @param string $prefix Optional prefix for output strings (default => '').
54
     * @param bool $alphaNumeric Flag for switching to alphanumerical format (default => false).
55
     *
56
     * @return string Randomly generated strong hexadecimal/alphanumerical UUID string.
57
     * @throws \Exception Validation errors.
58
     */
59 3
    public function getStrongUniqueId($prefix = '', $alphaNumeric = false)
60
    {
61 3
        if ($alphaNumeric) {
62 3
            $id = $this->getAlphaNumeric(128);
1 ignored issue
show
Bug introduced by
It seems like getAlphaNumeric() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            /** @scrutinizer ignore-call */ 
63
            $id = $this->getAlphaNumeric(128);
Loading history...
63
64 3
            $id = $this->getBool() ? StringBuilder::stringReverse($id) : $id;
1 ignored issue
show
Bug introduced by
It seems like getBool() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
            $id = $this->/** @scrutinizer ignore-call */ getBool() ? StringBuilder::stringReverse($id) : $id;
Loading history...
65
        } else {
66 3
            $id = hash_hmac(
67 3
                'sha512', // exactly 128 chars output (1024-bit)
68 3
                $this->getBytes(64), // 512-bit input
69 3
                $this->getBytes(64)  // 512-bit key
70
            );
71
72 3
            $id = StringBuilder::stringToUpper($id);
73
        }
74
75 3
        return StringBuilder::stringFullTrimming($prefix) . $id;
76
    }
77
}
78