Completed
Push — master ( afdf8f...03c0bd )
by
unknown
05:18
created

PropelCollectionAbstractExport::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
13
namespace Thelia\ImportExport\Export;
14
15
use Propel\Runtime\ActiveQuery\ModelCriteria;
16
use Propel\Runtime\Map\TableMap;
17
18
abstract class PropelCollectionAbstractExport extends AbstractExport
0 ignored issues
show
Deprecated Code introduced by
The class Thelia\ImportExport\Export\AbstractExport has been deprecated: since 2.4, please use a specific AbstractExport (like JsonFileAbstractExport). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

18
abstract class PropelCollectionAbstractExport extends /** @scrutinizer ignore-deprecated */ AbstractExport
Loading history...
19
{
20
    /**
21
     * @var \Propel\Runtime\Util\PropelModelPager Data to export
22
     */
23
    private $data;
24
25
    /**
26
     * @return array|false|mixed|string
27
     * @throws \Exception
28
     */
29
    public function current()
30
    {
31
        $data = $this->data->getIterator()->current()->toArray(TableMap::TYPE_COLNAME, true, [], true);
32
33
        foreach ($this->data->getQuery()->getWith() as $withKey => $with) {
34
            $data = array_merge($data, $data[$withKey]);
35
            unset($data[$withKey]);
36
        }
37
38
        return $data;
39
    }
40
41
    /**
42
     * @return bool|float|int|string|null
43
     * @throws \Exception
44
     */
45
    public function key()
46
    {
47
        if ($this->data->getIterator()->key() !== null) {
48
            return $this->data->getIterator()->key() + ($this->data->getPage() - 1) * 1000;
49
        }
50
51
        return null;
52
    }
53
54
    /**
55
     * @throws \Exception
56
     */
57
    public function next()
58
    {
59
        $this->data->getIterator()->next();
60
        if (!$this->valid() && !$this->data->isLastPage()) {
61
            $this->data = $this->data->getQuery()->paginate($this->data->getNextPage(), 1000);
62
            $this->data->getIterator()->rewind();
63
        }
64
    }
65
66
    /**
67
     * @throws \Exception
68
     */
69
    public function rewind()
70
    {
71
        if ($this->data === null) {
72
            $data = $this->getData();
73
74
            if ($data instanceof ModelCriteria) {
75
                $this->data = $data->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)->keepQuery(false)->paginate(1, 1000);
76
                $this->data->getIterator()->rewind();
77
78
                return;
79
            }
80
81
            throw new \DomainException(
82
                'Data must be an instance of \\Propel\\Runtime\\ActiveQuery\\ModelCriteria'
83
            );
84
        }
85
86
        throw new \LogicException('Export data can\'t be rewinded');
87
    }
88
89
    /**
90
     * @return bool
91
     * @throws \Exception
92
     */
93
    public function valid()
94
    {
95
        return $this->data->getIterator()->valid();
96
    }
97
}
98