Passed
Push — master ( 89c1c1...48858c )
by Jesse
07:23
created

PredefinedInstanceProvider::use()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Instantiator;
5
6
use function get_class;
7
8
/**
9
 * Provider for predefined instances.
10
 *
11
 * @author Stratadox
12
 */
13
final class PredefinedInstanceProvider implements Instantiator
14
{
15
    private $objects;
16
    private $offset;
17
18
    private function __construct(...$objects)
19
    {
20
        $this->objects = $objects;
21
        $this->offset = 0;
22
    }
23
24
    /**
25
     * Produces a new provider for predefined instances.
26
     *
27
     * @param mixed ...$objects           The objects to provide.
28
     * @return PredefinedInstanceProvider The object provider.
29
     */
30
    public static function use(...$objects): self
31
    {
32
        return new self(...$objects);
33
    }
34
35
    /** @inheritdoc */
36
    public function instance(): object
37
    {
38
        if (!isset($this->objects[$this->offset])) {
39
            throw NoMoreInstances::listRanOutAt($this->offset);
40
        }
41
        return $this->objects[$this->offset++];
42
    }
43
44
    /** @inheritdoc */
45
    public function class(): string
46
    {
47
        if (!isset($this->objects[$this->offset])) {
48
            return '';
49
        }
50
        return get_class($this->objects[$this->offset]);
51
    }
52
}
53