SimpleIterator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 30
c 3
b 0
f 0
dl 0
loc 81
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findCell() 0 5 2
B getChunk() 0 37 9
A __construct() 0 5 1
A findRow() 0 3 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Infrastructure Related Agent
5
 * @author Max Demian <[email protected]>
6
 */
7
8
namespace Ticaje\FileManager\Infrastructure\Driver\Iterator;
9
10
use Iterator as SuperIterator;
11
use Ticaje\FileManager\Infrastructure\Driver\Reader\Interfaces\FileInterface as FileManagerAgency;
12
use Ticaje\FileManager\Infrastructure\Driver\Reader\Interfaces\Gateway\FileGatewayInterface;
13
use ArrayIterator;
14
15
/**
16
 * Class SimpleIterator
17
 * @package Ticaje\FileManager\Infrastructure\Driver\Iterator
18
 * This is an API to provide high level interaction with iterators. It offers composition over inheritance approach, so
19
 * the iterator is being injected through constructor and the class offers the possibility of accessing useful features
20
 * to files especially on big ones
21
 */
22
class SimpleIterator implements SimpleIteratorInterface
23
{
24
    /** @var ArrayIterator $content */
25
    private $content;
26
27
    /** @var array $cachedRows */
28
    private $cachedRows = [];
29
30
    /** @var FileGatewayInterface $implementor */
31
    private $implementor;
32
33
    /**
34
     * SimpleIterator constructor.
35
     *
36
     * @param FileManagerAgency $fileManagerAgency
37
     */
38
    public function __construct(
39
        FileManagerAgency $fileManagerAgency
40
    ) {
41
        $this->implementor = $fileManagerAgency->getImplementor();
42
        $this->content = $fileManagerAgency->getContentCopy();
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function findCell(int $rowIndex, int $columnIndex)
49
    {
50
        $row = $this->findRow($rowIndex);
51
52
        return $row ? $row[$columnIndex] : null;
0 ignored issues
show
introduced by
$row is an empty array, thus is always false.
Loading history...
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function findRow(int $number): ?array
59
    {
60
        return $this->getChunk($number, $number);
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function getChunk(int $begin, int $end): ?array
67
    {
68
        $end ++;
69
        $result = [];
70
        // Returns none if no range at all
71
        if ($begin >= $end || $begin < 0) {
72
            return $result;
73
        }
74
        // Pick data from cache first of none
75
        if ($begin < $this->content->key()) {
76
            // Last row not inclusive
77
            $range = range($begin, $end - 1);
78
            array_walk($range, function ($i) use (&$result) {
79
                if (isset($this->cachedRows[$i])) {
80
                    $result[$i] = $this->cachedRows[$i];
81
                }
82
            });
83
        }
84
        // Go for the left un-cached data
85
        // Csv file with no data and just the header, key is null
86
        while (!is_null($this->content->key()) && ($this->content->key() < $end)) {
87
            $key = $this->content->key();
88
            // This almost unlikely
89
            if (isset($this->cachedRows[$key])) {
90
                $result[$key] = $this->cachedRows[$key];
91
                $this->content->next();
92
                continue;
93
            }
94
            $row = $this->content->current();
95
            if ($key >= $begin) {
96
                $result[$key] = $this->implementor->asArray($row);
97
            }
98
            // Always cache row
99
            $this->cachedRows[$key] = $this->implementor->asArray($row);
100
            $this->content->next();
101
        }
102
        return $result;
103
    }
104
}
105