Completed
Branch full-rewrite (4754d3)
by Thibaud
03:13
created

ProtocolBasedIteratorResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
wmc 5
lcom 1
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addFactory() 0 4 1
A getFactory() 0 9 2
A resolveIterator() 0 14 2
1
<?php
2
3
namespace Alchemy\Zippy\Package\IteratorResolver;
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
        $factory = $this->getFactory($protocol);
36
37
        $iterator = $factory($container);
38
39
        if (! $iterator) {
40
            throw new \RuntimeException('Unsupported protocol: ' . $protocol);
41
        }
42
43
        return $iterator;
44
45
    }
46
}
47