RandomnessFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 2
b 0
f 0
dl 0
loc 64
ccs 13
cts 13
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A createInstance() 0 14 3
A __debugInfo() 0 6 1
1
<?php
2
3
/**
4
 * Factory object for pseudo-randomness generator instancing.
5
 */
6
7
namespace CryptoManana\Factories;
8
9
use CryptoManana\Core\Abstractions\DesignPatterns\AbstractFactory as FactoryPattern;
10
use CryptoManana\Core\Abstractions\Randomness\AbstractRandomness as RandomnessSource;
11
use CryptoManana\Randomness\QuasiRandom as QuasiRandomness;
12
use CryptoManana\Randomness\PseudoRandom as PseudoRandomness;
13
use CryptoManana\Randomness\CryptoRandom as CryptographyRandomness;
14
15
/**
16
 * Class RandomnessFactory - Factory object for pseudo-randomness generator instancing.
17
 *
18
 * @package CryptoManana\Factories
19
 */
20
class RandomnessFactory extends FactoryPattern
21
{
22
    /**
23
     * The quasi-random generator type.
24
     */
25
    const QUASI_SOURCE = QuasiRandomness::class;
26
27
    /**
28
     * The pseudo-random generator type.
29
     */
30
    const PSEUDO_SOURCE = PseudoRandomness::class;
31
32
    /**
33
     * The cryptographically secure pseudo-random generator type.
34
     */
35
    const CRYPTO_SOURCE = CryptographyRandomness::class;
36
37
    /**
38
     * Get debug information for the class instance.
39
     *
40
     * @return array Debug information.
41
     */
42 1
    public function __debugInfo()
43
    {
44 1
        return [
45 1
            self::class . '::QUASI_SOURCE' => self::QUASI_SOURCE,
46 1
            self::class . '::PSEUDO_SOURCE' => self::PSEUDO_SOURCE,
47 1
            self::class . '::CRYPTO_SOURCE' => self::CRYPTO_SOURCE,
48 1
        ];
49
    }
50
51
    /**
52
     * Create a pseudo-randomness generator.
53
     *
54
     * @param string|null $type The generator class name as type for creation.
55
     *
56
     * @return RandomnessSource|object|null A generator object or null.
57
     */
58 3
    public function create($type)
59
    {
60 3
        return self::createInstance($type);
61
    }
62
63
    /**
64
     * Create a pseudo-randomness generator.
65
     *
66
     * @param string|null $type The generator class name as type for creation.
67
     *
68
     * @return RandomnessSource|object|null A generator object or null.
69
     */
70 4
    public static function createInstance($type)
71
    {
72
        /**
73
         * Check if class exists and has a correct base class
74
         *
75
         * @var RandomnessSource|null $exception Object instance.
76
         */
77 4
        if (class_exists($type) && is_subclass_of($type, RandomnessSource::class)) {
78 4
            $exception = new $type();
79
        } else {
80 2
            $exception = null; // Invalid type given
81
        }
82
83 4
        return $exception;
84
    }
85
}
86