Completed
Push — master ( c7c89b...c6d188 )
by Tony Karavasilev (Тони
09:30 queued 04:11
created

RandomnessFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 63
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A __debugInfo() 0 6 1
A createInstance() 0 14 3
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
     * Create a pseudo-randomness generator.
39
     *
40
     * @param string|null $type The generator class name as type for creation.
41
     *
42
     * @return RandomnessSource|object|null An exception object or null.
43
     */
44 3
    public function create($type)
45
    {
46 3
        return self::createInstance($type);
47
    }
48
49
    /**
50
     * Create a pseudo-randomness generator.
51
     *
52
     * @param string|null $type The generator class name as type for creation.
53
     *
54
     * @return RandomnessSource|object|null An exception object or null.
55
     */
56 4
    public static function createInstance($type)
57
    {
58
        /**
59
         * Check if class exists and has a correct base class
60
         *
61
         * @var RandomnessSource|null $exception Object instance.
62
         */
63 4
        if (class_exists($type) && is_subclass_of($type, RandomnessSource::class)) {
64 4
            $exception = new $type();
65
        } else {
66 2
            $exception = null; // Invalid exception type given
67
        }
68
69 4
        return $exception;
70
    }
71
72
    /**
73
     * Get debug information for the class instance.
74
     *
75
     * @return array Debug information.
76
     */
77 1
    public function __debugInfo()
78
    {
79
        return [
80 1
            self::class . '::QUASI_SOURCE' => self::QUASI_SOURCE,
81 1
            self::class . '::PSEUDO_SOURCE' => self::PSEUDO_SOURCE,
82 1
            self::class . '::CRYPTO_SOURCE' => self::CRYPTO_SOURCE,
83
        ];
84
    }
85
}
86