RandomOrg   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 60
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isSupported() 0 3 1
A __construct() 0 3 1
A generate() 0 19 1
A getStrength() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\Yaroc\Plugin\RandomLib\Source;
6
7
use drupol\Yaroc\Plugin\Provider;
8
use drupol\Yaroc\RandomOrgAPI;
9
use drupol\Yaroc\RandomOrgAPIInterface;
10
use RandomLib\AbstractSource;
11
use SecurityLib\Strength;
12
13
/**
14
 * The Random.Org Source.
15
 *
16
 * @category   PHPCryptLib
17
 *
18
 * @author     Pol Dellaiera <[email protected]>
19
 *
20
 * @codeCoverageIgnore
21
 */
22
final class RandomOrg extends AbstractSource
23
{
24
    /**
25
     * The Random.Org API.
26
     */
27
    protected RandomOrgAPIInterface $randomOrgAPI;
28
29
    public function __construct(RandomOrgAPIInterface $randomOrgAPI)
30
    {
31
        $this->randomOrgAPI = $randomOrgAPI;
32
    }
33
34
    /**
35
     * Generate a random string of the specified size.
36
     *
37
     * @param int $size
38
     *   The size of the requested random string
39
     *
40
     * @return string
41
     *   A string of the requested size
42
     */
43
    public function generate($size): string
44
    {
45
        $provider = (new Provider())->withResource('generateStrings')
46
            ->withParameters([
47
                'n' => 1,
48
                'length' => $size,
49
                'characters' => implode(
50
                    '',
51
                    array_merge(
52
                        range('A', 'Z'),
53
                        range('a', 'z'),
54
                        range(0, 9)
55
                    )
56
                ),
57
            ]);
58
59
        $result = $this->randomOrgAPI->getData($provider);
60
61
        return $result[0];
62
    }
63
64
    /**
65
     * Return an instance of Strength indicating the strength of the source.
66
     *
67
     * @return \SecurityLib\Strength
68
     *   An instance of one of the strength classes
69
     */
70
    public static function getStrength(): Strength
71
    {
72
        return new Strength(Strength::HIGH);
73
    }
74
75
    /**
76
     * If the source is currently available.
77
     * Reasons might be because the library is not installed.
78
     */
79
    public static function isSupported(): bool
80
    {
81
        return class_exists(RandomOrgAPI::class);
82
    }
83
}
84