StreamRegistrar::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Mpyw\StreamInterfaceResource\Registry;
4
5
use Mpyw\StreamInterfaceResource\Unserializable;
6
use Psr\Http\Message\StreamInterface;
7
8
class StreamRegistrar implements StreamRegistrarInterface
9
{
10
    use Unserializable;
11
12
    /**
13
     * @var static
14
     */
15
    protected static $instance;
16
17
    /**
18
     * @var PathGeneratorInterface
19
     */
20
    protected $pathGenerator;
21
22
    /**
23
     * @var \SplObjectStorage|string[]
24
     */
25
    protected $streamToPath;
26
27
    /**
28
     * @var StreamInterface[]
29
     */
30
    protected $pathToStream = [];
31
32
    /**
33
     * @return static
34
     */
35 15
    public static function getInstance()
36
    {
37 15
        if (!static::$instance) {
38 1
            StreamWrapperRegistrar::registerFor(static::$instance = new static());
39
        }
40
41 15
        return static::$instance;
42
    }
43
44 1
    public function __construct(?PathGeneratorInterface $pathGenerator = null)
45
    {
46 1
        $this->pathGenerator = $pathGenerator ?? new PathGenerator($this);
47 1
        $this->streamToPath = new \SplObjectStorage();
48
    }
49
50 15
    public function register(StreamInterface $stream): void
51
    {
52 15
        if (!$this->streamToPath->contains($stream)) {
53 15
            $path = $this->pathGenerator->generatePathFor($stream);
54
55 15
            $this->streamToPath->attach($stream, $path);
56 15
            $this->pathToStream[$path] = $stream;
57
        }
58
    }
59
60 15
    public function remove(StreamInterface $stream): void
61
    {
62 15
        if ($this->streamToPath->contains($stream)) {
63 15
            $path = $this->pathGenerator->generatePathFor($stream);
64
65 15
            $this->streamToPath->detach($stream);
66 15
            unset($this->pathToStream[$path]);
67
        }
68
    }
69
70 15
    public function pathFor(StreamInterface $stream): string
71
    {
72 15
        if (!$this->streamToPath->contains($stream)) {
73
            // @codeCoverageIgnoreStart
74
            throw new \OutOfBoundsException();
75
            // @codeCoverageIgnoreEnd
76
        }
77
78 15
        return $this->streamToPath[$stream];
79
    }
80
81 15
    public function streamFor(string $path): StreamInterface
82
    {
83 15
        if (!isset($this->pathToStream[$path])) {
84
            // @codeCoverageIgnoreStart
85
            throw new \OutOfBoundsException();
86
            // @codeCoverageIgnoreEnd
87
        }
88
89 15
        return $this->pathToStream[$path];
90
    }
91
92
    public function __debugInfo(): array
93
    {
94
        $map = [];
95
96
        foreach ($this->pathToStream as $path => $stream) {
97
            $key = \sprintf('object(%s)#%s', \get_class($stream), \spl_object_id($stream));
98
            $map[$key] = $this->pathGenerator->generatePathFor($stream);
99
        }
100
101
        return [
102
            '*map*' => $map,
103
        ];
104
    }
105
}
106