RandomOrg   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getPriority() 0 3 1
A isSupported() 0 3 1
A generate() 0 19 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\Yaroc\Plugin\RychRandom\Generator;
6
7
use drupol\Yaroc\Plugin\Provider;
8
use drupol\Yaroc\RandomOrgAPI;
9
use drupol\Yaroc\RandomOrgAPIInterface;
10
use Rych\Random\Generator\GeneratorInterface;
11
12
/**
13
 * The Random.Org Generator.
14
 *
15
 * @codeCoverageIgnore
16
 */
17
final class RandomOrg implements GeneratorInterface
18
{
19
    /**
20
     * The Random.Org API.
21
     */
22
    protected RandomOrgAPIInterface $randomOrgAPI;
23
24
    public function __construct(RandomOrgAPIInterface $randomOrgAPI)
25
    {
26
        $this->randomOrgAPI = $randomOrgAPI;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function generate($size): string
33
    {
34
        $provider = (new Provider())->withResource('generateStrings')
35
            ->withParameters([
36
                'n' => 1,
37
                'length' => $size,
38
                'characters' => implode(
39
                    '',
40
                    array_merge(
41
                        range('A', 'Z'),
42
                        range('a', 'z'),
43
                        range(0, 9)
44
                    )
45
                ),
46
            ]);
47
48
        $result = $this->randomOrgAPI->getData($provider);
49
50
        return $result[0];
51
    }
52
53
    public static function getPriority(): int
54
    {
55
        return GeneratorInterface::PRIORITY_HIGH;
56
    }
57
58
    public static function isSupported(): bool
59
    {
60
        return class_exists(RandomOrgAPI::class);
61
    }
62
}
63