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

ZipResourceIterator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 99
wmc 7
lcom 1
cbo 3
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A current() 0 11 1
A resolveReader() 0 4 1
1
<?php
2
3
namespace Alchemy\Zippy\Adapter\Pecl\Zip;
4
5
use Alchemy\Zippy\Package\PackagedResource;
6
use Alchemy\Zippy\Package\PackagedResourceIterator;
7
use Alchemy\Zippy\Resource\Reader\RawStreamReader;
8
use Alchemy\Zippy\Resource\ResourceReader;
9
use Alchemy\Zippy\Resource\ResourceReaderResolver;
10
use Alchemy\Zippy\Resource\ResourceUri;
11
12
class ZipResourceIterator implements PackagedResourceIterator, ResourceReaderResolver
13
{
14
15
    /**
16
     * @var \ZipArchive
17
     */
18
    private $archive;
19
20
    /**
21
     * @var int
22
     */
23
    private $current = 0;
24
25
    /**
26
     * @var PackagedResource
27
     */
28
    private $parent;
29
30
    /**
31
     * @param PackagedResource $parent
32
     */
33
    public function __construct(PackagedResource $parent) {
34
        $this->parent = $parent;
35
36
        $this->archive = new \ZipArchive();
37
        $this->archive->open($this->parent->getAbsoluteUri()->getResource());
38
    }
39
40
    /**
41
     * (PHP 5 &gt;= 5.0.0)<br/>
42
     * Move forward to next element
43
     * @link http://php.net/manual/en/iterator.next.php
44
     * @return void Any returned value is ignored.
45
     */
46
    public function next()
47
    {
48
        $this->current++;
49
    }
50
51
    /**
52
     * (PHP 5 &gt;= 5.0.0)<br/>
53
     * Return the key of the current element
54
     * @link http://php.net/manual/en/iterator.key.php
55
     * @return mixed scalar on success, or null on failure.
56
     */
57
    public function key()
58
    {
59
        return $this->archive->getNameIndex($this->current);
60
    }
61
62
    /**
63
     * (PHP 5 &gt;= 5.0.0)<br/>
64
     * Checks if current position is valid
65
     * @link http://php.net/manual/en/iterator.valid.php
66
     * @return boolean The return value will be casted to boolean and then evaluated.
67
     * Returns true on success or false on failure.
68
     */
69
    public function valid()
70
    {
71
        return $this->current < $this->archive->numFiles;
72
    }
73
74
    /**
75
     * (PHP 5 &gt;= 5.0.0)<br/>
76
     * Rewind the Iterator to the first element
77
     * @link http://php.net/manual/en/iterator.rewind.php
78
     * @return void Any returned value is ignored.
79
     */
80
    public function rewind()
81
    {
82
        $this->current = 0;
83
    }
84
85
    /**
86
     * @return PackagedResource
87
     */
88
    public function current()
89
    {
90
        $file = $this->key();
91
92
        return new PackagedResource(
93
            ResourceUri::fromString($file),
94
            $this,
95
            $this->parent->getWriterResolver(),
96
            $this->parent
97
        );
98
    }
99
100
    /**
101
     * Resolves a reader for the given resource URI.
102
     *
103
     * @param ResourceUri $resource
104
     * @return ResourceReader
105
     */
106
    public function resolveReader(ResourceUri $resource)
107
    {
108
        return new RawStreamReader($this->archive->getStream($resource->getResource()));
109
    }
110
}
111