Completed
Branch full-rewrite (3b3ffb)
by Thibaud
03:12
created

ProtocolBasedIteratorResolver::addFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Alchemy\Zippy\Package\Resolver;
4
5
use Alchemy\Zippy\Package\PackagedResource;
6
use Alchemy\Zippy\Package\PackagedResourceIteratorResolver;
7
8
class ProtocolBasedIteratorResolver implements PackagedResourceIteratorResolver
9
{
10
11
    private $factories = [];
12
13
    /**
14
     * @param string $protocol
15
     * @param callable $factory
16
     */
17
    public function addFactory($protocol, callable $factory)
18
    {
19
        $this->factories[$protocol] = $factory;
20
    }
21
22
    public function getFactory($protocol)
23
    {
24
        if (! isset($this->factories[$protocol])) {
25
            throw new \RuntimeException('Unsupported protocol: ' . $protocol);
26
        }
27
28
        return $this->factories[$protocol];
29
30
    }
31
32
    public function resolveIterator(PackagedResource $container)
33
    {
34
        $protocol = $container->getAbsoluteUri()->getProtocol();
35
        $iterator = call_user_func($this->getFactory($protocol), $container);
36
37
        if (! $iterator) {
38
            throw new \RuntimeException('Unsupported protocol: ' . $protocol);
39
        }
40
41
        return $iterator;
42
43
    }
44
}
45