ServiceReader   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 100
ccs 28
cts 30
cp 0.9333
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getFields() 0 4 1
A current() 0 8 2
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 8 2
A count() 0 8 2
A getDataFromService() 0 4 1
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