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

RarResourceIterator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A buildIterator() 6 6 1
A current() 11 11 1
A resolveReader() 4 4 1

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\Adapter\Pecl\Rar;
4
5
use Alchemy\Zippy\MappingArrayIterator;
6
use Alchemy\Zippy\Package\Iterator\AbstractIterator;
7
use Alchemy\Zippy\Package\PackagedResource;
8
use Alchemy\Zippy\Resource\Reader\RawStreamReader;
9
use Alchemy\Zippy\Resource\ResourceReader;
10
use Alchemy\Zippy\Resource\ResourceReaderResolver;
11
use Alchemy\Zippy\Resource\ResourceUri;
12
13 View Code Duplication
class RarResourceIterator extends AbstractIterator implements ResourceReaderResolver
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    /**
16
     * @var \RarArchive
17
     */
18
    private $archive;
19
20
    /**
21
     * @var PackagedResource
22
     */
23
    private $container;
24
25
    /**
26
     * @param PackagedResource $container
27
     */
28
    public function __construct(PackagedResource $container)
29
    {
30
        $this->container = $container;
31
        $this->archive = \RarArchive::open($container->getAbsoluteUri()->getResource());
32
    }
33
34
    /**
35
     * @return \Iterator
36
     */
37
    protected  function buildIterator()
38
    {
39
        return new MappingArrayIterator($this->archive->getEntries(), function ($current) {
40
            return ResourceUri::fromString($current->getName());
41
        });
42
    }
43
44
    /**
45
     * @return PackagedResource
46
     */
47
    public function current()
48
    {
49
        $resource = $this->getIterator()->current();
50
51
        return new PackagedResource(
52
            $resource,
53
            $this,
54
            $this->container->getWriterResolver(),
55
            $this->container
56
        );
57
    }
58
59
    /**
60
     * Resolves a reader for the given resource URI.
61
     *
62
     * @param ResourceUri $resource
63
     * @return ResourceReader
64
     */
65
    public function resolveReader(ResourceUri $resource)
66
    {
67
        return new RawStreamReader($this->archive->getEntry($resource->getResource())->getStream());
68
    }
69
}
70