Completed
Pull Request — master (#41)
by Romain
02:26
created

AbstractIteratorReader::createIterator()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 1
nc 1
1
<?php
2
3
namespace Pim\Bundle\ExcelConnectorBundle\Reader;
4
5
use Akeneo\Component\Batch\Model\StepExecution;
6
use Akeneo\Component\Batch\Item\AbstractConfigurableStepElement;
7
use Akeneo\Component\Batch\Item\ItemReaderInterface;
8
use Akeneo\Component\Batch\Step\StepExecutionAwareInterface;
9
10
/**
11
 * Abstract iterator based reader
12
 *
13
 * @author    Antoine Guigan <[email protected]>
14
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
15
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
16
 */
17
abstract class AbstractIteratorReader extends AbstractConfigurableStepElement implements ItemReaderInterface,
0 ignored issues
show
Coding Style introduced by
The first item in a multi-line implements list must be on the line following the implements keyword
Loading history...
18
    StepExecutionAwareInterface
19
{
20
    /** @var \Iterator */
21
    protected $iterator;
22
23
    /** @var StepExecution */
24
    protected $stepExecution;
25
26
    /** @var boolean */
27
    protected $batchMode;
28
29
    /**
30
     * @param boolean $batchMode
31
     */
32
    public function __construct($batchMode = false)
33
    {
34
        $this->batchMode = $batchMode;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function setStepExecution(StepExecution $stepExecution)
41
    {
42
        $this->stepExecution = $stepExecution;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function read()
49
    {
50
        if (!isset($this->iterator)) {
51
            $this->initializeIterator();
52
        }
53
54
        if (!$this->iterator->valid()) {
55
            return null;
56
        }
57
58
        $current = $this->iterator->current();
59
        if ($this->stepExecution) {
60
            $this->stepExecution->incrementSummaryInfo('read');
61
        }
62
        $this->iterator->next();
63
64
        return $this->convertNumericIdentifierToString($current);
65
    }
66
67
    /**
68
     * Resets the state of the reader
69
     */
70
    public function reset()
71
    {
72
        $this->iterator = null;
73
    }
74
75
    /**
76
     * Rewinds the iterator if it has already been initialized.
77
     * It allows to use the same service in two successive steps
78
     *
79
     * {@inheritdoc}
80
     */
81
    public function initialize()
82
    {
83
        $this->iterator = null;
84
    }
85
86
    /**
87
     * Converts an entity numerical identifier ('sku' for products,
88
     * 'code' for other entities) into string to allow import.
89
     *
90
     * @param array $item
91
     *
92
     * @return array
93
     */
94
    protected function convertNumericIdentifierToString(array $item)
95
    {
96
        return $item;
97
    }
98
99
    /**
100
     * Initializes the iterator
101
     */
102
    protected function initializeIterator()
103
    {
104
        $this->iterator = $this->createIterator();
105
        if ($this->batchMode) {
106
            $this->iterator = new \ArrayIterator(array(iterator_to_array($this->iterator)));
107
        }
108
        $this->iterator->rewind();
109
    }
110
111
    /**
112
     * Creates the iterator
113
     *
114
     * @return \Iterator
115
     */
116
    abstract protected function createIterator();
117
}
118