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

TarResourceIterator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
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 current() 11 11 1
A buildIterator() 6 6 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\Pear\Tar;
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\StringReader;
9
use Alchemy\Zippy\Resource\ResourceReader;
10
use Alchemy\Zippy\Resource\ResourceReaderResolver;
11
use Alchemy\Zippy\Resource\ResourceUri;
12
13 View Code Duplication
class TarResourceIterator 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 \Archive_Tar
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 = new \Archive_Tar($container->getRelativeUri()->getResource());
32
    }
33
34
    /**
35
     * @return PackagedResource
36
     */
37
    public function current()
38
    {
39
        $resource = $this->getIterator()->current();
40
41
        return new PackagedResource(
42
            $resource,
43
            $this,
44
            $this->container->getWriterResolver(),
45
            $this->container
46
        );
47
    }
48
49
    /**
50
     * @return \ArrayIterator
51
     */
52
    protected function buildIterator()
53
    {
54
        return new MappingArrayIterator($this->archive->listContent(), function ($current) {
55
            return ResourceUri::fromString($current['filename']);
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 StringReader($this->archive->extractInString($resource->getResource()));
68
    }
69
}
70