Passed
Push — master ( 64a057...f2bf02 )
by Antonio Oertel
41s
created

ReaderDataset::valid()   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 1
    public function __construct($filepath)
20
    {
21 1
        $this->states = new \ArrayIterator();
22
23 1
        if (empty($filepath)) {
24
            $filepath = self::DATASETBR_STATE_FILE;
25
        }
26
27 1
        $this->filepath = $filepath;
28 1
    }
29
30 1
    public function read()
31
    {
32 1
        $resource = fopen($this->filepath, 'r');
33 1
        $states = [];
34 1
        $skipFirst = true;
35
36 1
        while (!feof($resource)) {
37 1
            $state = $this->readLine($resource);
38 1
            if ($state->isState() && !$skipFirst) {
39 1
                $states[] = $state;
40
            }
41 1
            $skipFirst = false;
42
        }
43
44 1
        $this->states = new \ArrayIterator($states);
45 1
        $this->rewind();
46 1
    }
47
48
    /**
49
     * @param $resource
50
     *
51
     * @return GenericState
52
     */
53 1
    private function readLine($resource)
54
    {
55 1
        $line = fgetcsv($resource, null, $this->separator, $this->enclosure, $this->escape);
56
57 1
        return new GenericState($line[4], $line[1], $line[0], $line[9]);
58
    }
59
60
    public function current()
61
    {
62
        return $this->states->current();
63
    }
64
65 1
    public function next()
66
    {
67 1
        $this->states->next();
68 1
    }
69
70 1
    public function key()
71
    {
72 1
        return $this->states->key();
73
    }
74
75 1
    public function valid()
76
    {
77 1
        return $this->states->valid();
78
    }
79
80 1
    public function rewind()
81
    {
82 1
        $this->states->rewind();
83 1
    }
84
}
85