Completed
Pull Request — master (#12)
by lee
06:13
created

ReaderDataset::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Brazanation\States\Console\Commands\Generate;
4
5
class ReaderDataset implements \Iterator
6
{
7
    const DATASETBR_STATE_FILE = 'https://raw.githubusercontent.com/datasets-br/state-codes/master/data/br-state-codes.csv';
8
9
    private $states;
10
11
    private $separator = ',';
12
    private $enclosure = '"';
13
    private $escape = '\\';
14
    /**
15
     * @var string
16
     */
17
    private $filepath;
18
19 3
    public function __construct($filepath)
20
    {
21 3
        $this->states = new \ArrayIterator();
22
23 3
        if (empty($filepath)) {
24 2
            $filepath = self::DATASETBR_STATE_FILE;
25
        }
26
27 3
        $this->filepath = $filepath;
28 3
    }
29
30 3
    public function read()
31
    {
32 3
        $resource = fopen($this->filepath, 'r');
33 3
        $states = [];
34 3
        $skipFirst = true;
35
36 3
        while (!feof($resource)) {
37 3
            $state = $this->readLine($resource);
38 3
            if ($state->isState() && !$skipFirst) {
39 3
                $states[] = $state;
40
            }
41 3
            $skipFirst = false;
42
        }
43
44 3
        $this->states = new \ArrayIterator($states);
45 3
        $this->rewind();
46 3
    }
47
48
    /**
49
     * @param $resource
50
     *
51
     * @return GenericState
52
     */
53 3
    private function readLine($resource)
54
    {
55 3
        $line = fgetcsv($resource, null, $this->separator, $this->enclosure, $this->escape);
56
57 3
        return new GenericState($line[4], $line[1], $line[0], $line[9]);
58
    }
59
60 1
    public function current()
61
    {
62 1
        return $this->states->current();
63
    }
64
65 2
    public function next()
66
    {
67 2
        $this->states->next();
68 2
    }
69
70 2
    public function key()
71
    {
72 2
        return $this->states->key();
73
    }
74
75 2
    public function valid()
76
    {
77 2
        return $this->states->valid();
78
    }
79
80 3
    public function rewind()
81
    {
82 3
        $this->states->rewind();
83 3
    }
84
}
85