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

ProtocolBasedIteratorResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addFactory() 0 4 1
A getFactory() 0 9 2
A resolveIterator() 0 12 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