Passed
Branch master (d51fdb)
by Joao
05:45 queued 02:33
created

FixedTextFileIterator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 68.08%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 134
ccs 32
cts 47
cp 0.6808
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A count() 0 4 1
A hasNext() 0 14 3
B moveNext() 0 24 5
B processBuffer() 0 32 5
A key() 0 4 1
1
<?php
2
3
namespace ByJG\AnyDataset\Dataset;
4
5
use ByJG\AnyDataset\Enum\FixedTextDefinition;
6
use ByJG\AnyDataset\Exception\IteratorException;
7
8
class FixedTextFileIterator extends GenericIterator
9
{
10
11
    /**
12
     *
13
     * @var FixedTextDefinition[]
14
     */
15
    protected $fields;
16
17
    /**
18
     * @var resource
19
     */
20
    protected $handle;
21
22
    /**
23
     * @var int
24
     */
25
    protected $current = 0;
26
27
    /**
28
     *
29
     * @param resource $handle
30
     * @param FixedTextDefinition[] $fields
31
     */
32 2
    public function __construct($handle, $fields)
33
    {
34 2
        $this->fields = $fields;
35 2
        $this->handle = $handle;
36 2
        $this->current = 0;
37 2
    }
38
39
    /**
40
     * @access public
41
     * @return int
42
     */
43
    public function count()
44
    {
45
        return -1;
46
    }
47
48
    /**
49
     * @access public
50
     * @return bool
51
     */
52 2
    public function hasNext()
53
    {
54 2
        if (!$this->handle) {
55
            return false;
56
        }
57
58 2
        if (feof($this->handle)) {
59 2
            fclose($this->handle);
60
61 2
            return false;
62
        }
63
64 2
        return true;
65
    }
66
67
68
    /**
69
     * @return Row|null
70
     * @throws \ByJG\AnyDataset\Exception\IteratorException
71
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
72
     */
73 2
    public function moveNext()
74
    {
75 2
        if ($this->hasNext()) {
76 2
            $buffer = fgets($this->handle, 4096);
77
78 2
            if ($buffer == "") {
79
                return new Row();
80
            }
81
82 2
            $fields = $this->processBuffer($buffer, $this->fields);
83
84 2
            if (is_null($fields)) {
85
                throw new IteratorException("Definition does not match");
86
            }
87
88 2
            $this->current++;
89 2
            return new Row($fields);
90
        }
91
92
        if ($this->handle) {
93
            fclose($this->handle);
94
        }
95
        return null;
96
    }
97
98
    /**
99
     * @param $buffer
100
     * @param $fieldDefinition
101
     * @return array
102
     * @throws \ByJG\AnyDataset\Exception\IteratorException
103
     */
104 2
    protected function processBuffer($buffer, $fieldDefinition)
105
    {
106 2
        $cntDef = count($fieldDefinition);
107 2
        $fields = [];
108 2
        for ($i = 0; $i < $cntDef; $i++) {
109 2
            $fieldDef = $fieldDefinition[$i];
110
111 2
            $fields[$fieldDef->fieldName] = substr($buffer, $fieldDef->startPos, $fieldDef->length);
112 2
            if (!empty($fieldDef->requiredValue)
113
                && (
114 2
                    !preg_match("/^[" . $fieldDef->requiredValue . "]$/", $fields[$fieldDef->fieldName])
115
                )
116
            ) {
117
                throw new IteratorException(
118
                    "Expected the value '"
119
                    . $fieldDef->requiredValue
120
                    . "' and I got '"
121
                    . $fields[$fieldDef->fieldName]
122
                    . "'"
123
                );
124
            }
125
126 2
            if (is_array($fieldDef->subTypes)) {
127 1
                $fields[$fieldDef->fieldName] = $this->processBuffer(
128 1
                    $fields[$fieldDef->fieldName],
129 1
                    $fieldDef->subTypes
130
                );
131
            }
132
        }
133
134 2
        return $fields;
135
    }
136
137
    public function key()
138
    {
139
        return $this->current;
140
    }
141
}
142