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

RarResourceIterator::buildIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 6
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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