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

ProtocolBasedReaderResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 30.43 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addFactory() 11 11 3
A resolveReader() 3 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Alchemy\Zippy\Resource\ReaderResolver;
4
5
use Alchemy\Zippy\Resource\ResourceReader;
6
use Alchemy\Zippy\Resource\ResourceReaderFactory;
7
use Alchemy\Zippy\Resource\ResourceReaderResolver;
8
use Alchemy\Zippy\Resource\ResourceUri;
9
10
class ProtocolBasedReaderResolver implements ResourceReaderResolver
11
{
12
    /**
13
     * @var ResourceReaderFactory[]
14
     */
15
    private $factories = [];
16
17
    /**
18
     * @var int[] Dictionary of factory indexes, indexed by resource protocol name
19
     */
20
    private $protocolFactoryIndexes = [];
21
22
    /**
23
     * @param ResourceReaderFactory $factory
24
     * @param string|string[] $protocols List of compatible protocols
25
     */
26 View Code Duplication
    public function addFactory(ResourceReaderFactory $factory, $protocols)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        $protocols = is_array($protocols) ? $protocols : [ $protocols ];
29
        $index = count($this->factories);
30
31
        $this->factories[$index] = $factory;
32
33
        foreach ($protocols as $protocol) {
34
            $this->protocolFactoryIndexes[$protocol] = $index;
35
        }
36
    }
37
38
    /**
39
     * Resolves a reader for the given resource URI.
40
     *
41
     * @param ResourceUri $resource
42
     * @return ResourceReader
43
     */
44
    public function resolveReader(ResourceUri $resource)
45
    {
46 View Code Duplication
        if (! array_key_exists($resource->getProtocol(), $this->protocolFactoryIndexes)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
            throw new \RuntimeException('Unsupported protocol: ' . $resource->getProtocol() . '( ' . $resource  . ')');
48
        }
49
50
        $index = $this->protocolFactoryIndexes[$resource->getProtocol()];
51
        $factory = $this->factories[$index];
52
53
        return $factory->getReader($resource);
54
    }
55
}
56