ServiceReader::current()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace Mathielen\DataImport\Reader;
4
5
use Ddeboer\DataImport\Reader\CountableReaderInterface;
6
7
/**
8
 * Reads data from a given service.
9
 */
10
class ServiceReader implements CountableReaderInterface
11
{
12
    /**
13
     * @var \Iterator
14
     */
15
    protected $iterableResult;
16
17
    /**
18
     * @var callable
19
     */
20
    private $callable;
21
22
    /**
23
     * @var array
24
     */
25
    private $arguments;
26
27 3
    public function __construct(callable $callable, array $arguments = array())
28
    {
29 3
        if (!is_callable($callable)) {
30
            throw new \InvalidArgumentException('Given callable is not a callable');
31
        }
32
33 3
        $this->callable = $callable;
34 3
        $this->arguments = $arguments;
35 3
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 1
    public function getFields()
41
    {
42 1
        return array_keys($this->current()); //TODO
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    public function current()
49
    {
50 2
        if (!$this->iterableResult) {
51
            $this->rewind();
52
        }
53
54 2
        return $this->iterableResult->current();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function next()
61
    {
62 2
        $this->iterableResult->next();
63 2
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function key()
69
    {
70 1
        return $this->iterableResult->key();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 2
    public function valid()
77
    {
78 2
        return $this->iterableResult->valid();
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 3
    public function rewind()
85
    {
86 3
        if (!$this->iterableResult) {
87 3
            $this->iterableResult = new \ArrayIterator($this->getDataFromService());
88
        }
89
90 3
        $this->iterableResult->rewind();
91 3
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 2
    public function count()
97
    {
98 2
        if (!$this->iterableResult) {
99 2
            $this->rewind();
100
        }
101
102 2
        return count($this->iterableResult);
103
    }
104
105 3
    private function getDataFromService()
106
    {
107 3
        return call_user_func_array($this->callable, $this->arguments);
108
    }
109
}
110