Completed
Pull Request — master (#43)
by Romain
02:36
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
     * Initializes the iterator allowing to use the same service in two successive steps
77
     *
78
     * {@inheritdoc}
79
     */
80
    public function initialize()
81
    {
82
        $this->iterator = null;
83
    }
84
85
    /**
86
     * Converts an entity numerical identifier ('sku' for products,
87
     * 'code' for other entities) into string to allow import.
88
     *
89
     * @param array $item
90
     *
91
     * @return array
92
     */
93
    protected function convertNumericIdentifierToString(array $item)
94
    {
95
        return $item;
96
    }
97
98
    /**
99
     * Initializes the iterator
100
     */
101
    protected function initializeIterator()
102
    {
103
        $this->iterator = $this->createIterator();
104
        if ($this->batchMode) {
105
            $this->iterator = new \ArrayIterator(array(iterator_to_array($this->iterator)));
106
        }
107
        $this->iterator->rewind();
108
    }
109
110
    /**
111
     * Creates the iterator
112
     *
113
     * @return \Iterator
114
     */
115
    abstract protected function createIterator();
116
}
117