AbstractRandomnessInjectable   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 12
c 1
b 0
f 0
dl 0
loc 68
ccs 12
cts 12
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A __destruct() 0 3 1
A seedRandomGenerator() 0 10 2
A __clone() 0 3 1
1
<?php
2
3
/**
4
 * Abstraction container for dependency injection of pseudo-random generator services.
5
 */
6
7
namespace CryptoManana\Core\Abstractions\Containers;
8
9
use CryptoManana\Core\Abstractions\Randomness\AbstractGenerator as RandomnessSource;
10
use CryptoManana\Core\Interfaces\Randomness\SeedableContainerInterface as SeedableService;
11
use CryptoManana\Core\Interfaces\Randomness\SeedableGeneratorInterface as SeedableGenerator;
12
use CryptoManana\Core\Interfaces\Containers\RandomnessInjectableInterface as SetterInjectable;
13
use CryptoManana\Core\Traits\Containers\RandomnessInjectableTrait as SetterInjectableImplementation;
14
use CryptoManana\Randomness\CryptoRandom as DefaultRandomnessSource;
15
16
/**
17
 * Class AbstractRandomnessInjectable - Abstraction container for dependency injection of data generator services.
18
 *
19
 * @package CryptoManana\Core\Abstractions\Containers
20
 *
21
 * @mixin SetterInjectableImplementation
22
 */
23
abstract class AbstractRandomnessInjectable implements SetterInjectable, SeedableService
24
{
25
    /**
26
     * Dependency injection via a setter method implementation.
27
     *
28
     * {@internal Reusable implementation of `RandomnessInjectableInterface`. }}
29
     */
30
    use SetterInjectableImplementation;
31
32
    /**
33
     * The pseudo-random generator service property storage.
34
     *
35
     * @var RandomnessSource|null The pseudo-random generator service.
36
     */
37
    protected $randomnessSource = null;
38
39
    /**
40
     * Container constructor.
41
     *
42
     * @param RandomnessSource|null $generator The pseudo-random generator service.
43
     *
44
     * @throws \Exception Initialization validation.
45
     *
46
     * @note The default service is always the most secure one available.
47
     */
48 77
    public function __construct(RandomnessSource $generator = null)
49
    {
50 77
        if ($generator === null) {
51 41
            $this->randomnessSource = new DefaultRandomnessSource();
52
        } else {
53 36
            $this->randomnessSource = $generator;
54
        }
55
    }
56
57
    /**
58
     * Container destructor.
59
     */
60 77
    public function __destruct()
61
    {
62 77
        unset($this->randomnessSource);
63
    }
64
65
    /**
66
     * Container cloning via deep copy.
67
     */
68 8
    public function __clone()
69
    {
70 8
        $this->randomnessSource = clone $this->randomnessSource;
71
    }
72
73
    /**
74
     * Pass a seed value to the pseudo-randomness generator service.
75
     *
76
     * @param null|int $seed Seed value in integer format or null for auto-seeding.
77
     *
78
     * @return $this The container object.
79
     * @throws \Exception Validation errors and service misuse.
80
     */
81 14
    public function seedRandomGenerator($seed = null)
82
    {
83 14
        if ($this->randomnessSource instanceof SeedableGenerator) {
84
            /**
85
             * {@internal Backward compatibility way of calling a static method (`::`) via dynamic operator (`->`). }}
86
             */
87 14
            $this->randomnessSource->setSeed($seed);
88
        }
89
90 14
        return $this;
91
    }
92
}
93