ProtocolBasedWriterResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 30.43 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 14
loc 46
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addWriter() 11 11 3
A resolveWriter() 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
/*
4
 * This file is part of alchemy/resource-component.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Alchemy\Resource\Resolver;
13
14
use Alchemy\Resource\ResourceUri;
15
use Alchemy\Resource\ResourceWriter;
16
use Alchemy\Resource\ResourceWriterResolver;
17
18
class ProtocolBasedWriterResolver implements ResourceWriterResolver
19
{
20
    /**
21
     * @var ResourceWriter[]
22
     */
23
    private $writers = [];
24
25
    /**
26
     * @var int[] Dictionary of writer indexes, indexed by resource protocol name
27
     */
28
    private $protocolWriterIndexes = [];
29
30
    /**
31
     * @param ResourceWriter $writer
32
     * @param string|string[] $protocols List of compatible protocols
33
     */
34 4 View Code Duplication
    public function addWriter(ResourceWriter $writer, $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...
35
    {
36 4
        $protocols = is_array($protocols) ? $protocols : [ $protocols ];
37 4
        $index = count($this->writers);
38
39 4
        $this->writers[$index] = $writer;
40
41 4
        foreach ($protocols as $protocol) {
42 4
            $this->protocolWriterIndexes[$protocol] = $index;
43 3
        }
44 4
    }
45
46
    /**
47
     * Resolves a writer for the given resource URI.
48
     *
49
     * @param ResourceUri $resource
50
     * @return ResourceWriter
51
     */
52 8
    public function resolveWriter(ResourceUri $resource)
53
    {
54 8 View Code Duplication
        if (! array_key_exists($resource->getProtocol(), $this->protocolWriterIndexes)) {
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...
55 4
            throw new \RuntimeException('Unsupported protocol: ' . $resource->getProtocol() . '( ' . $resource  . ')');
56
        }
57
58
        /** @var int $index */
59 4
        $index = $this->protocolWriterIndexes[$resource->getProtocol()];
60
61 4
        return $this->writers[$index];
62
    }
63
}
64