Completed
Push — master ( 88b9f7...c044d2 )
by Pol
15:00
created

src/Plugin/RandomLib/Source/RandomOrg.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\RandomOrgAPIInterface;
9
use SecurityLib\Strength;
10
11
/**
12
 * The Random.Org Source.
13
 *
14
 * @category   PHPCryptLib
15
 *
16
 * @author     Pol Dellaiera <[email protected]>
17
 *
18
 * @codeCoverageIgnore
19
 */
20 View Code Duplication
class RandomOrg extends \RandomLib\AbstractSource
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
{
22
    /**
23
     * The Random.Org API.
24
     *
25
     * @var RandomOrgAPIInterface
26
     */
27
    protected $randomOrgAPI;
28
29
    /**
30
     * RandomOrg constructor.
31
     *
32
     * @param \drupol\Yaroc\RandomOrgAPIInterface $randomOrgAPI
33
     */
34
    public function __construct(RandomOrgAPIInterface $randomOrgAPI)
35
    {
36
        $this->randomOrgAPI = $randomOrgAPI;
37
    }
38
39
    /**
40
     * Generate a random string of the specified size.
41
     *
42
     * @param int $size
43
     *   The size of the requested random string
44
     *
45
     * @return string
46
     *   A string of the requested size
47
     */
48
    public function generate($size): string
49
    {
50
        $provider = Provider::withResource('generateStrings')
51
            ->withParameters([
52
                'n' => 1,
53
                'length' => $size,
54
                'characters' => \implode('', \array_merge(\range('A', 'Z'), \range('a', 'z'), \range(0, 9))),
55
            ]);
56
57
        $result = $this->randomOrgAPI->getData($provider);
58
59
        return $result[0];
60
    }
61
62
    /**
63
     * Return an instance of Strength indicating the strength of the source.
64
     *
65
     * @return \SecurityLib\Strength
66
     *   An instance of one of the strength classes
67
     */
68
    public static function getStrength(): Strength
69
    {
70
        return new Strength(Strength::HIGH);
71
    }
72
73
    /**
74
     * If the source is currently available.
75
     * Reasons might be because the library is not installed.
76
     *
77
     * @return bool
78
     */
79
    public static function isSupported(): bool
80
    {
81
        return \class_exists('RandomOrgAPI');
82
    }
83
}
84