Passed
Push — master ( edaa30...f05285 )
by Tony Karavasilev (Тони
04:46
created

AbstractRandomnessInjectable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

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 setter method.
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
     * @internal The default service is always the most secure one available.
43
     *
44
     * @param RandomnessSource|null $generator The pseudo-random generator service.
45
     *
46
     * @throws \Exception Initialization validation.
47
     */
48
    public function __construct(RandomnessSource $generator = null)
49
    {
50
        if (is_null($generator)) {
51
            $this->randomnessSource = new DefaultRandomnessSource();
52
        } else {
53
            $this->randomnessSource = $generator;
54
        }
55
    }
56
57
    /**
58
     * Container destructor.
59
     */
60
    public function __destruct()
61
    {
62
        unset($this->randomnessSource);
63
    }
64
65
    /**
66
     * Container cloning via deep copy.
67
     */
68
    public function __clone()
69
    {
70
        $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
    public function seedRandomGenerator($seed = null)
82
    {
83
        if ($this->randomnessSource instanceof SeedableGenerator) {
84
            /**
85
             * {@internal Back-ward comparability way of calling a static method (`::`) via dynamic operator (`->`). }
86
             */
87
            $this->randomnessSource->setSeed($seed);
88
        }
89
90
        return $this;
91
    }
92
}
93